我有一个NN组件,我希望它在按下组件时执行一段代码。有人能帮助我吗?
答案 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
}
}