怎样获得键盘事件
怎样在面板中获得键盘事件import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class D extends JFrame
{
public D()
{
setSize(300,200);
setVisible(true);
this.getContentPane().add(new AA());
}
public static void main(String args[])
{
new D();
}
}
class AA extends JPanel
{
JTextField text1;
JTextField text2;
JButton button1;
JButton button2;
public AA()
{
text1=new JTextField(15);
text2=new JTextField(15);
button1=new JButton("确定");
button2=new JButton("取消");
this.setLayout(new FlowLayout());
add(text1);
add(text2);
add(button1);
add(button2);
this.requestFocus();
System.out.println("aaaaaaaaa");
this.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
System.out.println("aaaaaaaaa");
}
});
}
}