So I'm creating an application. I want main to initialize a login window, which I've done. And for now I want the login window to close whenever I click on the button, and when it closes, a new window (called MainWindow) opens with different buttons/info/text.
All these classes are separate, but in the same package.
The problem is: The main window opens when I click login, however the login window doesn't terminate/close.
My main method, from which I call the login window:
import javax.swing.*;
public class Main {
    // Display Login Window
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new LoginWindow();
            LoginWindow.createAndShowLoginGUI();
        });
    }
}
My login window class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginWindow extends JFrame implements ActionListener {
    public void prepareLoginGUI() {
        // Frame with GridBagLayout
        JFrame loginFrame = new JFrame("Login Window");
        loginFrame.setSize(1200, 800);
        loginFrame.setLayout(new GridBagLayout());
        // Button for logging in, with respective GridBagConstraint
        // setFocusable is set to false to take out the border around the text
        JButton loginButton = new JButton("Login");
        loginButton.addActionListener(this::actionPerformed);
        loginButton.setActionCommand("Open");
        GridBagConstraints lButtonC = new GridBagConstraints();
        loginButton.setFocusable(false);
        // Username text-field and JLabel with respective GridBagConstraints
        JTextField tfUsername = new JTextField(15);
        GridBagConstraints tfUserC = new GridBagConstraints();
        JLabel txtUser = new JLabel("Username: ");
        GridBagConstraints txtUserC = new GridBagConstraints();
        // Password text-field and JLabel with respective GridBagConstraints
        JPasswordField tfPassword = new JPasswordField(15);
        GridBagConstraints tfPassC = new GridBagConstraints();
        JLabel txtPassword = new JLabel("Password: ");
        GridBagConstraints txtPassC = new GridBagConstraints();
        // Add all components to the JFrame
        // Making sure to add the text before the text-fields
        loginFrame.add(txtUser, txtUserC);
        loginFrame.add(tfUsername, tfUserC);
        loginFrame.add(txtPassword, txtPassC);
        loginFrame.add(tfPassword, tfPassC);
        loginFrame.add(loginButton, lButtonC);
        // Show and set close parameters
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.setVisible(true);
    }
    // Instructions for when the login button is clicked
    // Close this window, if button == open, which it does
    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals("Open")) {
            this.dispose();
            this.setVisible(false);
            new MainWindow();
            MainWindow.createAndShowMainWGUI();
        }
    }
    // Callable from Main class
    public static void createAndShowLoginGUI() {
        LoginWindow loginW = new LoginWindow();
        loginW.prepareLoginGUI();
    }
}
My Main Window Class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainWindow extends JFrame implements ActionListener {
    public void prepareMainWGUI(){
        // Frame with GridBagLayout
        JFrame loginFrame = new JFrame("Main Window");
        loginFrame.setSize(1200, 800);
        loginFrame.setLayout(new GridBagLayout());
        // Username text-field and JLabel with respective GridBagConstraints
        JLabel txtUser = new JLabel("It worked!");
        GridBagConstraints txtUserC = new GridBagConstraints();
        loginFrame.add(txtUser, txtUserC);
        loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        loginFrame.setVisible(true);
    }
    // Callable from Main class
    public static void createAndShowMainWGUI() {
        MainWindow mainWind = new MainWindow();
        mainWind.prepareMainWGUI();
    }
}
If you use this code, when the main window opens, the login window will be right behind it.
 
    