java - GridBagLayout 权重错误?

标签 java swing layout grid

我正在处理的项目中的组件布局看起来不正确,我怀疑 Swing 中存在错误。基本上,似乎正在发生的事情是,当布局的单元格具有不同的最小尺寸和/或首选尺寸时,没有遵守 weightxweighty 比例。我创建了一个示例程序来演示这一点,这是源代码:

package com.ensoftcorp.product.simmerge.gui.swing.dialogs;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new TestClass();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static final GridBagConstraints GBC_CELL = new GridBagConstraints();
    static final GridBagConstraints GBC_ROWEND = new GridBagConstraints();
    static final GridBagConstraints GBC_FILLROW = new GridBagConstraints();
    static {
        GBC_CELL.anchor = GridBagConstraints.NORTHWEST;
        GBC_CELL.weightx = 0.5;
        GBC_CELL.fill = GridBagConstraints.BOTH;

        GBC_ROWEND.gridwidth = GridBagConstraints.REMAINDER;

        GBC_FILLROW.gridwidth = GridBagConstraints.REMAINDER;
        GBC_FILLROW.weightx = 1.0;
        GBC_FILLROW.fill = GridBagConstraints.BOTH;
    }

    public TestClass() {
        JFrame frame = new JFrame();

        JPanel pnlContent = new JPanel(new GridBagLayout());
        pnlContent.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        /*
         * Layout "ruler" panel
         */
        JPanel pnlRuler = new JPanel(new GridBagLayout());

        pnlRuler.add(createRulerCell(Color.BLACK, Color.WHITE),GBC_CELL);
        pnlRuler.add(createRulerCell(Color.BLACK, Color.WHITE),GBC_CELL);

        pnlRuler.add(Box.createHorizontalGlue(),GBC_ROWEND);

        /*
         * Layout "correct" panel
         */
        JPanel pnlGoodLayout = new JPanel(new GridBagLayout());

        pnlGoodLayout.add(new JButton("JButton1"),GBC_CELL);
        pnlGoodLayout.add(new JButton("JButton2"),GBC_CELL);

        pnlGoodLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        pnlGoodLayout.add(new JButton("JButton3"),GBC_CELL);
        pnlGoodLayout.add(new JButton("JButton4"),GBC_CELL);

        pnlGoodLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        /*
         * Layout "incorrect" panel
         */
        JPanel pnlBadLayout = new JPanel(new GridBagLayout());

        pnlBadLayout.add(new JButton("JButton1"),GBC_CELL);
        pnlBadLayout.add(new JButton("JButton2"),GBC_CELL);

        pnlBadLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        pnlBadLayout.add(new JButton("JButton number 3 is wide"),GBC_CELL);
        pnlBadLayout.add(new JButton("JButton4"),GBC_CELL);

        pnlBadLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        /*
         * Add panels to main panel
         */
        pnlContent.add(pnlRuler,GBC_FILLROW);
        pnlContent.add(Box.createVerticalStrut(8),GBC_FILLROW);
        pnlContent.add(pnlGoodLayout,GBC_FILLROW);
        pnlContent.add(Box.createVerticalStrut(8),GBC_FILLROW);
        pnlContent.add(pnlBadLayout,GBC_FILLROW);

        /*
         * Configure frame
         */
        frame.getContentPane().add(pnlContent);
        frame.setTitle("GridBagLayout Weight Bug?");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,200);
        frame.setVisible(true);
}

    JComponent createRulerCell(Color border, Color background) {
        JPanel glue = new JPanel();
        glue.setBorder(BorderFactory.createLineBorder(border));
        glue.setBackground(background);

        return glue;
    }

}

示例程序有三组...第一组是显示 50% 标记的各种“标尺”。第二组和第三组是使用 GridBagLayout 的面板,其中每个单元格的 weightx 为 0.5。两组之间的唯一区别是按钮文本长度,而第二组即使有足够的空间也不会按比例均匀排列列。

那么我的问题是:有没有人以前遇到过这个问题,并且有他们推荐的解决方法?

附言- 我使用的是 jdk1.6.0_11,如果有区别的话。

最佳答案

来自 GridBagConstraints.weightx JavaDoc :

Specifies how to distribute extra horizontal space.

The grid bag layout manager calculates the weight of a column to be the maximum weightx of all the components in a column. If the resulting layout is smaller horizontally than the area it needs to fill, the extra space is distributed to each column in proportion to its weight.

考虑到这一点,我不认为这是一个错误,因为计算出带有宽按钮的列需要更多空间。计算完该值和正常按钮列的大小后,将分配剩余空间。

解决方法:

要解决此问题,您可以在添加每个按钮之前以及与大按钮共享行的按钮之前设置 GBC_CELL.gridxGBC_CELL.gridy按钮,你可以设置

GBC_CELL.ipadx=100;

然后在添加宽按钮 (#3) 之前将其设置为 0。这应该使您的按钮在更多网格中对齐。

关于java - GridBagLayout 权重错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1010323/

相关文章:

java - 如何改变背景颜色?

java - 绘制像素时数组索引越界

android - 如何在 Gallery 和 WebView 之间按固定比例布局?

java - Eclipse - 加载 JNI 共享库失败

java - Android工作室错误: An error occurred while trying to compute required packages

java - java中的动态复选框

android - AVD 忽略布局大/文件

html - 如何使水平始终保持其背景颜色,即使沿 X 轴滚动也是如此?

java - 创建一个将读取 txt 文件的文件

java - Gradle 来自源的非托管依赖项