避开网格拉特,仍确保垂直填充

问题描述 投票:0回答:1

我需要在容器中的两个水平定位的组件,它们两个都应填充所有容器的垂直空间。

由于填充要求,我现在必须使用重量重量

GridBagLayout

GridBagLayout(如果我希望它流到启动,运气不好,我没有选择)

component layout

java 8,秋千
java swing
1个回答
0
投票

在这种情况下,您可以使用

BoxLayout
的替代方案。
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class BoxLayoutMain {
    
    /**
     * Specifies minimum and maximum sizes for each child {@code Component} of the
     * {@linkplain BoxLayout#getTarget() target} of the given {@code BoxLayout}, such that aspect
     * ratios of their preferred size are maintained throughout parent resizing. Assumes all
     * children of the target are already added to it. Assumes the layout is horizontal.
     * @param boxLayout
     */
    private static void assignHorizontalBoxLayoutChildWeights(final BoxLayout boxLayout) {
        final int maxValue = Short.MAX_VALUE;
        final Container parent = boxLayout.getTarget();
        final int totalWidth = parent.getPreferredSize().width;
        final Dimension commonMinimumSize = new Dimension();
        for (final Component child: parent.getComponents()) {
            child.setMinimumSize(commonMinimumSize);
            final Dimension prefSize = child.getPreferredSize();
            child.setMaximumSize(new Dimension((int) Math.round(maxValue * (prefSize.width / (double) totalWidth)), maxValue));
        }
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(() -> {
            
            final JLabel component1 = new JLabel("Component 1", JLabel.CENTER);
            component1.setBorder(BorderFactory.createLineBorder(Color.RED, 5));
            
            final JLabel component2 = new JLabel("Component 2 (which is a little longer)", JLabel.CENTER);
            component2.setBorder(BorderFactory.createLineBorder(Color.GREEN, 5));
            
            final JPanel parent = new JPanel();
            parent.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
            final BoxLayout layout = new BoxLayout(parent, BoxLayout.X_AXIS);
            parent.setLayout(layout);
            parent.add(component1);
            parent.add(component2);
            
            assignHorizontalBoxLayoutChildWeights(layout);
            
            final JFrame frame = new JFrame("Box for no gridbag");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(parent);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}

产生以下结果:

Candidate solution with BoxLayout当父母调整大小时,孩子们保持宽度的宽高比,同时始终填满他们的整个高度。 不得不,可悲的是,在调整大小时,可以在父母右侧的绿色边界和蓝色边界之间观察到一个小的差距。这在以下图像中显示在棕色圆圈中:

尽管情况取决于情况,但这可能不是问题(例如,如果您没有在上述代码中使用任何边界)。

ContainerBoxLayout solution problem, with gap between borders在这种情况下,我也想到了,我相信能够产生所需的结果,但我还没有尝试。 在情况下,用户需要能够手动确定每个宽度

SpringLayout

,然后您可以使用

Component


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.