有2个课程-
public class MainClass {
public static void main(String[] args) {
JFrame frame = new JFrame();
....
Component mouseClick = new MyComponent() ;
frame.setVisible(true);
}
public class MyComponent extends JComponent implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("here was a click ! ");
}
...
}
我试图在
frame
上设置鼠标单击的侦听器,但是当我运行它并按鼠标单击时,没有任何反应。如何使其工作?
最佳答案
为了在框架上获得鼠标单击,还需要将mouseClick
添加到框架的MouseListeners
列表中。创建mouseClick
后,尝试添加以下行:
frame.addMouseListener((MouseListener) mouseClick);