如何最大程度地发挥盒子胶的效果? / 如何优先考虑组件的首选尺寸?

2023-12-27

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.

我的(理想)假设:

  1. sourceAMask and sourceBMask是左对齐的并且需要最小宽度(根据最大长度)source[A|B]Package),因为右侧的水平粘合占据了分配给的所有进一步空间maskPairs.
  2. mask_initialPair(以及所有其他对)具有所需的最小高度(根据当前使用的字体大小),因为下面的垂直粘合占据了所有其他空间。
  3. 的左边距和右边距maskPage具有相同的宽度。
  4. 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);
  }
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何最大程度地发挥盒子胶的效果? / 如何优先考虑组件的首选尺寸? 的相关文章

随机推荐