Java鼠标监听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2668718/
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
Java MouseListener
提问by iTEgg
I have a bunch of JLabels and i would like to trap mouse click events. at the moment i am having to use:
我有一堆 JLabels,我想捕获鼠标点击事件。目前我不得不使用:
public void mouseClicked(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("Welcome to Java Programming!");
}
I was wondering if there is a tidier way of doing this instead of having a bunch of events I do not wish trap?
我想知道是否有更整洁的方法来执行此操作,而不是进行一堆我不希望陷入困境的事件?
EDIT:
编辑:
class MyAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
System.out.println(event.getComponent());
}
}
the above works but netBeans says add @override anotation. what does this mean?
以上工作,但netBeans 说添加@override 注释。这是什么意思?
EDIT: ok got it. fixed and solved.
编辑:好的,明白了。固定并解决。
采纳答案by ring bearer
Use MouseAdapter()
用 MouseAdapter()
An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects. So you need to implement only the method you like such as following example:
用于接收鼠标事件的抽象适配器类。这个类中的方法是空的。此类的存在是为了方便创建侦听器对象。所以你只需要实现你喜欢的方法,比如下面的例子:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public MainClass() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me);
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
回答by Bill the Lizard
You can extend MouseAdapterinstead, and just override the events you're really interested in.
您可以改为扩展MouseAdapter,只需覆盖您真正感兴趣的事件。
回答by coobird
One could use a MouseAdapter
class, which implements the MouseListener
interface, so one does not need to implement all the methods.
可以使用MouseAdapter
实现MouseListener
接口的类,因此不需要实现所有方法。
However, by overriding the methods of interest, one can get the desired behavior. For example, if one overrides the mouseClicked
method, then one can define some behavior for the mouse click event.
但是,通过覆盖感兴趣的方法,可以获得所需的行为。例如,如果重写该mouseClicked
方法,则可以为鼠标单击事件定义一些行为。
For example (untested code):
例如(未经测试的代码):
JLabel label = new JLabel("Hello");
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked!");
}
});
In the code above, the JLabel
will print "Clicked!
" to the console upon being clicked on.
在上面的代码中,点击JLabel
后将打印“ Clicked!
”到控制台。
回答by jarnbjo
You can inherit from java.awt.event.MouseAdapter
and only override the methods for the event(s) you are interested in.
您可以继承java.awt.event.MouseAdapter
并仅覆盖您感兴趣的事件的方法。
回答by Arthur Tsidkilov
some example of mouse event clicked,
单击鼠标事件的一些示例,
in the same way you can use mousePressed or other mouse events
以同样的方式您可以使用 mousePressed 或其他鼠标事件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Work1 extends JFrame{
private JPanel panelNew;
public Work1(){
super("Work 1");
// create Panel
panelNew = new JPanel();
panelNew.setLayout(null);
panelNew.setBackground(Color.cyan );
add(panelNew);
// create Button
JButton btn = new JButton("click me");
// position and size of a button
btn.setBounds(100, 50, 150, 30);
panelNew.add(btn);
// add event to button
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
// change text of button after click
if (btn.getText() == "abraCadabra"){
btn.setText("click me again") ;
}
else btn.setText("abraCadabra");
}
});
}
public static void main(String[] args){
Work1 go1 = new Work1();
go1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go1.setSize(320,200);
go1.setVisible(true);
}
}