我正在尝试将键绑定集成到我正在制作的程序中,但由于该程序很长,我正在尝试学习我在StackOverflow上找到的较小的类似编码程序。
这是我正在使用的代码:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
@SuppressWarnings("serial")
private void initGUI() {
JPanel content = new JPanel(new FlowLayout());
content.add(new JLabel("Test:"));
AbstractAction buttonPressed = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
System.out.println(e.getSource());
if ("a".equals(e.getActionCommand()))
content.setBackground(new Color(227, 19, 19));
if ("b".equals(e.getActionCommand()))
content.setBackground(new Color(0, 225, 19));
}
};
JButton submit = new JButton("Submit");
submit.addActionListener(buttonPressed);
submit.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_A,
java.awt.event.InputEvent.CTRL_DOWN_MASK),
"A_pressed");
submit.getActionMap().put("A_pressed", buttonPressed);
submit.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_B, 0),
"B_pressed");
submit.getActionMap().put("B_pressed", buttonPressed);
content.add(submit);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().initGUI();
}
});
}
}
我要做的是按CTRL + A并使背景改变颜色。但是,当我这样做时,System.out.println(e.getActionCommand())
返回一个看起来像一个问号的字符,就像它是一个未知的字符或其他东西。如果按B键,程序可以正常工作,但添加修饰符CTRL无法正常工作。
问题是我做得不对吗?该程序是否正常工作,我不知道如何比较e.getActionCommand()
和什么字符串CTRL + A作为ActionEvent返回?请帮忙。
答案 0 :(得分:0)
据我所知,如果Action没有显式设置其actionCommmand,则会在创建ActionEvent时设置它。在你的代码中,actionCommmand将是你的keyStroke的charKey的字符串表示形式(b和ctrl + a不是a) 所以我可以建议为每个动作采取不同的行动:
class simpleAction extends AbstractAction {
public simpleAction ( String name ) {
super ();
putValue ( Action.ACTION_COMMAND_KEY, name );
}
@Override
public void actionPerformed ( ActionEvent e ) {
System.out.println ( "getActionCommand----->" + e.getActionCommand () );
System.out.println ( "getSource----->" + e.getSource () );
if ( "a".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 227, 19, 19 ) );
}
if ( "b".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 0, 225, 19 ) );
}
}
}
JButton submit = new JButton ( "Submit" );
submit.addActionListener ( new simpleAction ( "Submit" ) );
submit.getInputMap ( javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ).put (
javax.swing.KeyStroke.getKeyStroke ( java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_DOWN_MASK ), "A_pressed" );
submit.getActionMap ().put ( "A_pressed", new simpleAction ( "a" ) );
submit.getInputMap ( javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ).put (
javax.swing.KeyStroke.getKeyStroke (
java.awt.event.KeyEvent.VK_B, 0 ),
"B_pressed" );
submit.getActionMap ().put ( "B_pressed", new simpleAction ( "b" ) );
或第二个解决方案是检查ctrl + a
的字符串表示所以你改变了
if ( "a".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 227, 19, 19 ) );
}
通过
if ( "\u0001".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 227, 19, 19 ) );
}