您需要添加一个动作监听器,特别是对于 actionPerformed。在构造函数中的某处声明它:
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class Main {
public static void main(String[] argv) throws Exception {
JButton component = new JButton();
MyAction action = new MyAction();
component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"),
action.getValue(Action.NAME));
}
}
class MyAction extends AbstractAction {
public MyAction() {
super("my action");
}
public void actionPerformed(ActionEvent e) {
//Here goes the code where the button does something
System.out.println("hi");//In this case we print hi
}
}
在这个例子中,如果我们按下 F2 就相当于按下按钮。