【发布时间】:2014-01-18 07:25:16
【问题描述】:
我遇到了动作监听器的问题。 问题是这样的:
private static void createAndShowGUI() {
int i;
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Best Game Ever");
JButton buttonA = new JButton("Press 'z'");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel(),BorderLayout.CENTER);
f.add(buttonA, BorderLayout.PAGE_END);
f.pack();
f.setVisible(true);
final char ACTION_KEY = 'z';
final char ACTION_KEY2 ='x';
Action actionListener = new AbstractAction()
{
public void actionPerformed(ActionEvent actionEvent)
{
}
};
Action actionListener2 = new AbstractAction()
{
public void actionPerformed(ActionEvent actionEvent)
{
System.out.println("x");
}
};
KeyStroke z = KeyStroke.getKeyStroke('z');
InputMap inputMap = buttonA.getInputMap();
inputMap.put(z, ACTION_KEY);
ActionMap actionMap = buttonA.getActionMap() ;
actionMap.put(ACTION_KEY, actionListener);
KeyStroke x = KeyStroke.getKeyStroke('x');
InputMap inputMap2 = buttonA.getInputMap();
inputMap2.put(x, ACTION_KEY2);
ActionMap actionMap2 = buttonA.getActionMap();
actionMap2.put(ACTION_KEY2, actionListener2);
}
我只是无法为它编写一个代码,即使我拿着钥匙也只打印一次 x。 谢谢。
很抱歉没有把所有的代码:(
希望它现在有意义。
【问题讨论】:
-
您的操作在哪里触发???
-
你遇到了什么问题?
标签: java swing action actionlistener