How to close a GUI when i push a JButton

后端 未结 7 868
无人共我
无人共我 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:39

    Add your button:

    JButton close = new JButton("Close");
    

    Add an ActionListener:

    close.addActionListner(new CloseListener());
    

    Add a class for the Listener implementing the ActionListener interface and override its main function:

    private class CloseListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            //DO SOMETHING
            System.exit(0);
        }
    }
    

    This might be not the best way, but its a point to start. The class for example can be made public and not as a private class inside another one.

提交回复
热议问题