在swing中如何给窗口加一个F1的快捷键?

import   java.awt.AWTEvent;
import   java.awt.Toolkit;
import   java.awt.event.AWTEventListener;
import   java.awt.event.KeyEvent;

import   javax.swing.JFrame;
import   javax.swing.JRadioButton;
import   javax.swing.JTextField;

public   class   WindowF1   extends   JFrame{
private   Toolkit   toolkit   =   Toolkit.getDefaultToolkit();

public   WindowF1()   {
toolkit.addAWTEventListener(new   CapListener(),   AWTEvent.KEY_EVENT_MASK);
JTextField   jt1   =   new   JTextField();
jt1.setBounds(30,30,60,20);
JRadioButton   jr1   =   new   JRadioButton();
jr1.setBounds(60,60,20,20);
getContentPane().setLayout(null);
add(jt1);
add(jr1);
setSize(400,   300);
setVisible(true);
setDefaultCloseOperation(3);
}

public   static   void   main(String   args[])   {
new   WindowF1();
}

//   toolkit
//   实现一个监听器:
class   CapListener   implements   AWTEventListener   {
public   void   eventDispatched(AWTEvent   event)   {
Object   o   =   event;
if(((KeyEvent)event).getKeyCode()   ==   KeyEvent.VK_F1){
System.out.println( "F1 ");
}
}
}

}

你可能感兴趣的:(swing)