我正在开发一个具有多个 JFrame
和 JDialog
窗口的程序。
我有一个 JFrame,其中包含一个按钮,当我单击此按钮时,将打开一个 JDialog 窗口。在此 JDialog 窗口中还有另一个按钮,单击该按钮时会打开第二个 JDialog 窗口。在第二个 JDialog 窗口中,我有一个最后一个按钮。
我想要做的是在单击最后一个按钮时关闭 JDialog
窗口和 JFrame
窗口。
开仓订单是这样的:
JFrame Frame1;
JButton Button1;
JDialog Dialog1;
JButton Button2;
JDialog Dialog2;
JButton Button3;
Button1ActionPerformed(ActionEvent e){
new Dialog(Frame1Frame);
}
Button2ActionPerformed(ActionEvent e){
new Dialog2(Dialog1Frame)
}
Button3ActionPerformed(ActionEvent e){
//Here I wnat to add the code that closes JDialog2 JDialog1 and JFrame1 windows.
}
我已经尝试过 super.dispose();
但它不起作用。有什么想法吗?
最佳答案
如图here使用Action
,您的 actionPerformed()
实现可以将 WINDOW_CLOSING
事件分派(dispatch)到所需的 Window
实例。
@Override
public void actionPerformed(ActionEvent e) {
d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
d2.dispatchEvent(new WindowEvent(d2, WindowEvent.WINDOW_CLOSING));
f1.dispatchEvent(new WindowEvent(f1, WindowEvent.WINDOW_CLOSING));
}
关于java - 如何关闭多个JFrame和JDialog窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28008652/