As an alternative, consider a nested layout. In the example below, the relevant label is added to the SOUTH
area of a BorderLayout
, the default for JFrame
, and a placeholder for your login panel is added to CENTER
. Examine the resize behavior of each approach for suitability.
Addendum: I hope to [learn] why the setAlignmentY()
is being ignored.
As noted in How to Use BoxLayout: Box Layout Features, "When a BoxLayout
lays out components from top to bottom, … any extra space appears at the bottom of the container." This explains your original observation and correct solution.
In the API, note that setAlignmentX()
"Sets the the vertical alignment," and setAlignmentY()
"Sets the the horizontal alignment." In this context, vertical means the vertical axis of a top-to-bottom layout, such as BoxLayout.Y_AXIS
, while horizontal means the horizontal axis of a left-to-right layout, such as BoxLayout.X_AXIS
. In How to Use BoxLayout: Fixing Alignment Problems, BoxAlignmentDemo
contrasts the two. In a left-to-right layout, pictured below, setAlignmentY()
is used to adjust vertical positioning relative to the horizontal layout axis. In a top-to-bottom layout, such as yours, setAlignmentY()
simply has no effect.


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/** @see https://stackoverflow.com/a/18805146/230513 */
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createLogin(), BorderLayout.CENTER);
JLabel admonition = new JLabel("ChatBytes™—Do not steal.", JLabel.CENTER);
f.add(admonition, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static JPanel createLogin() {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
JLabel label = new JLabel("Existing CHATBYTES login panel.");
label.setFont(label.getFont().deriveFont(Font.ITALIC, 24f));
label.setAlignmentX(0.5f);
label.setBorder(new EmptyBorder(0, 20, 0, 20));
p.add(Box.createVerticalStrut(36));
p.add(label);
p.add(Box.createVerticalStrut(144));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().display();
}
});
}
}