1

This is my first time learning Java Swing and I had a label and setLocation wasn't working. Someone told me it's because you have to set the layout to null, otherwise they're set to default locations and sizes. So I did, and now my label isn't appearing

import java.util.*;
import javax.swing.*; 
public class School {
    private ArrayList <String> Usernames = new ArrayList <> ();
    private ArrayList <String> Passwords = new ArrayList <> ();
    public void registerUser(){
        JFrame Register = new JFrame("Register");  
        JLabel Username = new JLabel("Username: ");
        Username.setBounds(50, 50, 100, 30);
        Register.add(Username);
        Register.setVisible(true);
        Register.setSize(500, 500);
        Register.setLayout(null);
        Register.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main (String [] args){
        School example = new School();
        example.registerUser();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JavaNovice
  • 13
  • 2
  • 3
    If you are new to Java Swing and you are struggling with layouts, you should have a look through the Oracle Tutorial [Layout Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html). This should give you a good overview of what the do's and don'ts when working with swing layouts are. Also, using `null` layout is usually not a good idea, see [Why is it frowned upon to use a null layout in Swing?](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – maloomeister Jun 01 '21 at 14:13
  • 3
    Also for future reference, `setVisible()` on the `JFrame` should be the last thing you should be doing when building your GUI. As changes done after calling `setVisible(true)` will not be immediately updated on the GUI, if you don't tell Swing to do so. (This is most likely your main issue with your current code, since you only `setSize()` *after* you made the frame visible). – maloomeister Jun 01 '21 at 14:15
  • 1
    Besides the above points, Java Swing is not thread safe. You should be making all calls to Swing components on the Event Dispatch Thread. This is rather advanced for a newbie but it's something you should start thinking about: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – markspace Jun 01 '21 at 14:28
  • 1
    Variable names should NOT start with an upper case character. Learn by example. All the tutorial links you see from above will follow the Java conventions. – camickr Jun 01 '21 at 14:52

1 Answers1

1

Here's a simple example where I corrected a few issues.

  1. I'm using the default layouts, not null. The defaults work well if you learn how to use them.

  2. I use a JPanel instead of adding components directly to the JFrame. JFrames actually use a rather confusing layout, it's best to just put stuff in a panel which makes the layout more intuitive.

  3. I'm using vertical boxes and horizontal boxes and nesting them (putting one inside the other). When I first started this was an easy way to make simple formatted layouts.

  4. I put the labels and text fields in a loop so you could see how to make several components in a loop and still lay them out.

  5. I changed several of your variable names to conform to the Java coding conventions (use lower case for local variables and fields).

  6. I added a more conventional sequence for displaying a window for the first time.

  7. I also kicked off your Swing code on the Event Dispatch Thread. You should do this for all Swing code.

Lightly tested:

package stackoverflow;

import java.util.*;
import javax.swing.*;


public class BasicWindow {

   private ArrayList<String> userNames = new ArrayList<>();
   private ArrayList<String> passwords = new ArrayList<>();

   public void registerUser() {
      JFrame register = new JFrame( "Register" );
      JPanel panel = new JPanel();
      Box vbox = Box.createVerticalBox();
      
      for( int i = 0; i < 4; i++ ) {
         Box hbox = Box.createHorizontalBox();
         JLabel username = new JLabel( "Username: " );
         hbox.add( username );
         JTextField input = new JTextField( 25 );
         hbox.add( input );
         vbox.add( hbox );
      }
      panel.add( vbox );
      register.add( panel );
      register.setSize( 500, 500 );
      register.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      register.setLocationRelativeTo( null );  // center on screen
      register.setVisible( true );
   }

   public static void main( String[] args ) {
      SwingUtilities.invokeLater( new Runnable() {
         @Override
         public void run() {
            BasicWindow example = new BasicWindow();
            example.registerUser();
         }
      });
   }
}
markspace
  • 10,621
  • 3
  • 25
  • 39