Java Reference
In-Depth Information
56
// set second and third lines of output
57
private
void
setLines2and3(KeyEvent event)
58
{
59
line2 = String.format(
"This key is %san action key"
,
60
event.isActionKey()
(
?
""
:
"not "
));
61
62
String temp = KeyEvent.getKeyModifiersText(event.getModifiers());
63
64
line3 = String.format(
"Modifier keys pressed: %s"
,
65
(temp.equals(
""
) ?
"none"
: temp));
// output modifiers
66
67
textArea.setText(String.format(
"%s\n%s\n%s\n"
,
68
line1, line2, line3));
// output three lines of text
69
}
70
}
// end class KeyDemoFrame
Fig. 12.36
|
Key event handling. (Part 3 of 3.)
1
// Fig. 12.37: KeyDemo.java
2
// Testing KeyDemoFrame.
3
import
javax.swing.JFrame;
4
5
public
class
KeyDemo
6
{
7
public
static
void
main(String[] args)
8
{
9
KeyDemoFrame keyDemoFrame =
new
KeyDemoFrame();
10
keyDemoFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
keyDemoFrame.setSize(
350
,
100
);
12
keyDemoFrame.setVisible(
true
);
13
}
14
}
// end class KeyDemo
Fig. 12.37
|
Testing
KeyDemoFrame
. (Part 1 of 2.)