Java addMouseListener 或 addActionListener 或 JButton?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3616761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
addMouseListener or addActionListener or JButton?
提问by Hectoret
When defining the behaviour of a simple click on a JButton, which is the right way to do it? And, what's the difference?
在定义对 JButton 进行简单单击的行为时,正确的做法是什么?而且,有什么区别?
JButton but = new JButton();
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked the button, using an ActionListener");
}
});
or
或者
JButton but = new JButton();
but.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("You clicked the button, using a MouseListenr");
}
});
采纳答案by jfpoilpret
MouseListener
is a low-level event listener in Swing (and AWT by the way).
MouseListener
是 Swing 中的低级事件侦听器(顺便说一下,还有 AWT)。
ActionListener
is higher-level and should be used.
ActionListener
是更高级别的,应该使用。
Better than ActionListener
though, one should use a javax.swing.Action
(which is actually an ActionListener
).
更好的ActionListener
是,应该使用 a javax.swing.Action
(实际上是 an ActionListener
)。
Using an Action
allows to share it among several widgets (eg JButton
, JMenuItem
...); not only do you share the code that is triggered when the button/menu is pushed, but also the state is shared, in particular the fact whether the action (and its associated widgets) is enabled or not.
使用Action
允许在多个小部件之间共享它(例如JButton
,JMenuItem
...);您不仅共享按下按钮/菜单时触发的代码,而且共享状态,尤其是操作(及其关联的小部件)是否启用的事实。
回答by vladmihaisima
You should be able to press that button using keyboard also. So, if you add just the mouse listener you will not get the 'press' event if using keyboard.
您也应该可以使用键盘按下该按钮。因此,如果您仅添加鼠标侦听器,则在使用键盘时将不会收到 'press' 事件。
I would go for the action listener, it's more clear.
我会选择动作监听器,它更清楚。
回答by Andreas Dolk
A registered ActionListener
is invoked when the Button fires an Action event, the MouseListener
is invoked when the widget detects a mouse click.
ActionListener
当 Button 触发 Action 事件时调用注册,MouseListener
当小部件检测到鼠标单击时调用。
In your example both approaches show the same behaviour when you use a mouse to click on the button. But give focus to the button and press SPACE
, this should fire an action event and trigger the Action Listener but not the mouse listener.
在您的示例中,当您使用鼠标单击按钮时,两种方法都显示相同的行为。但是将焦点放在按钮上并按下SPACE
,这应该会触发一个动作事件并触发动作监听器而不是鼠标监听器。
It is advisable to use the ActionListener on Buttons, otherwise you'll not be able to control the application with a keyboard or you would be in the need to add another key event listener.
建议在按钮上使用 ActionListener,否则您将无法使用键盘控制应用程序,或者您需要添加另一个按键事件侦听器。
回答by Brent
If you want to do something when a Jbutton is clicked, action listener is better because mouse listener does not recognize that a mouse is clicked on a button if a user presses the mouse on a JButton and then moves the mouse a little bit before releasing the mouse button while remaining within the button the whole time, but action listener does. MouseListener requires that mouse clicks have no movement between mouse press and mouse release, which is not the case for my users.
如果您想在单击 JButton 时执行某些操作,则动作侦听器会更好,因为如果用户在 JButton 上按下鼠标,然后在释放鼠标之前稍微移动鼠标,则鼠标侦听器无法识别鼠标已单击按钮鼠标按钮始终保持在按钮内,但动作侦听器确实如此。MouseListener 要求鼠标点击在鼠标按下和鼠标释放之间没有移动,这对我的用户来说不是这种情况。