Boxlayout wont align

1.1k views Asked by At

Ok so I wanted to make a box layout to test it out, and on the layout help page it specifies that you can give it an alignement, which is what I tried but it seems to not work at all.

package gameflow;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginScreen extends JFrame 
{
    private static final Dimension SCREENSIZE = Toolkit.getDefaultToolkit().getScreenSize();
    private static final double DEFAULTHEIGHT = (SCREENSIZE.getHeight() * 80) / 100;
    private static final double DEFAULTWIDTH = (SCREENSIZE.getWidth() * 80) / 100;
    private static final long serialVersionUID = -7245840869407664992L;
    private JTextField userfield = new JTextField(), passfield = new JTextField();
    private JLabel userlabel = new JLabel("Username"), passlabel = new JLabel("Password");
    private static final Dimension fieldSize = new Dimension();

    public LoginScreen()
    {
        super("Wraith: the game");
        setBounds((SCREENSIZE.width * 10) / 100, (SCREENSIZE.height * 10) / 100,(int)DEFAULTWIDTH,(int)DEFAULTHEIGHT);
        fieldSize.setSize((double)this.getWidth()/2,(double)this.getHeight()/30);

        userfield.setMaximumSize(fieldSize);
        userfield.setForeground(new Color(1f,1f,1f));

        passfield.setMaximumSize(fieldSize);
        passfield.setForeground(new Color(1f,1f,1f));

        userlabel.setMaximumSize(fieldSize);
        passlabel.setMaximumSize(fieldSize);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setBackground(new Color(0f,0f,0f));
        panel.setAlignmentX(0.5f);

        panel.add(userlabel);
        panel.add(userfield);
        panel.add(passlabel);
        panel.add(passfield);
        this.add(panel, BorderLayout.CENTER);
    }
}

as you see, Panel has a boxlayout inside the JFrame whom has a BorderLayout, the components wont align to center. Why?

1

There are 1 answers

0
camickr On

First of all when you create a text field you should use:

JTextField textField = new JTextField(10);

The number will allow the component to determine a preferred size to display about 10 character. However, the actual size of the component may be determined by the layout manager.

the components wont align to center. Why?

Because that is the rule of the BoxLayout. When the panel is added to the BorderLayout of the JFrame, the panel is resized to fill then entire area of the frame. So know the BoxLayout will resize the components to fill the space available based in its rules.

First, each component is displayed at its preferred height and extra space is just added to the bottom of the panel. So all components appear at the top of the frame.

If you want the components to be vertically centered in the panel then you need to add "glue" to the top and bottom of the panel. Then empty space will be added above/below the components so they appear centered. Reread the tutorial link for more information on how "glue" works.

Next the components are resize horizontally to fill the width of the panel. A component is only filled up to its maximum width. For many components (label, button...) the maximum width is the preferred width, but some components (text components) don't have a maximum width.

So to center the components horizontally you need to set the alignment of each component using:

theComponent.setAlignment(0.5f);

For the text field you can prevent the text field from growing by using:

textField.setMaximumSize( textField.getPreferredSize() );

So experiment with these properties to see the different effect.

Having said all that, the easiest way to center a panel (vertically and horizontally) on a frame is to use the GridBagLayout on the content pane of the frame. Then the code is just:

setLayout( new GridBagLayout() );
add(panel, new GridBagConstraints());

and now the panel is centered in the frame at its preferred size.