Search code examples
javaeclipsewindowgraphic

Window always opens blank even though code is written


I am making a program which will open a class GUI called "Menu" when correct login info is put into a text field password field and a button called "login" is pressed. The problem is that the Menu window will open blank, not displaying what I have programmed into the Menu.

This is my code for Login class,

package ems;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JFrame implements ActionListener{

    private JFrame loginFrame;
    private JTextField usernameField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    window.loginFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Login() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        loginFrame = new JFrame();
        loginFrame.setTitle("Login");
        loginFrame.setBounds(100, 100, 450, 300);
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.getContentPane().setLayout(null);

        JLabel lblUsername = new JLabel("Username");
        lblUsername.setBounds(112, 116, 74, 16);
        loginFrame.getContentPane().add(lblUsername);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(112, 165, 74, 16);
        loginFrame.getContentPane().add(lblPassword);

        usernameField = new JTextField();
        usernameField.setBounds(198, 110, 134, 28);
        loginFrame.getContentPane().add(usernameField);
        usernameField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(198, 159, 134, 28);
        loginFrame.getContentPane().add(passwordField);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String uname = usernameField.getText();
                String pword = passwordField.getText();

                if (uname.equals("test") && pword.equals("test")){
                    JOptionPane.showMessageDialog(loginFrame, "Login successful.");
                    loginFrame.setVisible(false);
                    Menu menu = new Menu ();
                    menu.setVisible (true);

                }else{
                    JOptionPane.showMessageDialog(loginFrame, "Login unsuccessful.");
                }
            }
        });
        btnLogin.setBounds(238, 210, 90, 30);
        loginFrame.getContentPane().add(btnLogin);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(108, 210, 90, 30);
        loginFrame.getContentPane().add(btnExit);

        JPanel panel = new JPanel();
        panel.setBackground(new Color(192, 192, 192));
        panel.setBounds(91, 86, 258, 163);
        loginFrame.getContentPane().add(panel);

        JLabel lblLoginToThe = new JLabel("LOGIN TO THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
        lblLoginToThe.setBounds(26, 23, 418, 16);
        loginFrame.getContentPane().add(lblLoginToThe);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.setBounds(0, 0, 450, 63);
        loginFrame.getContentPane().add(panel_1);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

This is the code for the Menu class,

package ems;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import ems.Login;

public class Menu extends Login implements ActionListener{

    private JFrame menuFrame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Menu window = new Menu();
                    window.menuFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Menu() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        menuFrame = new JFrame();
        menuFrame.setTitle("Menu");
        menuFrame.setBounds(100, 100, 450, 300);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuFrame.getContentPane().setLayout(null);

        JLabel lblLoginToThe = new JLabel("THE ELECTRICITY MONITORING SYSTEM");
        lblLoginToThe.setForeground(new Color(255, 255, 255));
        lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
        lblLoginToThe.setBounds(64, 22, 331, 16);
        menuFrame.getContentPane().add(lblLoginToThe);

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(new Color(46, 139, 87));
        panel_1.setBounds(0, 0, 450, 63);
        menuFrame.getContentPane().add(panel_1);

        JButton btnLogout = new JButton("Logout");
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                menuFrame.setVisible(false);
                Login login = new Login ();
                login.setVisible(true);
            }
        });
        btnLogout.setBounds(307, 222, 117, 29);
                menuFrame.getContentPane().add(btnLogout);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnExit.setBounds(10, 222, 117, 29);
        menuFrame.getContentPane().add(btnExit);



    }
}

Both of these classes are under a package called ems. I am very new to programming and help would be greatly appreciated. Thank you


Solution

  • You are creating another JFrame inside of Login and Menu, which is wrong, theres also no need that Menu extends Login

    Here is the fixed version for Login:

    package ems;
    
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Login extends JFrame {
        private static final long serialVersionUID = 1L;
    
        private JTextField usernameField;
        private JPasswordField passwordField;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Login window = new Login();
                        window.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public Login() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            this.setTitle("Login");
            this.setBounds(100, 100, 450, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.getContentPane().setLayout(null);
    
            JLabel lblUsername = new JLabel("Username");
            lblUsername.setBounds(112, 116, 74, 16);
            this.getContentPane().add(lblUsername);
    
            JLabel lblPassword = new JLabel("Password");
            lblPassword.setBounds(112, 165, 74, 16);
            this.getContentPane().add(lblPassword);
    
            usernameField = new JTextField();
            usernameField.setBounds(198, 110, 134, 28);
            this.getContentPane().add(usernameField);
            usernameField.setColumns(10);
    
            passwordField = new JPasswordField();
            passwordField.setBounds(198, 159, 134, 28);
            this.getContentPane().add(passwordField);
    
            JButton btnLogin = new JButton("Login");
            btnLogin.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String uname = usernameField.getText();
                    String pword = passwordField.getText();
    
                    if (uname.equals("test") && pword.equals("test")){
                        JOptionPane.showMessageDialog(Login.this, "Login successful.");
                        Login.this.setVisible(false);
                        Menu menu = new Menu ();
                        menu.setVisible (true);
    
                    }else{
                        JOptionPane.showMessageDialog(Login.this, "Login unsuccessful.");
                    }
                }
            });
            btnLogin.setBounds(238, 210, 90, 30);
            this.getContentPane().add(btnLogin);
    
            JButton btnExit = new JButton("Exit");
            btnExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            btnExit.setBounds(108, 210, 90, 30);
            this.getContentPane().add(btnExit);
    
            JPanel panel = new JPanel();
            panel.setBackground(new Color(192, 192, 192));
            panel.setBounds(91, 86, 258, 163);
            this.getContentPane().add(panel);
    
            JLabel lblLoginToThe = new JLabel("LOGIN TO THE ELECTRICITY MONITORING SYSTEM");
            lblLoginToThe.setForeground(new Color(255, 255, 255));
            lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
            lblLoginToThe.setBounds(26, 23, 418, 16);
            this.getContentPane().add(lblLoginToThe);
    
            JPanel panel_1 = new JPanel();
            panel_1.setBackground(new Color(46, 139, 87));
            panel_1.setBounds(0, 0, 450, 63);
            this.getContentPane().add(panel_1);
        }
    }
    

    And for Menu

    package ems;
    
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Font;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Menu extends JFrame{
        private static final long serialVersionUID = 1L;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Menu window = new Menu();
                        window.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public Menu() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            this.setTitle("Menu");
            this.setBounds(100, 100, 450, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.getContentPane().setLayout(null);
    
            JLabel lblLoginToThe = new JLabel("THE ELECTRICITY MONITORING SYSTEM");
            lblLoginToThe.setForeground(new Color(255, 255, 255));
            lblLoginToThe.setFont(new Font("Arial", Font.BOLD, 16));
            lblLoginToThe.setBounds(64, 22, 331, 16);
            this.getContentPane().add(lblLoginToThe);
    
            JPanel panel_1 = new JPanel();
            panel_1.setBackground(new Color(46, 139, 87));
            panel_1.setBounds(0, 0, 450, 63);
            this.getContentPane().add(panel_1);
    
            JButton btnLogout = new JButton("Logout");
            btnLogout.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Menu.this.setVisible(false);
                    Login login = new Login ();
                    login.setVisible(true);
                }
            });
            btnLogout.setBounds(307, 222, 117, 29);
            this.getContentPane().add(btnLogout);
    
            JButton btnExit = new JButton("Exit");
            btnExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
            btnExit.setBounds(10, 222, 117, 29);
            this.getContentPane().add(btnExit);
        }
    }