1

有 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监听器上设置鼠标单击,但是当我运行它然后按下鼠标单击时没有任何反应。

如何使它工作?

4

1 回答 1

3

为了在您的框架上接收鼠标点击,您还需要mouseClickMouseListeners. 创建后尝试添加此行mouseClick

frame.addMouseListener((MouseListener) mouseClick);
于 2013-03-28T15:33:31.767 回答