How to close multiple JFrame and JDialog windows?

2019-09-09 20:21发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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