How can I align all elements to the left in JPanel?

前端 未结 3 1098
误落风尘
误落风尘 2020-12-14 05:42

I would like to have all elements in my JPanel to be aligned to the left. I try to do it in the following way:

JPanel panel = new JPanel();
panel.setLayout(n         


        
3条回答
  •  太阳男子
    2020-12-14 06:34

    You should use setAlignmentX(..) on components you want to align, not on the container that has them..

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.add(c1);
    panel.add(c2);
    
    c1.setAlignmentX(Component.LEFT_ALIGNMENT);
    c2.setAlignmentX(Component.LEFT_ALIGNMENT);
    

提交回复
热议问题