有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侦听器上设置鼠标单击,但是当我运行它然后按鼠标单击时,没有任何反应。
frame
如何使其工作?
为了在框架上获得鼠标点击,您还需要添加mouseClick到的框架列表中MouseListeners。创建后尝试添加以下行mouseClick:
mouseClick
MouseListeners
frame.addMouseListener((MouseListener) mouseClick);