在 java/swing 中关闭窗口时采取的正确操作是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5540354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:36:28  来源:igfitidea点击:

What is the right action to take upon closing windows in java/swing?

javaswingdialog

提问by Jason S

I just wrote this test code in my CustomUIPanel class:

我刚刚在我的 CustomUIPanel 类中编写了这个测试代码:

public static void main(String[] args) {
    final JDialog dialog = CustomUIPanel.createDialog(null, 
       CustomUIPanel.selectFile());
    dialog.addWindowListener(new WindowAdapter() {
        @Override public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

It works correctly if CustomUIPanel.main()is the program's entry point, but it makes me wonder something: what if another class called CustomUIPanel.main()for testing? Then my call to System.exit(0)is incorrect.

如果CustomUIPanel.main()是程序的入口点,它可以正常工作,但它让我想知道:如果另一个类调用CustomUIPanel.main()测试怎么办?那么我的调用System.exit(0)是不正确的。

Is there a way to tell the Swing event dispatch thread to exit automatically if there are no top-level windows?

如果没有顶级窗口,有没有办法告诉 Swing 事件调度线程自动退出?

If not, what's the right thing for a JDialog/JFrame to do upon closing if the goal is for the program to exit when all the top level windows are closed?

如果不是,如果目标是让程序在所有顶级窗口都关闭时退出,那么 JDialog/JFrame 在关闭时应该做什么?

回答by trashgod

You can use the setDefaultCloseOperation()method of JDialog, specifying DISPOSE_ON_CLOSE:

您可以使用 的setDefaultCloseOperation()方法JDialog,指定DISPOSE_ON_CLOSE

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

See also 12.8 Program Exit.

另见12.8 程序退出

Addendum: Incorporating @camickr's helpful answer, this example exits when either the window is closed or the close button is pressed.

附录:结合@camickr 的有用答案,此示例在窗口关闭或关闭按钮被按下时退出。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

/** @see http://stackoverflow.com/questions/5540354 */
public class DialogClose extends JDialog {

    public DialogClose() {
        this.setLayout(new GridLayout(0, 1));
        this.add(new JLabel("Dialog close test.", JLabel.CENTER));
        this.add(new JButton(new AbstractAction("Close") {

            @Override
            public void actionPerformed(ActionEvent e) {
                DialogClose.this.setVisible(false);
                DialogClose.this.dispatchEvent(new WindowEvent(
                    DialogClose.this, WindowEvent.WINDOW_CLOSING));
            }
        }));
    }

    private void display() {
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DialogClose().display();
            }
        });
    }
}

回答by camickr

Not sure about when using a JDialog.

不确定何时使用 JDialog。

But when using a JFrame you should use frame.dispose(). If the frame is the last open frame then the VM will exit.

但是当使用 JFrame 时,你应该使用 frame.dispose()。如果该帧是最后一个打开的帧,则 VM 将退出。

Note a dialog does not have an EXIT_ON_CLOSE option since it should not generally exit the VM.

请注意,对话框没有 EXIT_ON_CLOSE 选项,因为它通常不应退出 VM。

When closing the dialog you could always get the dialogs parent frame. Then you could dispatch an event to the frame to tell it to close itself. Something like:

关闭对话框时,您始终可以获得对话框的父框架。然后你可以向框架发送一个事件来告诉它关闭自己。就像是:

WindowEvent windowClosing = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
//Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowClosing);
frame.dispatchEvent(windowClosing);

回答by Avanish Kumar

Use

利用

this.dispose();

It should work.

它应该工作。

回答by krookedking

Here is what I would recommend : dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

这是我的建议: dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

回答by martin

Well,

好,

You could use a JFrame instead. JDialog is supposed to be used as popup of an application that runs in an JFrame to catch the users attention and to pause the main application. If the JFrame is closed, you can call System.exit(0)

您可以改用 JFrame。JDialog 应该用作在 JFrame 中运行的应用程序的弹出窗口,以吸引用户的注意力并暂停主应用程序。如果 JFrame 关闭,则可以调用 System.exit(0)

回答by Manoj

dialog has a getParent()method, which I guess, is set to null in your case here CustomUIPanel.createDialog(null,

对话框有一个getParent()方法,我猜在你的情况下设置为 nullCustomUIPanel.createDialog(null,

you can use that to exit conditionally.

您可以使用它来有条件地退出。