0

So I've been learning Java for the very first time and it's time for me to attempt my first project. And I'm stuck at the "first hurdle" haha.

The issue I have is the fact that I don't actually know how to space J Items apart.

I have a 250,350 window for a Log In form with a JLabel, a JTextField for username and JLabel JPassword for Password with a JButton at the bottom.

What I want to do now is style it so that the spacing between the top and the bottom of the form makes it so that the form is centered as well as adding a line's height space between the JLabel and the JTextField. (Basically a \n type deal but that isn't working.)

Hopefully this makes sense, if not, I apologise and I'll try to rephrase/add code!

public Game() {
    this.setSize(250,350);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Sticket Cricket - Login");

    JPanel loginMenuPanel = new JPanel();

    loginButton = new JButton("Login");

    usernameField = new JTextField();
    usernameField.setColumns(10);

    passwordField = new JPasswordField();
    passwordField.setColumns(10);
    passwordField.requestFocus();

    usernameLabel = new JLabel("Username: ");

    passwordLabel = new JLabel("Password: ");

    this.add(loginMenuPanel);

    loginMenuPanel.add(usernameLabel);
    loginMenuPanel.add(usernameField);
    loginMenuPanel.add(passwordLabel);
    loginMenuPanel.add(passwordField);
    loginMenuPanel.add(loginButton);

    this.setVisible(true);
}
lunr
  • 5,159
  • 4
  • 31
  • 47
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space. 2) For better help sooner, post an [SSCCE](http://sscce.org/) of your current attempt. 3) *"Hopefully this makes sense, if not.."* ..post ASCII art of the GUI at minimum size and when expanded. – Andrew Thompson Jun 22 '13 at 23:54
  • You need to look at Layout-Managers, Java has a number of layout managers built in (see http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). I normally use an external layout manager (Table Layout - http://code.google.com/p/table-layout/). There are more recent layout managers like mig layout as well – Bruce Martin Jun 22 '13 at 23:56
  • 1
    `public Game() {..` For better help sooner, post an [SSCCE](http://sscce.org/) *rather than uncompilable code snippets.* – Andrew Thompson Jun 23 '13 at 00:01

1 Answers1

0

Short Answer:

Create a JPanel, set the layoutmanger of the panel (some examples, GridLayout, BorderLayout, Check out the tutorial here where more of these are explained)

Then add your components to this panel accordingly

For the layout you are looking for it would possibly be easier to use an IDE to create this, I find Net Beans to be the easiest for doing this.

My recommendation would be for you to create a JPanel with a grid layout of 2 columns and 2 rows, to this add you JLabels and Text fields for the logon name and password.

Then create another JPanel possibly BorderLayout or Flow Layout and add the above panel to this then add this parent panel to the frame.

Java Devil
  • 10,629
  • 7
  • 33
  • 48