本题的功能是监听键盘键的敲击,并显示在窗口中。 import javax,.swing.*; importjavA.awt.*; , import javA.awt.event.*; public class java3 extends JFramE.extends KeyListener { private String linel=''line2='' private String line3='' private JTextArea textArea; public java3 { super('java3'); textArea=new JTextArea(10,15); textAreA.setText('Press any key on the key- board…'); textAreA.setEnabled(false); addKeyListener(this); getContentPanE.add(textArea); setSize(350,100); show; } public void keyPressed(KeyEvent e) { linel='Key pressed:'+E.getKeyText(E.getKeyCode); setLines2and3(e); } public void keyReleased(KeyEvent e) { linel='Key released:'+E.getKeyText(E.getKeyCode); setLines2and3(e): } public void keyTyped(KeyEvent e) { Linel='Key typed:'+E.getKeychar; setLines2and3(e); } private void setLines2and3(KeyEvent e) { line2='This key is'+(E.isActionKey?'' :'not')+'an action key'; String temp=E.getKeyModifiersText(E.get- Modifiers); hne3='Modifier keys pressed:'+(temp.e- quals('')?'none':temp); textAreA.setText(linel+'\n'+line2+'\n' +line3+'\n'); } public static void main(String args[]) { java3 app=new java3; addWindowListener(new Windowadapted { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
A.swing.*;
B.awt.*;
C.awt.event.*;
D.setText('Press
E.setEnabled(false);
F.add(textArea);
G.getKeyText(E.
H.getKeyText(E.
I.getKeychar;
J.isActionKey?''
K.getKeyModifiersText(E.get-
L.e-
M.setText(linel+'\n'+line2+'\n'
N.exit(0);