0

First time using Swing to create a GUI and unsure why I am only getting a grey 1280 x 720 window when running the code and nothing else.

There are no error messages. If anyone could assist it would be very helpful.

public void createGUI(){
    window = new JFrame();
    window.setSize(1280, 720);
    window.setTitle("Prototype");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setBackground(Color.GRAY);
    window.setLayout(null);
    window.setVisible(true);

    loginScreen = new JPanel();
    loginScreen.setBackground(Color.GRAY);
    loginScreen.setForeground(Color.WHITE);

    userLabel = new JLabel("Username:");
    userLabel.setBounds(10, 20, 80, 25);
    loginScreen.add(userLabel);

    userText = new JTextField(20);
    userText.setBounds(100, 20, 165, 25);
    loginScreen.add(userText);

    passLabel = new JLabel("Password");
    passLabel.setBounds(10, 50, 80, 25);
    loginScreen.add(passLabel);

    passwordText = new JPasswordField();
    passwordText.setBounds(100, 50, 165, 25);
    loginScreen.add(passwordText);

    correctLogin = new JLabel("");
    correctLogin.setBounds(10, 110, 300, 25);
    loginScreen.add(correctLogin);

    logInButton = new JButton("Login");
    logInButton.setBounds(10, 80, 80, 25);
    loginScreen.add(logInButton);

    window.add(loginScreen);
    loginScreen.setVisible(true);
}
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
  • 2
    1. Call `window.setVisible(true);` ***after*** adding all components to the GUI. 2. Do *not* use null layouts and `setBounds` but instead learn and use the layout managers. 3. Improve your question title as it should be informative and summarize your actual problem. Check out the [ask] link as well as Jon Skeet's ["Stack Overflow question checklist"](http://meta.stackoverflow.com/q/260648) which can be useful when you want to write a good and well-received question. – Hovercraft Full Of Eels Aug 22 '21 at 17:18

0 Answers0