如何关闭多个JFrame和JDialog窗口? [英] How to close multiple JFrame and JDialog windows?

查看:165
本文介绍了如何关闭多个JFrame和JDialog窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有多个JFrameJDialog窗口的程序.

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?

推荐答案

此处所示,使用

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窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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