myKeyboardPanel.java 5/3/2012 import javax.swing.*; import java ...

snottybugbearΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 4 χρόνια και 1 μήνα)

80 εμφανίσεις

myKeyboardPanel.java 5/3/2012
import
javax
.
swing
.
*
;
import
java
.
awt
.
*
;
import
java
.
awt
.
event
.
*
;
import
java
.
util
.
*
;
public
class
myKeyboardPanel
extends
JPanel
implements
ActionListener
{
private
final
int
WIDTH
=
300
,
HEIGHT
=
200
;
private
int
x
=
100
,
y
=
100
;
private
String
keyString
=
"none"
;
private
JButton
b1
;
public
myKeyboardPanel
()
{
addKeyListener
(
new
DotsListener
())
;
setBackground
(
Color
.
black
)
;
setPreferredSize
(
new
Dimension
(
WIDTH
,
HEIGHT
))
;
b1
=
new
JButton
(
"Start"
)
;
b1
.
addActionListener
(
this
)
;
add
(
b1
)
;
}
public
void
paintComponent
(
Graphics
page
)
{
super
.
paintComponent
(
page
)
;
page
.
setColor
(
Color
.
green
)
;
page
.
drawString
(
"Hello!"
,
x
,
y
)
;
repaint
()
;
}
public
void
actionPerformed
(
ActionEvent
event
)
{
requestFocus
()
;
}
private
class
DotsListener
implements
KeyListener
{
public
void
keyPressed
(
KeyEvent
event
)
{
int
keyCode
=
event
.
getKeyCode
()
;
if
(
keyCode
=
=
KeyEvent
.
VK_DOWN
)
y
=
y
+
1
;
else
if
(
keyCode
=
=
KeyEvent
.
VK_UP
)
y
=
y
-
1
;
}
public
void
keyTyped
(
KeyEvent
event
)
{
int
id
=
event
.
getID
()
;
1
myKeyboardPanel.java 5/3/2012
char
c
=
event
.
getKeyChar
()
;
if
(
c
=
=
'd'
)
y
=
y
+
1
;
else
if
(
c
=
=
'u'
)
y
=
y
-
1
;
}
public
void
keyReleased
(
KeyEvent
event
)
{}
}
}
2