我一直在开发 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
我想要的是让一切都由 JSeperator 居中和分隔,
视觉描述
谢谢。
只需更换new JSeparator(...)
包含以下几行(如果需要,可以将它们放入方法中):
JSeparator separator = new JSeparator(JSeparator.VERTICAL);
Dimension size = new Dimension(
separator.getPreferredSize().width,
separator.getMaximumSize().height);
separator.setMaximumSize(size);
正如 @kleopatra 所解释的,JSeparator 具有无限的最大尺寸(在两个方向上),因此这里的技巧是将最大宽度限制为首选宽度,但仍然保持最大高度不变(因为首选高度是0
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)