问题描述
我正在开发一个具有多个JFrame
和JDialog
窗口的程序.
I'm working on a program which has multiple JFrame
and JDialog
windows.
我有一个包含一个按钮的JFrame,当我单击该按钮时,将打开一个JDialog窗口.在此JDialog窗口中,还有另一个按钮,单击该按钮将打开第二个JDialog窗口.在第二个JDialog窗口中,我有一个最后一个按钮.
I have a JFrame which contains a button, when I click on this button a JDialog window opens up. In this JDialog windows there is another button, which when is clicked it opens up a second JDialog window. In the second JDialog window I have a last button.
我想做的是,当单击最后一个按钮时,同时关闭JDialog
窗口和JFrame
窗口.
What I want to do is to close both JDialog
windows and JFrame
window when this last button is clicked.
打开订单的方式如下:
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();
,但是它不起作用.有什么想法吗?
I have tried super.dispose();
but it doesn't work. Any ideas?
推荐答案
如此处所示,使用 Action
,您的actionPerformed()
实现可以将WINDOW_CLOSING
事件调度到所需的Window
实例.
As shown here using Action
, your actionPerformed()
implementation can dispatch the WINDOW_CLOSING
event to the desired Window
instances.
@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));
}
这篇关于如何关闭多个JFrame和JDialog窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!