不幸的是,看起来这个最近封闭的问题还不太清楚.这是典型的输出:
run:
Trying to Remove JDialog
Remove Cycle Done :-)
Checking if still exists any of TopLayoutContainers
JFrame
JDialog
Will Try Remove Dialog again, CycleNo. 1
-----------------------------------------------------------
Trying to Remove JDialog
Remove Cycle Done :-)
Checking if still exists any of TopLayoutContainers
JFrame
JDialog
Will Try Remove Dialog again, CycleNo. 2
-----------------------------------------------------------
Trying to Remove JDialog
Remove Cycle Done :-)
Checking if still exists any of TopLayoutContainers
JFrame
JDialog
Will Try Remove Dialog again, CycleNo. 3
-----------------------------------------------------------
Trying …Run Code Online (Sandbox Code Playgroud) 我刚在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);
}
});
}
Run Code Online (Sandbox Code Playgroud)
它是正确的,如果CustomUIPanel.main()是程序的入口点,但它让我想知道:如果另一个类要求CustomUIPanel.main()测试怎么办?然后我的电话System.exit(0)是不正确的.
如果没有顶级窗口,有没有办法告诉Swing事件调度线程自动退出?
如果不是,如果目标是让所有顶级窗口关闭时程序退出,那么JDialog/JFrame在关闭时做什么是正确的?
使用这样的代码处理JFrame的好方法是什么?我想处理Window出口和窗口关闭.
我知道我们不应该使用System.exit();
public class JavaCellularAutomataSquare {
public static final String TITLE = "Cellular Automata - Squaring Example";
private int maxWidth = 600;
private int maxHeight = 600;
public void launch() {
final JFrame frame = new JFrame(TITLE);
frame.setLocation(20, 20);
frame.setPreferredSize(new Dimension(maxWidth, maxHeight));
frame.setResizable(false);
frame.setFocusable(true);
final JPanel panel = new JPanel();
panel.setLocation(20, 20);
panel.setVisible(true);
panel.setPreferredSize(new Dimension(maxWidth, maxHeight));
panel.setFocusable(true);
panel.setBackground(Color.white);
// Panel setup, toggle visibility on frame
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)