In the code below, by calling setAlignmentX
with Component.LEFT_ALIGNMENT
I expected to get a left aligned label over a centered slider. For some reason the label is also centered, seemingly regardless of what value is passed to setAlignmentX.
What value must I pass to setAlignmentX to get it left aligned?
package myjava;
import java.awt.Component;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
public class LayoutTest {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("BoxLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create left aligned label over centered column
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JLabel label = new JLabel("test");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
contentPane.add(label);
contentPane.add(new JSlider());
frame.pack();
frame.setVisible(true);
}
});
}
}