简体   繁体   English

如何关闭多个 JFrame 和 JDialog 窗口?

[英]How to close multiple JFrame and JDialog windows?

I'm working on a program which has multiple JFrame and JDialog windows.我正在开发一个具有多个JFrameJDialog窗口的程序。

I have a JFrame which contains a button, when I click on this button a JDialog window opens up.我有一个包含按钮的 JFrame,当我单击此按钮时,会打开一个 JDialog 窗口。 In this JDialog windows there is another button, which when is clicked it opens up a second JDialog window.在此 JDialog 窗口中还有另一个按钮,单击该按钮会打开第二个 JDialog 窗口。 In the second JDialog window I have a last button.在第二个 JDialog 窗口中,我有一个最后一个按钮。

What I want to do is to close both JDialog windows and JFrame window when this last button is clicked.我想要做的是在单击最后一个按钮时关闭JDialog窗口和JFrame窗口。

This is how the opening order is:开仓顺序是这样的:

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.
}

I have tried super.dispose();我试过super.dispose(); but it doesn't work.但它不起作用。 Any ideas?有任何想法吗?

There may be better ways of doing this, but here is one general approach that might help. 也许有更好的方法可以做到这一点,但这是一种可能有用的通用方法。

In your code you create the windows but you do not store the reference to the windows you created into a variable. 在代码中,您创建了窗口,但没有将对创建的窗口的引用存储到变量中。 For example, you have: 例如,您有:

JDialog Dialog1;

Then later, when you create the instance of Dialog1, you have this code: 然后,当您创建Dialog1的实例时,您将具有以下代码:

Button1ActionPerformed(ActionEvent e){
    new Dialog(Frame1Frame);
}

This means you have created the Dialog, but you have not retained a reference to the Dialog for later manipulation by your code. 这意味着您已经创建了对话框,但是尚未保留对对话框的引用以供以后的代码操作。 If you assign this value here, you should be able to manipulate it later. 如果在此处分配此值,则以后应该可以对其进行操作。

If you change your implementation to: 如果将实现更改为:

Button1ActionPerformed(ActionEvent e){
    Dialog1 = new Dialog(Frame1Frame);
}

Then later in your code you will have a reference to the Dialog in order to manipulate it, 然后在您的代码的后面,您将引用该对话框以进行操作,

Button3ActionPerformed(ActionEvent e){
   Dialog1.dispose();
   // you can manipulate the variables of the class from here and close other windows etc.
}

If you have the objects reference, you can do: 如果有对象引用,则可以执行以下操作:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;


public class Main
{
    private static JFrame frame;

    private static JButton buttonFrame;


    private static JDialog dialog1;

    private static JButton buttonDialog1;


    private static JDialog dialog2;

    private static JButton buttonDialog2;


    public static void main(String[] args) {

        /* frame */

        frame = new JFrame("Main Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);

        buttonFrame = new JButton("open dialog 1");
        buttonFrame.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                dialog1.setVisible(true);
            }
        });

        frame.add(buttonFrame);

        /* dialog 1 */

        dialog1 = new JDialog(frame, "Dialog 1");
        dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog1.setSize(300, 300);
        dialog1.setLocationRelativeTo(null);

        buttonDialog1 = new JButton("open dialog 2");
        buttonDialog1.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                dialog2.setVisible(true);
            }
        });

        dialog1.add(buttonDialog1);

        /* dialog 2 */

        dialog2 = new JDialog(dialog1, "Dialog 2");
        dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog2.setSize(200, 200);
        dialog2.setLocationRelativeTo(null);

        buttonDialog2 = new JButton("close all");
        buttonDialog2.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                dialog2.dispose();
                dialog1.dispose();
                frame.dispose();
            }
        });

        dialog2.add(buttonDialog2);

        /* show frame */

        frame.setVisible(true);
    }
}

Otherwise you can use System.exit(0); 否则,您可以使用System.exit(0); :

buttonDialog2.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
});

As shown here using Action , your actionPerformed() implementation can dispatch the WINDOW_CLOSING event to the desired Window instances. 如此处所示使用Action ,您的actionPerformed()实现可以将WINDOW_CLOSING事件调度到所需的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));
}

Closing multiple JFrame-关闭多个 JFrame-

It is possible to close multiple windows, even non-static Java swing windows.可以关闭多个窗口,甚至是非静态 Java 摆动窗口。 Type below small code, step by step.输入下面的小代码,一步一步。 Only a popup will come which will tell, open class2 again, only in case if the cursor moves to the class1.只有一个弹出窗口会告诉,再次打开 class2,只有在光标移动到 class1 的情况下。

All Methods will be typed in only Class1.所有方法将仅在 Class1 中键入。

Make a second class global variable in the first class.在第一类中创建第二类全局变量。

` Class2 NiceApp1; ` Class2 NiceApp1;

//The button name will be jToggleButton1. //按钮名称将为 jToggleButton1。 You need to clockwise click on the button and select action performs and then type to open the second class.您需要顺时针单击按钮并选择操作执行然后键入以打开第二个类。

private void jToggleButton1 ActionPerformed(java.awt.event.ActionEvent evt) {私人无效jToggleButton1 ActionPerformed(java.awt.event.ActionEvent evt){

    NiceApp1 = new Class2();
    NiceApp1.setVisible(true);

//JOptionPane.showMessageDialog(null, "Test");//Use this popup in the case doesn't work and after using again make comment only this popup. //JOptionPane.showMessageDialog(null, "Test");//在这种情况下使用此弹窗不起作用,再次使用后仅注释此弹窗。

} }

//Now you will need to use mouse exit click on the same jToggleButton1 select //MouseExited. //现在您将需要使用鼠标退出单击同一个 jToggleButton1 选择 //​​MouseExited。

private void jToggleButton1MouseExited(java.awt.event.MouseEvent evt) {                                           
    
    
    JOptionPane.showMessageDialog(null, "Open class2button again");

    CloseClass1();
 
    if(NiceApp1 instanceof Class2){
    CloseClass2();

    }

    Class1 nice = new Class1();
    nice.setVisible(true);


}                                          

//Now you will need to use focus lost click on the same jToggleButton1 select FocusLost. //现在您将需要使用焦点丢失单击相同的jToggleButton1选择FocusLost。

private void jToggleButton1FocusLost(java.awt.event.FocusEvent evt) {私人无效jToggleButton1FocusLost(java.awt.event.FocusEvent evt){

JOptionPane.showMessageDialog(null, "Affable to use Nice Application 1");

}                                        

//Now only type in your class Class1 and Class2 close method. //现在只输入你的类 Class1 和 Class2 关闭方法。

public void CloseClass1() {
    WindowEvent winclosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);

}

public void CloseClass2() {
WindowEvent winclosing = new WindowEvent(NiceApp1,WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);

}

` `

Note, if you didn't set Class2 location in the center so you need to locate your class center type in Class2-this.setLocationRelativeTo(null);请注意,如果您没有在中心设置 Class2 位置,则需要在 Class2-this.setLocationRelativeTo(null); 中找到您的班级中心类型;

You have done now if you did the above process.如果你做了上面的过程,你现在就完成了。

If you can't click the button clockwise to select action performed or etc. so use like jToggleButton1.addActionListener.如果你不能顺时针点击按钮来选择执行的动作等,那么使用 jToggleButton1.addActionListener。

You can also visit NiceApplication1.BlogSpot.Com.您也可以访问 NiceApplication1.BlogSpot.Com。

You can use the above code.您可以使用上面的代码。 From Class2 close window so you will see your previous window also will close and a new fresh class1 window will be visible, only use the second class smaller than the first class.从 Class2 关闭窗口,您将看到之前的窗口也将关闭,并且会看到一个新的新 class1 窗口,仅使用比第一个类小的第二个类。

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

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