* 一个使用KeyStroke自定义快捷键的示例
*
*/
public class MnemonicT extends JFrame {
private int count = 0;
private JButton button;
public MnemonicT() {
super("Mnemonic Test");
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(320, 240));
panel.setLayout(new FlowLayout());
button = new JButton(new AbstractAction("ClickTimes:" + count) {
public void actionPerformed(ActionEvent e) {
button.setText("ClickTimes:" + (++count));
}
});
panel.add(button);
add(panel);
InputMap im = button.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("control A"), "buttonAction"); // 按CTRL+A
button.getActionMap().put("buttonAction", button.getAction());
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MnemonicT().setVisible(true);
}
});
}
}