Java BoxLayout alignment issue
- by ManInMoon
Can anyone help me. Why is the Label "Current" NOT left aligned in Panel/Frame?
public static void main(String[] args) {
JFrame TFrame = new JFrame("Test DisplayLayout");
TFrame.setResizable(true);
TFrame.setSize(new Dimension(900, 840));
TFrame.setLocationRelativeTo(null);
TFrame.setTitle("DisplayLayout");
TFrame.setVisible(true);
JPanel P = DisplayLayout2();
P.setVisible(true);
P.setOpaque(true);
P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));
TFrame.add(P);
TFrame.revalidate();
TFrame.repaint();
}
public static JPanel DisplayLayout2() {
JPanel Panel=new JPanel();
Panel.setVisible(true);
Panel.setOpaque(true);
Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
JLabel lab = new JLabel("Current");
lab.setHorizontalAlignment(SwingConstants.LEFT);
lab.setForeground(Color.WHITE);
lab.setBackground(Color.PINK);
lab.setOpaque(true);
Panel.add(lab,Component.LEFT_ALIGNMENT);
JPanel posPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(posPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setPreferredSize(new Dimension(290, 200));
scrollPane.setOpaque(true);
posPanel.setBackground(Color.YELLOW);
posPanel.setPreferredSize(new Dimension(290, 200));
posPanel.setLayout(new BoxLayout(posPanel, BoxLayout.Y_AXIS));
posPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
Panel.add(scrollPane);
return Panel;
}