日期:2014-05-20 浏览次数:20681 次
package ch4; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JLabel; public class KeyEventTest extends JFrame { JLabel j1=new JLabel("您输入的东西:"); KeyEventTest() { Container main=getContentPane(); main.setLayout(new GridLayout(2,1)); main.add(j1); main.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { int i=e.getKeyCode(); String s=e.getKeyModifiersText(1); j1.setText(s); if(e.getKeyChar()=='a') System.out.print(i); } }); } public static void main(String[] args) { JFrame f=new KeyEventTest(); f.setBounds(100,100,300,250); f.setVisible(true); } }