如何检查JComponent上是否按下了鼠标

时间:2012-08-02 14:57:26

标签: java jcomponent

我有一个NN组件,我希望它在按下组件时执行一段代码。有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

JComponent comp = new JPanel();
comp.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
        // Place your code here
    }
});

答案 1 :(得分:0)

假设您有一个扩展JComponent的类

import java.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyClass extends JComponent{
    public MyClass(){

        //Other code

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // Place your code here
            }
        });

        //Other code
    }
}