java - 使用 JSeperator 时出现异常间隙 - Java

标签 java swing user-interface awt layout-manager

我一直在开发 Swing GUI,在添加 JSeperator 后出现了一些不寻常和不需要的间隙,知道如何删除它们吗?或者如何很好地实现这一目标的任何其他选择!

视觉描述

enter image description here

JLabel“Speed”之前和 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 分隔,

视觉描述

enter image description here

谢谢。

最佳答案

只需将 new JSeparator(...) 替换为以下几行(如果需要,可以将它们放在方法中):

JSeparator separator = new JSeparator(JSeparator.VERTICAL);
Dimension size = new Dimension(
    separator.getPreferredSize().width,
    separator.getMaximumSize().height);
separator.setMaximumSize(size);

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

关于java - 使用 JSeperator 时出现异常间隙 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7515241/

相关文章:

Java:使用scanner.nextLine().equals ("example"))需要重复输入

java - 什么是用于检测 JDatePicker 文本字段中的移动/单击的适当监听器?

java - 在数据库中存储 joda-time 日期时间

java - 序列化 JTabbedPane

Java - 如果 JBDC 成功连接则切换 Jframe

java - JRE 在不应出现的情况下给出 NullPointerException

java - 使用 HashMap 创建 JTree

ios - 绘制背景颜色和边框和背景 PNG

user-interface - 开发多显示器

twitter-bootstrap - 响应式网站在移动设备上缩小至全宽