javaswingauthenticationframes

Java swing two indipendent frames


I have a main frame of my application and i want to have another frame for login. So i have written two classes: a Main class and a LoginFrame class.

/*Imported classes*/

public class Main {

    private JFrame frame;
    private LoginFrame loginFrame;

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

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

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame("Main Frame");
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JPanel mainPanel = new JPanel();
        mainPanel.setBounds(10, 10, 426, 243);
        frame.getContentPane().add(mainPanel);
        mainPanel.setLayout(null);
        
        loginFrame = new LoginFrame("Login...");
        loginFrame.setVisible(true);
        loginFrame.pack();
        
    }
}
/*Imported classes*/
public class LoginFrame extends JFrame {

    private JFrame frame;
    private JTextField usernameField;
    private JPasswordField passwordField;
    
    /**
     * Launch the application.
     */
    /*public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginFrame window = new LoginFrame("Login...");
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }*/

    /**
     * Create the application.
     */
    public LoginFrame(String title) {
        
    

    /**
     * Initialize the contents of the frame.
     */

        frame = new JFrame(title);
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        JPanel panel = new JPanel();
        panel.setBounds(0, 10, 426, 243);
        frame.getContentPane().add(panel);
        panel.setLayout(null);
        
        JLabel userLabel = new JLabel("User:");
        userLabel.setFont(new Font("Dialog", Font.BOLD, 14));
        userLabel.setEnabled(true);
        userLabel.setBounds(41, 78, 45, 13);
        panel.add(userLabel);
        
        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        passwordLabel.setBounds(41, 124, 76, 13);
        panel.add(passwordLabel);
        
        usernameField = new JTextField();
        usernameField.setBounds(165, 75, 173, 19);
        panel.add(usernameField);
        usernameField.setColumns(10);
        
        passwordField = new JPasswordField();
        passwordField.setColumns(10);
        passwordField.setBounds(165, 123, 173, 19);
        panel.add(passwordField);
        
        JLabel loginLabel = new JLabel("Enter Username and Password");
        loginLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
        loginLabel.setBounds(79, 22, 259, 13);
        panel.add(loginLabel);
        
        JButton resetLoginButton = new JButton("Reset");
        resetLoginButton.setFont(new Font("Tahoma", Font.BOLD, 14));
        resetLoginButton.setBounds(318, 189, 85, 21);
        panel.add(resetLoginButton);
        
        JButton okLoginButton = new JButton("Ok");
        okLoginButton.setFont(new Font("Tahoma", Font.BOLD, 14));
        okLoginButton.setBounds(217, 189, 85, 21);
        panel.add(okLoginButton);
    }
    
}

what i get is the two frames:

https://i.sstatic.net/Fl2GO.png

When i open the login frame there is nothing (buttons and text fields).What is the problem? Is it a right way to implement a login frame?

I expect two different frame the main one empty. The second one with two fields one for username and the other with password and two buttons.


Solution

  • When i open the login frame there is nothing (buttons and text fields).

    Of course.

    What is the problem?

    You never add any components to your LoginFrame. You instead create a separate JFrame, add some components to that, and then do nothing further with it.