0

So I have aligned everything in my program using the BoxLayout, but when I run the GUI the components fill the whole screen and are not centrally aligned. I was just wondering how I could change the size of them all and align everything centrally

import javax.swing.*;

public class NewMain {

    JFrame window = new JFrame();
    JPanel login = new JPanel();
    JTextField username = new JTextField("Username", 15);
    JTextField password = new JTextField("Password", 15);
    JButton sendLogin = new JButton("Login");
    JLabel data = new JLabel("aaaaaaaaaaaaaaaa");

    public NewMain() {
        login.setLayout(new BoxLayout(login, BoxLayout.PAGE_AXIS));
        login.add(username);
        login.add(password);
        login.add(sendLogin);
        login.add(data);
        window.add(login);
        window.setVisible(true);
        window.setSize(800, 500);
        window.setLocationRelativeTo(null);
        window.setResizable(false);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle("Title");
    }

    public static void main(String[] args) {
        new NewMain();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MaxCulley
  • 23
  • 5
  • 1
    Ugh.. `window.setVisible(true);` ..should, almost without fail, be last. `window.setSize(800, 500);` ..this is no better than a guess. Instead `pack()` the GUI to the correct size ..`window.setLocationRelativeTo(null);` this should be done **after** pack is called, making a window non resizable.. and therefore also after `window.setResizable(false); .. which changes the frame borders and thereby the frame size. `window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` This is generally not what a login should do. Once this is closed, it will end the JVM. In fact, a modal dialog is best for .. – Andrew Thompson Jun 16 '20 at 12:09
  • 1
    .. a login since any code statements after it is set visible, will only be acted on once it is closed. `JTextField password = new JTextField("Password", 15);` Use a `JPasswordField` this is exactly what it is designed for! **General tip** Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. – Andrew Thompson Jun 16 '20 at 12:12
  • 1
    BTW - since I spent so much time saying how the code should do things differently, I'll take a moment to point out some things done **right** `JFrame window = new JFrame();` Nice one. Many people would extend a frame here, and it is completely unnecessary. `JTextField username = new JTextField("Username", 15);` Yep. By specifying a number of columns, the layout manager knows how wide to make the field. Or at least how wide it 'prefers' to be - which will sometimes be stretched by the layout. It also provides useful information when the GUI is being packed. – Andrew Thompson Jun 16 '20 at 12:17
  • BTW - see [this answer](https://stackoverflow.com/a/10773412/418556) for an example implementing most of the advice from above. Rather than using a modal `JDialog`, it uses a `JOptionPane` which is modal by default. Without the `RequestFocusListener` it would be less that 50 lines of code. – Andrew Thompson Jun 17 '20 at 03:01

0 Answers0