Tea*_*App 6 java user-interface swing awt layout-manager
我一直在研究Swing GUI并在添加后得到一些不寻常和不需要的空白,不JSeperator
知道如何删除它们?或任何其他选项如何很好地实现这一目标!
视觉描述
在JLabel
"速度" 之前和之后,差距是显而易见的JSlider
.
相关代码
control.setLayout(new BoxLayout(control, BoxLayout.X_AXIS));
...another code omitted...
control.add(orientation); //JLabel
control.add(norm); //JRadioButton
control.add(back); //JRadioButton
control.add(new JSeparator(SwingConstants.VERTICAL));
control.add(speedLabel); //JLabel
control.add(speed); //JSlider
control.add(new JSeparator(SwingConstants.VERTICAL));
control.add(turnOutLabel); //JLabel
control.add(right); //JRadioButton
control.add(straight); //JRadioButton
control.add(left); //JRadioButton
Run Code Online (Sandbox Code Playgroud)
我想要的是让所有东西都集中在JSeperator中,
视觉描述
谢谢.
只需替换new JSeparator(...)
为以下行(如果需要,可以将它们放在方法中):
JSeparator separator = new JSeparator(JSeparator.VERTICAL);
Dimension size = new Dimension(
separator.getPreferredSize().width,
separator.getMaximumSize().height);
separator.setMaximumSize(size);
Run Code Online (Sandbox Code Playgroud)
正如@kleopatra解释的那样,JSeparator具有无限制的最大尺寸(两个方向),因此这里的技巧是将最大宽度限制为首选宽度,但仍保持最大高度不变(因为首选高度为0
).
归档时间: |
|
查看次数: |
3117 次 |
最近记录: |