-1

I have a problem with JFrame. All I want to do is to create a JFrame for Login with a Button, and when the Button is pressed: it close the Login Frame and opens the Program Frame.

This is my Login Frame:

public static void main(String[] args) {

    JFrame frame = new JFrame("My Program");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       LoginPanel primary = new LoginPanel(frame);

        frame.setPreferredSize (new Dimension (650, 500));
        frame.getContentPane().add(primary);
        frame.pack();
        frame.setVisible(true);

}

Which opens the Login Panel by passing the Frame in the constructor, the Login Panel:

public class LoginPanel extends JPanel {

JFrame fr;

class submitButton implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        ProgramFrame programFrame = new ProgramFrame();
        programFrame.setVisible(true);
        fr.setVisible(false);
        fr.dispose();
    }
  }

public LoginPanel(JFrame frame) {

    fr = frame;

    JButton submit = new JButton("Button Login");
    submit.addActionListener(new submitButton());
    add(submit);    
}

This is the problem:

When I click on the Button "Button Login" of the LoginPanel, it succesfully opens the new ProgramFrame but it doesn't close at all the old frame (LoginFrame). The LoginFrame becomes smaller, very little, but remains:

enter image description here

Thanks in advance for the help! :)

Simone C.
  • 369
  • 4
  • 14
  • I recommend using only 1 frame, using `CardLayout` to switch between different panels. You should also be posting your swing code to the EDT using `EventQueue.invokeLater`. Could you show us your `ProgramFrame` class? – Vince Apr 19 '16 at 17:20
  • I almost gave you a +1 for using Ubuntu ;) – Deval Khandelwal Apr 19 '16 at 17:36
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Apr 20 '16 at 04:10

2 Answers2

2
class submitButton implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        ProgramFrame programFrame = new ProgramFrame();
        programFrame.setVisible(true);
        this.dispose(); //changed line
    }
  }

well your panel is closed bt the jframe is still opened without the jpanel i have made some changes to your code now both will disposed at the same time

Priyamal
  • 2,919
  • 2
  • 25
  • 52
0

You first initialise JFrame completely, so after inside JButton click event first hide JFrame later dispose it.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class HideLoginPage{

    public static void main(String[] args){
        HideLoginPage loginPage = new HideLoginPage();

        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setBounds(200, 200, 200, 100);
        loginPage.setPane(frame);
        frame.setVisible(true);
    }

    public void setPane(final JFrame frame){
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        JButton submit = new JButton("Login");
        submit.setSize(100, 30);
        panel.add(submit);

        submit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                JFrame newFrame = new JFrame();
                newFrame.setBounds(400, 200, 400, 400);
                newFrame.setVisible(true);

                frame.setVisible(false);
                frame.dispose();
            }
        });

        frame.getContentPane().add(panel);
    }
}
ArifMustafa
  • 4,617
  • 5
  • 40
  • 48