我有一個JButton,我希望它在按下按鈕時執行某些操作,並且當按下某個按鍵時我希望它做同樣的事情,我該怎麼做?JButton with actionPerformed and keyReleased
回答
做某事時按下按鈕,您應該添加一個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);
對於鍵綁定而言,但代碼應該使用Action而不是ActionListener 。然後該操作可以被按鈕和鍵綁定使用。 – camickr
我可以舉一個完整的例子嗎? – KiraHelsing
@ KiraHelsing-希望[這](http://www.javaprogrammingforums.com/java-swing-tutorials/278-how-add-actionlistener-jbutton-swing.html)幫助。 – mok
據我所知(與Swing在幾年...),可以在一個按鈕動作的唯一按鍵事件是Tab
從而改變了元素焦點,Enter
沒有搞砸哪觸發actionPerformed
方法和助記符,這也引發actionPerformed
。
來處理按鈕點擊事件,你可以不喜歡這樣:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
}
});
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的
你可能想看看進入Action
API。你可以在How to Use Actions看到更多。您可以使用Action
作爲按鈕(與ActionListener
一樣工作),並且您可以向其添加快捷鍵。
您可以看到this example其中的快捷鍵被添加到工具欄按鈕以及菜單項。你想要注意的有趣的事情是菜單項和工具欄按鈕共享Action
,因此做同樣的事情。
+1,用於使用「Action」而不是ActionListener。現在該操作可以由按鈕和鍵盤使用。 – camickr
試試這個:
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來處理鍵盤事件。 – camickr
但鍵綁定需要專用的組合,它們不適用於例如調試目的。因爲我不知道開幕後的確切計劃是什麼,所以我通常會嘗試複製他想要做的確切行爲。 –
- 1. Java JButton actionPerformed freeze
- 2. textfield with actionperformed actionlistner
- 3. JButton actionPerformed方法提供空
- 4. 在ActionPerformed上更改JButton顏色
- 5. Java Swing問題與JButton選擇/ ActionPerformed
- 6. $ rootScope with TypeScript and controllerAs
- 7. bluetooth with android and raspberrypi
- 8. LazyLoading with spring and hibernate
- 9. Tkinter with after and time.sleep
- 10. Destructuration with imports and Typescript
- 11. ValueError with python and django
- 12. Rails with Backbone.js and Handlebars
- 13. recv with MSG_NONBLOCK and MSG_WAITALL
- 14. ItemsControl with SharedSizeGroup AND stretch
- 15. filterDescendants and findDescendant with slate.js
- 16. Viewpager With tabLayout and PagerTabStrip
- 17. Stuck with Thread and updateGraphics
- 18. Select2 with Rails and JSON
- 19. simpleform and country_select with iso_codes
- 20. Ninject with ActionFilters and SignalR
- 21. Android:Thread with progressBar and setcurrentTab
- 22. AutoComplete with service and Masterpage
- 23. junit with spring and mybatis
- 24. regex_search and regex_replace with Boost
- 25. GroupBy with elementSelector and resultSelector
- 26. EXC_BAD_ACCESS with box2d and b2body
- 27. Article and Aside with padding
- 28. Fancybox with images and youtube
- 29. Php if statement with and
- 30. 'this'scope with ES6 and Knockout
你做了什麼? – mok
pleases whats - '當某個鍵被按下時,' - 因爲只有'ENTER'和'TAB'鍵被內置在加速器中 – mKorbel