简体   繁体   English

如何在Eclipse中关闭JFrame

[英]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. 我正在Java AWT中工作,当窗口出现时,我不知道如何关闭它。 I try to Alt+F4 or use 我尝试使用Alt + F4或使用

[setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)] but this function wasn't called in Eclipse. [setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)],但是未在Eclipse中调用此函数。

Eclipse IDE version 2018-12. Eclipse IDE版本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: setDefaultCloseOperation确定单击关闭按钮时发生的情况,并采用以下参数:

  • WindowConstants.EXIT_ON_CLOSE WindowConstants.EXIT_ON_CLOSE
  • WindowConstants.DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE
  • WindowConstants.HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE
  • WindowConstants.DO_NOTHING_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);
                }
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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