当前位置: 首页 > 知识库问答 >
问题:

GridBagLayout中的JPanel不保持比例(weightx)

松和泰
2023-03-14

我有以下情况。我用GridBagLayout创建了一个JPanel,其比例如下。我想弄清楚这一点,然后我会添加更多的组件。现在我有点像

------------------------------------
|LABEL 45%  | LABEL 10% | LABEL 45%|
------------------------------------

每个标签水平和垂直各取1个单元格(网格宽度/高度=1)

JPanel topPanel = new JPanel(new GridBagLayout());

    topPanel.add(new JLabel("1"), new GridBagConstraints(
        0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
    topPanel.add(new JLabel("2"), new GridBagConstraints(
        1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.BOTH, INSETS_0PX, 0, 0));
    topPanel.add(new JLabel("3"), new GridBagConstraints(
        2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
        GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));

此时一切正常,第一个标签获得与右标签完全相同的空间(这是可用宽度的45%,另一个获得10%)。完美。

但是,当我试图添加另一个带有GridBagLayout的JPanel而不是标签(左/右)时,比例就不按定义保持了。

在左边,我想添加一个JPanel,它的定义如下

----------------------
|JLabel..............|
|--------------------|
|JTextArea...........|
|....................|
----------------------

JTextArea-加权1.0(x)、1.0,西锚,填充两者(以拉伸)

myPanel = new JPanel(new GridBagLayout());
myPanel.add(new JLabel("TITLE", new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
        GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

在右边,我想添加一个带有GridBagLayout的JPanel,它包含另外两个带有GridBagLaouts的JPanels(现在描述如何定义没有意义)。

-->因此,当我添加左面板而不是标签时,它得到了大约60%的屏幕(这不对,它应该得到45%)。

你能看出哪里出了问题吗?我没主意了

勒:这是一个简单的例子

public class Example extends JFrame {

public Example() {
    init();
}

private JPanel createPanel(Component comp) {
    JPanel topPanel = new JPanel(new GridBagLayout());

    topPanel.add(comp, new GridBagConstraints(
        0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));
    topPanel.add(new JLabel("2"), new GridBagConstraints(
        1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
    topPanel.add(new JLabel("3"), new GridBagConstraints(
        2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
        GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0, 0));

    return topPanel;
}

private JPanel getPanelWithLabel() {
    return createPanel(new JLabel("1"));
}

private JPanel getPanelWithPanel() {
    JPanel myPanel = new JPanel(new GridBagLayout());
    myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
    myPanel.add(new JTextArea(5,25), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
            GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
    return createPanel(myPanel);
}

private void init() {
    setTitle("Simple example");
    setSize(800, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(getPanelWithLabel(), BorderLayout.CENTER);
//panel.add(getPanelWithPanel(), BorderLayout.CENTER);

    add(panel);
    setVisible(true);
}

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Example ex = new Example();
        }
    });

}
}

共有1个答案

邹阳
2023-03-14

问题出在jtextarea的行上。

由于25行比45%的行宽,因此它本身就会消耗。

我不使用25,而是使用容器的width(MyPanel)

myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
                GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

因为这种方法的尺寸仍然不同。我尝试首先创建一个JPanel并向其添加JLabel

JPanel threeCont = new JPanel(new GridBagLayout());
threeCont.add(new JLabel("3"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
// We add myPanel and two(JLabel) to topPanel first and than the below.
topPanel.add(threeCont, new GridBagConstraints(
    2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
    GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));
System.out.println("First Panel's Size : " + myPanel.getSize());
System.out.println("Second Panel's Size : " +two.getSize());
System.out.println("Third Panel's Size : " +threeCont.getSize());
First Panel's Size : java.awt.Dimension[width=441,height=96]
Second Panel's Size : java.awt.Dimension[width=98,height=96]
Third Panel's Size : java.awt.Dimension[width=441,height=16]
public static void main(String[]a) {
    JFrame fr = new JFrame();
    fr.setSize(new Dimension(1000,1000));
    fr.setLocationRelativeTo(null);
    JPanel topPanel = new JPanel(new GridBagLayout());
    Insets INSETS_0PX = new Insets(0,0,0,0);
    JLabel one = new JLabel("1");
    JLabel two = new JLabel("2");
    one.setBackground(Color.yellow);
    two.setBackground(Color.blue);

    JPanel myPanel = new JPanel(new GridBagLayout());
    myPanel.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));
    myPanel.add(new JTextArea(5,myPanel.WIDTH), new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
            GridBagConstraints.WEST, GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

    JPanel threeCont = new JPanel(new GridBagLayout());
    threeCont.add(new JLabel("TITLE"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, INSETS_0PX, 0, 0));

    topPanel.add(myPanel, new GridBagConstraints(
        0, 0, 1, 1, 0.45, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));

    topPanel.add(two, new GridBagConstraints(
        1, 0, 1, 1, 0.1, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.BOTH, INSETS_0PX, 0, 0));

    topPanel.add(threeCont, new GridBagConstraints(
        2, 0, 1, 1, 0.45, 0.0, GridBagConstraints.EAST,
        GridBagConstraints.HORIZONTAL, INSETS_0PX, 0, 0));


    fr.add(topPanel);

    fr.setVisible(true);

    System.out.println("First Panel's Size : " + myPanel.getSize());
    System.out.println("Second Panel's Size : " +two.getSize());
    System.out.println("Third Panel's Size : " +threeCont.getSize());
}
 类似资料: