Close JFrame when I open another

0

I have a JFrame "1" with a button that opens another, when I press the button and the JFrame "2" opens, I want the JFrame "1" to close. I have tried to do it in the following ways:

But they have not worked for me.

    
asked by Melannie Nichole 21.09.2017 в 21:43
source

2 answers

1

If you want the JFrame to close when you open another one, then simply "drop it" with dispose ();

EXAMPLE

In JFrame1 in the event of your button, you would place something like this:

Ventana2 frame2 = new Ventana2();
dispose();

And in the JFrame2 ... in your exit event ....

dispose();
Ventana1 frame1 = new Ventana1();

And the case dies.

EYE: Make sure you have correctly set the setDefaultCloseOperation properties of the frame. In your case, it suits you that in the JFrame2 you set DISPOSE_ON_CLOSE .

Update:

I make it easier for you to understand with a simple example, we will create 2 JFrames.

The JFrame1 that will be "Window1":

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

/**
 *
 * @author Phillips
 */
public class Ventana1 extends JFrame{
    private JButton boton;
    public Ventana1(){
        super("Dumb Demo");
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        boton = new JButton("Abre otra ventana");
        boton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                Ventana2 obj = new Ventana2();
                dispose();
            }
        });
        add(boton);
        setVisible(true);
    }
}

The JFrame2 that will be "Ventana2":

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
 *
 * @author Phillips
 */
public class Ventana2 extends JFrame{
    private JButton boton;
    public Ventana2(){
        super("Segunda Ventana");
        setSize(300,300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        boton = new JButton("Soy el segundo boton de otra ventana");
        boton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                dispose();
                Ventana1 obj = new Ventana1();
            }
        });
        add(boton);
        setVisible(true);
    }
}

The main class that we always need as a launcher:

public class Demo {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Ventana1 prueba = new Ventana1();
}  
}

Result:

And if you press the button of the first window, the second one will appear

You can take the trouble if you want to look for the other window to check that everything is in place. (If you click on the second window button again, it will close and the first will appear)

    
answered by 21.09.2017 / 22:03
source
1

I have this line of code in a project and it helps me when I open a jframe the other one closes.

View obj = new View ();
        obj.setVisible (true);
        dispose ();

    
answered by 21.09.2017 в 21:52