简体   繁体   中英

How to close multiple JFrame and JDialog windows?

I'm working on a program which has multiple JFrame and JDialog windows.

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.

What I want to do is to close both JDialog windows and JFrame window when this last button is clicked.

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(); 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:

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); :

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.

@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-

It is possible to close multiple windows, even non-static Java swing windows. 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.

All Methods will be typed in only Class1.

Make a second class global variable in the first class.

` Class2 NiceApp1;

//The button name will be 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) {

    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.

}

//Now you will need to use mouse exit click on the same jToggleButton1 select //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.

private void 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.

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);

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.

You can also visit 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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