How do I simplify MouseListener so that I don't have all these unused methods?

前端 未结 2 649
攒了一身酷
攒了一身酷 2021-01-27 12:17

Below I have the following code, so that when someone clicks on the \"Close\", the window will close. Below that is another exit button on the same menu bar, simply for redundan

相关标签:
2条回答
  • 2021-01-27 12:53
    closeFile.addMouseListener(new MouseAdapter(){
         public void mouseClicked(MouseEvent e) {
            //your code
         }                
      });
    

    Note: You dont have to write 'implements MouseListener' during class definition. For more information, search for adapter classes, more specifically for MouseAdapter class.

    0 讨论(0)
  • 2021-01-27 13:18

    Use a MouseAdapter and override the methods that you want.

    closeFile.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                System.exit(0);
            }
        });
    
    0 讨论(0)
提交回复
热议问题