Setup:
- 在屏幕的右侧,有一个从上到下的列表。
- On the left side of the screen, there are manipulators for this list;
- 一个操纵器由两个元件组成,一个紧挨着另一个。
- at first only one manipulator exists, but further ones may appear;
这里,操纵器的元素是 JComboBoxes:sourceAMask
and sourceBMask
.
为了确保它们并排出现,我将它们包含在Box mask_initialPair
沿着BoxLayout.X_AXIS
。
它们应该左对齐,所以我用一个填充了右边Box.createHorizontalGlue
.
允许mask_initialPair
为了垂直不拉伸,我将其封装在Box maskPairs
沿着BoxLayout.Y_AXIS
并添加了一个Box.createVerticalGlue
在它下面。
要将这个垂直列表放在另一个列表旁边,targetScroller
,我将两者都包含在Box maskPage
沿着BoxLayout.X_AXIS
。这个结构应该显示在居中,所以我用左边和右边填充Box.createHorizontalGlue
.
我的(理想)假设:
-
sourceAMask
and sourceBMask
是左对齐的并且需要最小宽度(根据最大长度)source[A|B]Package
),因为右侧的水平粘合占据了分配给的所有进一步空间maskPairs
.
-
mask_initialPair
(以及所有其他对)具有所需的最小高度(根据当前使用的字体大小),因为下面的垂直粘合占据了所有其他空间。
- 的左边距和右边距
maskPage
具有相同的宽度。
-
maskPairs
and targetScroller
具有相同的宽度。
只有3个持有。
4 被违反为maskPairs
远宽于targetScroller
,因为(违反1),水平胶水内mask_initialPair
接收与胶水相同的宽度maskPage
,就好像插入在maskPairs
and targetScroller
,而不是作为maskPairs
.
2 被违反为mask_initialPair
与其下方的胶水高度相等。
问题:
- 我了解水平不对中是如何发生的。
- 我不明白为什么可见组件会发生拉伸(在两个维度上),尽管总是有胶水占据多余的空间。
在这两种情况下:我该如何修复它?
理想情况下,不要重写所有类并再次按其大小膨胀我的代码。
import javax.swing.*;
class Test
{
public static void main(String[] args)
{
String[] sourceAPackage = { "A::entry1", "A::entry2", "A::entry3" };
String[] sourceBPackage = { "B::entry1", "B::entry2" };
String[] targetPackage = {};
JList<String> targetList = new JList<String>(targetPackage);
JScrollPane targetScroller = new JScrollPane(targetList);
JComboBox sourceAMask = new JComboBox<String>(sourceAPackage);
JComboBox sourceBMask = new JComboBox<String>(sourceBPackage);
sourceAMask.setEditable(true);
sourceBMask.setEditable(true);
Box mask_initialPair = new Box(BoxLayout.X_AXIS);
mask_initialPair.add(sourceAMask);
mask_initialPair.add(sourceBMask);
mask_initialPair.add(Box.createHorizontalGlue());
Box maskPairs = new Box(BoxLayout.Y_AXIS);
maskPairs.add(mask_initialPair);
maskPairs.add(Box.createVerticalGlue());
Box maskPage = new Box(BoxLayout.X_AXIS);
maskPage.add(Box.createHorizontalGlue());
maskPage.add(maskPairs);
maskPage.add(targetScroller);
maskPage.add(Box.createHorizontalGlue());
JFrame frame = new JFrame();
frame.add(maskPage);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}