微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用JSeperator时不寻常的差距 – Java

我一直在研究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中,

视觉描述

谢谢.

解决方法

只需用以下行替换新的JSeparator(…)(如果需要,可以将它们放在方法中):
JSeparator separator = new JSeparator(JSeparator.VERTICAL);
Dimension size = new Dimension(
    separator.getPreferredSize().width,separator.getMaximumSize().height);
separator.setMaximumSize(size);

正如@kleopatra解释的那样,JSeparator具有无限制的最大尺寸(在两个方向上),因此这里的技巧是将最大宽度限制为首选宽度,但仍保持最大高度不变(因为首选高度为0).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐