How to close a GUI when i push a JButton

后端 未结 7 892
无人共我
无人共我 2020-12-10 11:58

Does anyone know how to make a jbutton close a gui? I think it is like System.CLOSE(0); but that didnt work. it also could be exitActionPerformed(evt);

7条回答
  •  忘掉有多难
    2020-12-10 12:36

    In Java 8, you can use Lambda expressions to make it simpler.

    Close application

    JButton btnClose = new JButton("Close");
    btnClose.addActionListener(e -> System.exit(0));
    

    Close window

    JButton btnClose = new JButton("Close");
    btnClose.addActionListener(e -> this.dispose());
    

提交回复
热议问题