How can I set distance between elements ordered vertically?

前端 未结 4 860
孤独总比滥情好
孤独总比滥情好 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:14

    You may want to consider GridLayout instead of BoxLayout, it has attributes Hgap and Vgap that let you specify a constant seperation between components.

    GridLayout layout = new GridLayout(2, 1);
    layout.setVgap(10);
    myPanel.setLayout(layout);
    myPanel.add(button);
    myPanel.add(label);
    

提交回复
热议问题