在 java/swing 中关闭窗口时采取的正确操作是什么? [英] What is the right action to take upon closing windows in java/swing?

查看:22
本文介绍了在 java/swing 中关闭窗口时采取的正确操作是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just wrote this test code in my CustomUIPanel class:

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);
        }
    });
}

如果 CustomUIPanel.main() 是程序的入口点,它可以正常工作,但它让我想知道:如果另一个名为 CustomUIPanel.main() 的类用于测试?然后我对 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.

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

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

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

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?

推荐答案

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

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

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

另见 12.8 程序退出.

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

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

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();
            }
        });
    }
}

这篇关于在 java/swing 中关闭窗口时采取的正确操作是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆