2014-04-10 85 views
-2

我有一个JButton,我希望它在按下按钮时执行某些操作,并且当按下某个按键时我希望它做同样的事情,我该怎么做?JButton with actionPerformed and keyReleased

+0

你做了什么? – mok

+1

pleases whats - '当某个键被按下时,' - 因为只有'ENTER'和'TAB'键被内置在加速器中 – mKorbel

回答

2

做某事时按下按钮,您应该添加一个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); 
+0

对于键绑定而言,但代码应该使用Action而不是ActionListener 。然后该操作可以被按钮和键绑定使用。 – camickr

+0

我可以举一个完整的例子吗? – KiraHelsing

+0

@ KiraHelsing-希望[这](http://www.javaprogrammingforums.com/java-swing-tutorials/278-how-add-actionlistener-jbutton-swing.html)帮助。 – mok

1

据我所知(与Swing在几年...),可以在一个按钮动作的唯一按键事件是Tab从而改变了元素焦点,Enter没有搞砸哪触发actionPerformed方法和助记符,这也引发actionPerformed

来处理按钮点击事件,你可以不喜欢这样:

button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       //Execute when button is pressed 
      } 
     }); 
0
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"); 
     } 
    });  

这是如何使用的ActionListener与JButton的

1

你可能想看看进入Action API。你可以在How to Use Actions看到更多。您可以使用Action作为按钮(与ActionListener一样工作),并且您可以向其添加快捷键。

您可以看到this example其中的快捷键被添加到工具栏按钮以及菜单项。你想要注意的有趣的事情是菜单项和工具栏按钮共享Action,因此做同样的事情。

enter image description here

+0

+1,用于使用“Action”而不是ActionListener。现在该操作可以由按钮和键盘使用。 – camickr

0

试试这个:

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!"); 
     } 
    } 
} 
+0

请勿使用KeyListener !!! Swing被设计成与'Key Bindings'一起使用。所有Swing组件都使用Key Bindings来处理键盘事件。 – camickr

+0

但键绑定需要专用的组合,它们不适用于例如调试目的。因为我不知道开幕后的确切计划是什么,所以我通常会尝试复制他想要做的确切行为。 –