Results 1 to 3 of 3
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Nothing in JFrame

    I have just made a JFrame using this code but only the JFrame appears, not the text fields or the button. Why?

    Java Code:
    import java.awt.GridLayout;
    import javax.swing.*;
    
    
    public class LoginScreen extends JFrame {
       
        
        public LoginScreen(){
        
            setLayout(new GridLayout(3, 1));
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 250);
            
            JTextField usernameField = new JTextField("Username");
            JPasswordField passwordField = new JPasswordField("Password");
            JButton enterButton = new JButton("Submit");
            
        
            add(usernameField);
            add(passwordField);
            add(enterButton);
            
    }
        
        
    }
    I initialise it using

    Java Code:
    LoginScreen loginScreen = new LoginScreen();

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Nothing in JFrame

    Set it visible only after all content has been added. Oh, and all Swing constructors and methods should be invoked on the EDT. Go through this tutorial for a better understanding: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Nothing in JFrame

    Set visible only after it had all been added and it worked, thanks!

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 08:29 AM
  2. Transferring information from one JFrame to Another JFrame
    By cherylminor85 in forum AWT / Swing
    Replies: 4
    Last Post: 05-24-2012, 06:24 AM
  3. Replies: 3
    Last Post: 11-27-2011, 06:18 AM
  4. Replies: 5
    Last Post: 06-14-2010, 07:10 PM
  5. Replies: 4
    Last Post: 04-23-2009, 12:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •