简体   繁体   English

没有更多窗口打开时结束Java AWT线程

[英]Ending the Java AWT thread when there are no more windows open

I have an ArrayList of JFrames and I want to be able to detect when all the windows are closed, and thus end the program. 我有一个JFrames的ArrayList,我希望能够检测到所有窗口何时关闭,从而结束程序。

So far every time I make a JFrame, I add it to the Array List. 到目前为止,每次创建JFrame时,我都会将其添加到“数组列表”中。 I also implemented a WindowListener, and remove that JFrame from the List whenever windowClosing() is called. 我还实现了一个WindowListener,并在每次调用windowClosing()时从列表中删除该JFrame。

My problem, however, is that the program does not end when the List is empty. 但是,我的问题是,列表为空时程序不会结束。 Usually, I use frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), but now I have multiple windows and that won't work. 通常,我使用frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE),但是现在我有多个窗口,将无法使用。 Should I just manually call System.Exit(0); 我应该手动调用System.Exit(0);吗? or is there a more preferred way of terminating the program. 还是有更优选的终止程序的方式。

Thanks 谢谢

Sample 样品

public class MainFrame extends JFrame{

private static final ArrayList<MainFrame> frameList = new ArrayList<MainFrame>();

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

public MainFrame(){     
    addWindowListener(new Listener());
    setSize(800, 600);
    setVisible(true);
}

class Listener implements WindowListener{
    public void windowClosing(WindowEvent arg0) {
        frameList.remove(MainFrame.this);
        if(frameList.size() == 0){
             //End Program
        }
    }
}

public static void newFrame() {
    frameList.add(new MainFrame());
}   

} }

Basically if you change defaultCloseOperation to DISPOSE_ON_CLOSE , this will cause the window to release it's reference to the native peer when it's closed and when all the peers are free, the JVM will exit automatically 基本上,如果将defaultCloseOperation更改为DISPOSE_ON_CLOSE ,这将导致窗口在关闭时释放对本地对等体的引用,并且当所有对等体都空闲时,JVM将自动退出

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainFrame extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                newFrame();
                newFrame();
                newFrame();
            }
        });
    }

    public MainFrame() {
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setSize(800, 600);
        setVisible(true);
    }

    public static void newFrame() {
        new MainFrame();
    }

}

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

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