How to close a GUI when i push a JButton

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

    JButton close = new JButton("Close");
    close.addActionListener(this);
    public void actionPerformed(ActionEvent closing) { 
    // getSource() checks for the source of clicked Button , compares with the name of button in which here is close .     
    if(closing.getSource()==close)
       System.exit(0);
       // This exit Your GUI 
    }
    /*Some Answers were asking for @override which is overriding the method the super class or the parent class and creating different objects and etc which makes the answer too long . Note : we just need to import java.awt.*; and java.swing.*; and Adding this command : class className implements actionListener{} */
    

提交回复
热议问题