Java - JLabel和垂直框之间的距离

时间:2017-06-18 20:14:38

标签: java swing model-view-controller

我写了这个代码用于实现一个盒子,我把四个JRadioButtons

JRadioButton beginner = new JRadioButton("Beginner"); beginner.setSelected(true);
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");

Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);

此外,我使用setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50))方法设置了框的边框。

另外,我创建了一个JLabel来指示框的标题:

JLabel difficulty = new JLabel("Choose the difficulty:");

这是代码的完整部分:

Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.add(difficulty);
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);

如何增加JLabel和JRadioButton之间的空间?

1 个答案:

答案 0 :(得分:2)

  

如何增加JLabel和JRadioButton之间的空间?

添加标签后,您可以在面板中添加间距组件:

boxDifficulty.add(difficulty);
boxDifficulty.add( Box.createVerticalStrut(...) );

支柱是固定高度(宽度)的不可见组件。