Java swing two indipendent frames

97 Views Asked by At

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.stack.imgur.com/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.

3

There are 3 best solutions below

3
John Bollinger On BEST ANSWER

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.

1
camickr On
loginFrame.pack();

The pack() statement will cause the frame to be displayed at its preferred size.

Since you are using a null layout the frame does NOT have a preferred size.

The solution is to NOT use a null layout. Swing was designed to be used with layout managers.

Read the Swing tutorial on Layout Managers

Also, note that the content pane of a frame is a JPanel, so there is no need for you to create a separate panel to add to the frame.

Read the tutorial for Swing basics.

0
Grinding For Reputation On

Credit to John Bollinger on the original answer.

What you're doing wrong is adding the components on a frame inside the LoginFrame class. What am I yapping about? well look here

JPanel panel = new JPanel();
panel.setBounds(0, 10, 426, 243);
frame.getContentPane().add(panel);
panel.setLayout(null);

What are you adding your panel to? Well JFrame frame inside your LoginFrame class!

Now I want you to look here

loginFrame = new LoginFrame("Login...");
loginFrame.setVisible(true);
loginFrame.pack();

What are you making visible? The login frame!

The second mistake you are making is using loginFrame.pack() but read camickr's answer to know more.

How can you fix this? You have two easy options.

Option A. Create a getter for the frame in LoginFrame

Well this is quite straight foward and it works, create a getter for the frame and instead of using loginFrame.setVisible(true); use loginFrame.getFrame().setVisible(true);

Option B. Delete the frame

Just delete the frame object. Replace anything like frame.getContentPane().add(panel); with getContentPane().add(panel); this will use this.getContentPane()... which means that you use the LoginFrame instead.

And most importantly remove the pack() line!

enter image description here

This should help you and you're gonna get something like this ;) I believe that's what you wanted.