简体   繁体   中英

How to close JFrame in Eclipse

I'm working in Java AWT and when the window appears, I don't know how to close it. I try to Alt+F4 or use

[setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)] but this function wasn't called in Eclipse.

Eclipse IDE version 2018-12.

 package test2;

import java.awt.Button;
import java.awt.Frame;

import javax.swing.JFrame;


public class java12 extends Frame{
public java12() {
    setTitle("Demo Java AWT");

    Button object=new Button("Click me");

    object.setBounds(100, 100, 100, 100);

    add(object);

    setSize(90, 30);

    setLayout(null);

    setVisible(true);


}

public static void main(String[] args) {
    new java12();
}

}

It only can close by Task Manager, how can I close it by put something in my code

setDefaultCloseOperation determines what happens when the close button is clicked and takes the following parameters:

  • WindowConstants.EXIT_ON_CLOSE
  • WindowConstants.DISPOSE_ON_CLOSE
  • WindowConstants.HIDE_ON_CLOSE
  • WindowConstants.DO_NOTHING_ON_CLOSE

Example :

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
                // Ask for confirmation before terminating the program.
                int option = JOptionPane.showConfirmDialog(
                        frame, 
                        "Are you sure you want to close the application?",
                        "Close Confirmation", 
                        JOptionPane.YES_NO_OPTION, 
                        JOptionPane.QUESTION_MESSAGE);
                if (option == JOptionPane.YES_OPTION) {
                        System.exit(0);
                }
        }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM