Java Reference
In-Depth Information
*andamessage printed in the text area.
**/
KeyTestPanel ()
{
setLayout (new GridLayout (2,1));
JPanel canvas
=
new JPanel ();
add (canvas, BorderLayout.NORTH);
canvas.setBackground (Color.YELLOW);
fTextArea
=
new JTextArea ();
fTextArea.setEditable (false);
// Add to a scrollpanesothatalonglistof
// keyinputs can be seen.
JScrollPane area
-
scroll
-
pane
=
new JScrollPane
(fTextArea);
add (area
-
scroll
-
pane, BorderLayout.CENTER);
// Add to the panel an anonymous KeyAdapter that will
// respond to key strokes.
canvas.addKeyListener (
new KeyAdapter ()
{
public void keyTyped (KeyEvent e)
{
saySomething (
"
got
"
+e.getKeyChar (), e);
}
}
// end anonymous class
); // end method call
// Let the canvas panel get the focus.
canvas.setFocusable (true);
}
// ctor
/** Display a message in text area about the key
event. **/
void saySomething (String eventDescription, KeyEvent e)
{
fTextArea.append (eventDescription +
"
on
"
+e.getComponent ().getClass ().getName ()
+
"\
n
"
);
}
}
// class KeyTestPanel
The
KeyListener
interface also has the
keyPressed()
,
keyReleased()
,
and
keyTyped()
methods that fire a
KeyEvent
when a key is pressed, released,
Search WWH ::
Custom Search