How can I set distance between elements ordered vertically?

前端 未结 4 825
孤独总比滥情好
孤独总比滥情好 2021-01-17 13:03

I have code like that:

    JPanel myPanel = new JPanel();
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));

    JButton button = new JButton(         


        
4条回答
  •  孤城傲影
    2021-01-17 13:31

    If you're definitely intending to use BoxLayout to layout your panel, then you should have a look at the How to Use BoxLayout Sun Learning Trail, specifically the Using Invisible Components as Filler section. In short, with BoxLayout you can create special invisible components that act as spacers between your other components:

    container.add(firstComponent);
    container.add(Box.createRigidArea(new Dimension(5,0)));
    container.add(secondComponent);
    

提交回复
热议问题