【问题标题】:JButton with actionPerformed and keyReleased带有 actionPerformed 和 keyReleased 的 JButton
【发布时间】:2014-04-10 12:23:36
【问题描述】:

我有一个 JButton,我希望它在按下按钮时做一些事情,我希望它在按下一个键时做同样的事情,我该怎么做?

【问题讨论】:

  • 请问是什么 -- when a key is pressed, -- 因为只有 ENTERTAB 键是内置加速键

标签: java swing jbutton actionlistener


【解决方案1】:

要在按下按钮时执行某些操作,您应该向该按钮添加 ActionListener,如下所示:

JButton button = new JButton();
button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
});

为了响应按键按下做这样的事情:(例如,如果用户输入控制 alt 7)

Action actionListener = new AbstractAction() {
  public void actionPerformed(ActionEvent actionEvent) {
    JButton source = (JButton) actionEvent.getSource();
    System.out.println("Activated: " + source.getText());// just for test
  }
};
//.....

KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = button.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = button.getActionMap();
actionMap.put(ACTION_KEY, actionListener);

【讨论】:

  • +1,用于键绑定,但代码应使用 Action,而不是 ActionListener。然后 Action 可以被按钮和键绑定使用。
  • 我可以举一个完整的例子吗?
  • @KiraHelsing- 希望this 有帮助。
  • @camickr- 很高兴您发现我的回答很有用,如果我的回答的第二部分有点混乱,我很抱歉;实际上动作监听器是一个Action,它的名字只是为了有意义。无论如何,我编辑我的答案以使其更清楚。
【解决方案2】:

据我所知(几年来还没有弄乱 Swing...)唯一可以作用于按钮的键按钮事件是 Tab 改变元素焦点,Enter 触发actionPerformed 方法和助记符,它们也会触发 actionPerformed

要处理按钮点击事件,您可以这样做:

 button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
            }
        });  

【讨论】:

    【解决方案3】:

    您可能想查看Action API。您可以在How to Use Actions 看到更多信息。您可以将Action 用于按钮(类似于ActionListener),您可以为其添加快捷键。

    您可以看到this example 将快捷键添加到工具栏按钮以及菜单项的位置。您需要注意的有趣的事情是菜单项和工具栏按钮共享相同的Action,因此执行相同的操作。

    【讨论】:

    • +1,用于使用 Action 而不是 ActionListener。现在 Action 可以被按钮和键盘使用了。
    【解决方案4】:
        JButton button = new JButton("Submit");
        //Add action listener to button
        button.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                //do your work here
                System.out.println("You clicked the submit button");
            }
        });      
    

    这是如何使用带有 jbutton 的 actionlistener

    【讨论】:

      【解决方案5】:

      试试这个:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class EventedButtonExample extends JFrame {
          private JButton simpleButton;
          private static final long serialVersionUID = 42L;
      
          public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      EventedButtonExample me = new EventedButtonExample();
                      me.initialize();
                      me.setVisible(true);
                  }
              });
          }
      
          void initialize() {
              simpleButton = new JButton("I am an simple, evented button!");
              CompoundEventHandler eh = new CompoundEventHandler();
              simpleButton.addActionListener(eh);
              simpleButton.addKeyListener(eh);
              getContentPane().add(simpleButton, BorderLayout.CENTER);
              pack();
          }
      
          class CompoundEventHandler extends KeyAdapter implements ActionListener {
              public void keyReleased(KeyEvent e) {
                  int keyCode = e.getKeyCode();
                  System.out.println("Key pressed: " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")");
              }
      
              public void actionPerformed(ActionEvent e) {
                  System.out.println("A button is pressed!");
              }
          }
      }
      

      【讨论】:

      • 不要使用 KeyListener!!! Swing 旨在与Key Bindings 一起使用。所有 Swing 组件都使用 Key Bindings 来处理键盘事件。
      • 但是键绑定需要一个专门的组合,它们不能很好地用于调试目的。因为我不知道开帖者的具体计划是什么,所以我通常会尝试复制他想要做的确切行为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-22
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多