2

I'm working on a layout for a simple login screen. I am using GridLayout in order to manipulate elements, but have came across an issue, It occupies full frame width like this:

enter image description here

Where as I want it to be with a fixed position and width,height and be vertically, horizontally centered inside a frame, but not occupy it's full size. Possibly make Username and Password labels occupy less space so text fields are actually closer to them. I tried setting main size of my panel like .setMaximumSize(Dimension (200, 150));

But it doesn't seem to work.

        //Create the frame.
        JFrame frame = new JFrame("Online Shop");
        JPanel mainPanel = new JPanel();
        JPanel usernamePanel = new JPanel();
        JPanel passwordPanel = new JPanel();
        JPanel buttonPanel = new JPanel();

        JButton loginButton = new JButton("Login");
        loginButton.setMaximumSize(new Dimension(100, 50));
        JTextField username = new JTextField();
        JLabel usernameLabel = new JLabel("Username");
        JPasswordField password = new JPasswordField();
        JLabel passwordLabel = new JLabel("Password");

        //Panel
        frame.setContentPane(mainPanel);

        usernamePanel.setLayout(new GridLayout(1,2));
        usernamePanel.add(usernameLabel);
        usernamePanel.add(username);

        passwordPanel.setLayout(new GridLayout(1,2));
        passwordPanel.add(passwordLabel);
        passwordPanel.add(password);

        mainPanel.setLayout(new GridLayout(3, 1));
        mainPanel.add(usernamePanel);
        mainPanel.add(passwordPanel);
        mainPanel.add(loginButton);

        //Event Listeners
        frame.addWindowListener(new MyWindowListener());
        loginButton.addActionListener(new MyActionListener());

        //Sizes, Positioning
        frame.setSize(720, 480);
        frame.setLocationRelativeTo(null);

        //Show Frame
        frame.setVisible(true);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ilja
  • 44,142
  • 92
  • 275
  • 498
  • Note that a log-in screen using only `GridLayout` will *always* look like rubbish. I would typically use a compound layout, or a `GroupLayout` for such things. Also agree strongly with both camickr & trashgod that explicitly setting sizes is a very bad idea. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Nov 27 '13 at 23:55

1 Answers1

4
frame.setSize(720, 480);

Don't use the setSize() method.

Instead use:

frame.pack();

Also use:

JTextField username = new JTextField(10);

To give the text fields a preferred number of characters.

Now when the frame is made visible the components will be displayed at their preferred size and the frame will be sized appropriately to fit the components.

If you want a little extra space between the components and the frame then you can do:

mainPanel.setBorder( new EmptyBorder(20, 20, 20, 20) );
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Using pack is fine, however If I expand it it still maintains this "stretching" behaviour, furthermore I need to be able to manipulate sizes of panels, as well as certain starting size of the frame. is there a way for me to manipulate panel sizes – Ilja Nov 27 '13 at 15:57
  • This is a login screen. There is no reason for the user to resize the dialog. Use the `setResizable(false)` method. However, one way to get around this is to create an addition panel called "contentPane". Add contentPane to the frame. Then add the mainPanel to contentPane. By default panels use a FlowLayout and the preferred size of components added to it will be retained. ` furthermore I need to be able to manipulate sizes of panels` - that is rarely a good requirement. Again, Swing knows better what the preferred size of a components should be base on the font and so on. – camickr Nov 27 '13 at 16:19
  • @camickr is correct; in addition, verify that you avoid this [pitfall](http://stackoverflow.com/a/12532237/230513). – trashgod Nov 27 '13 at 16:46