我有一个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