【问题标题】:ActionEvent not recognizing CTRL keybindingActionEvent 无法识别 CTRL 键绑定
【发布时间】:2016-01-08 19:44:25
【问题描述】:

我正在尝试将键绑定集成到我正在制作的程序中,但由于该程序很长,我正在尝试学习我在 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 返回的内容?请帮忙。

【问题讨论】:

    标签: character actionlistener key-bindings actionevent


    【解决方案1】:

    据我所知,如果一个 Action 没有明确设置其 actionCommand,它将在创建 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 ) );
                }
    

    【讨论】:

    • 第二个解决方案你把同样的东西放了两次。另外,为什么第二种解决方案有效? “\u0001”是 CTRL+A 的 Unicode 字符吗?如何修改它以便我可以使用不同的字母?
    猜你喜欢
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多