2014-04-10 86 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

但鍵綁定需要專用的組合,它們不適用於例如調試目的。因爲我不知道開幕後的確切計劃是什麼,所以我通常會嘗試複製他想要做的確切行爲。 –