GridBagLayout 创建左右两个不同大小的JPanel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridBagLayout 创建左右两个不同大小的JPanel相关的知识,希望对你有一定的参考价值。

我需要使用GridBagLayout来创建一个一行两列的表格,其中第一列横跨三个Cell,第二列横跨一个Cell,就是类似于:
<tr>
<td colspan="3"> </td>
<td> </td>
</tr>
常量什么的我都设置好的,但是出来的效果确是两个相同大小的JPanel,并没有按照我的意图来创建,所以在这里提问这是为什么?是我的代码有问题还是还有什么东西我没考虑到?
下面是代码:
import java.awt.*;
import javax.swing.*;
public class Demo extends JFrame

private final int WIDTH = 500 ;
private final int HEIGHT = 300 ;
public Demo()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH) / 2,((Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT)) / 2,WIDTH,HEIGHT);

/////////////set layout///////////////
Container cont = getContentPane();
GridBagLayout layout = new GridBagLayout();

cont.setLayout(layout);
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.BOTH;
c1.weightx=1.0;
c1.weighty=1.0;

JPanel p1 = new JPanel(); p1.setBorder(BorderFactory.createLineBorder(Color.RED));
JPanel p2 = new JPanel(); p2.setBorder(BorderFactory.createLineBorder(Color.RED));
c1.gridx=0 ;
c1.gridy=0 ;
c1.gridwidth=3;
c1.gridheight=1;
cont.add(p1,c1);

GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
c2.weightx=1.0;
c2.weighty=1.0;
c2.gridx=3;
c2.gridy=0;
c2.gridwidth=1;
c2.gridheight=1;
cont.add(p2,c2);


public static void main(String[] args)
new Demo().setVisible(true);

把c1的权重weightx由1.0改成3.0,即c1.weightx=3.0;就行了。
完整的程序如下:(改动的地方见注释)
import java.awt.*;
import javax.swing.*;
public class Demo extends JFrame

private final int WIDTH = 500 ;
private final int HEIGHT = 300 ;
public Demo()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH) / 2,((Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT)) / 2,WIDTH,HEIGHT);

/////////////set layout///////////////
Container cont = getContentPane();
GridBagLayout layout = new GridBagLayout();

cont.setLayout(layout);
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.BOTH;
c1.weightx=3.0; //这里把c1.weightx=1.0;改成c1.weightx=3.0;
c1.weighty=1.0;

JPanel p1 = new JPanel(); p1.setBorder(BorderFactory.createLineBorder(Color.RED));
JPanel p2 = new JPanel(); p2.setBorder(BorderFactory.createLineBorder(Color.RED));
c1.gridx=0 ;
c1.gridy=0 ;
c1.gridwidth=3;
c1.gridheight=1;
cont.add(p1,c1);

GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
c2.weightx=1.0;
c2.weighty=1.0;
c2.gridx=3;
c2.gridy=0;
c2.gridwidth=1;
c2.gridheight=1;
cont.add(p2,c2);


public static void main(String[] args)
new Demo().setVisible(true);

参考技术A 我想每个位置得有东西占着,也就是说如果 0,0 没人用,你设定 3, 0 就不行吧?追问

第一个Cell的的坐标是0,0单元格横跨3列,那么第二个Cell的坐标当然就是3,0了,两个Cell中都设置的JPanel呢

参考技术B 0

GridBagLayout 组件被拉伸

【中文标题】GridBagLayout 组件被拉伸【英文标题】:GridBagLayout component stretched 【发布时间】:2022-01-12 19:57:54 【问题描述】:

我创建了一个GridBagLayout,在其中添加了不同的组件:

private static void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) 
          c.gridx = gridx;  //column
          c.gridy = gridy;  //row
          c.anchor = anchor;//position
          c.insets = new Insets(10,0,0,5);  //top left bottom right padding
          c.fill = GridBagConstraints.HORIZONTAL;
       
setMyConstraints(constraints,0,0,GridBagConstraints.NORTH);
        panel1.add(namelabel,constraints);
        setMyConstraints(constraints,1,0,GridBagConstraints.NORTH);
        panel1.add(name,constraints);
        setMyConstraints(constraints,0,1,GridBagConstraints.NORTH);
        panel1.add(genderlabel,constraints);
        setMyConstraints(constraints,0,2,GridBagConstraints.NORTH);
        panel1.add(gender,constraints);
        setMyConstraints(constraints,1,1,GridBagConstraints.NORTH);
        panel1.add(optionlabel,constraints);
        setMyConstraints(constraints,1,2,GridBagConstraints.NORTH);
        panel1.add(option,constraints);
        setMyConstraints(constraints,0,3,GridBagConstraints.NORTH);
        panel1.add(addrlabel,constraints);
        setMyConstraints(constraints,0,4,GridBagConstraints.NORTH);
        panel1.add(addr,constraints);
        setMyConstraints(constraints,0,5,GridBagConstraints.NORTH);
        panel1.add(alabel,constraints);
        setMyConstraints(constraints,0,6,GridBagConstraints.NORTH);
        panel1.add(a,constraints);
        setMyConstraints(constraints,1,5,GridBagConstraints.NORTH);
        panel1.add(blabel,constraints);
        setMyConstraints(constraints,1,6,GridBagConstraints.NORTH);
        panel1.add(b,constraints);
        setMyConstraints(constraints,2,5,GridBagConstraints.NORTH);
        panel1.add(clabel,constraints);
        setMyConstraints(constraints,2,6,GridBagConstraints.NORTH);
        panel1.add(c,constraints);
        
        setMyConstraints(constraints,0,7,GridBagConstraints.SOUTHWEST);
        panel1.add(dOkButton,constraints);
        setMyConstraints(constraints,1,7,GridBagConstraints.SOUTHEAST);
        panel1.add(dClearButton,constraints);

每当性别的组合框被拉伸以包裹文本时,相同 x 位置的组件也会被拉伸,如果 addr. textarea 也增加 column 相同位置 x 的组件拉伸。

这是当前的输出:

ok button, a textfiled and namelabel is tretched

预期输出:

even though the component like combobox stretched or add text area increased, the other component at same x position must not stretched

【问题讨论】:

如需更好的帮助,请edit添加minimal reproducible example。 【参考方案1】:

第一列似乎设置为“增长”,它的权重可能设置为“1.0”。

如果所有的列权重都设置为“0.0”,它们将不会被拉伸:

gridBagLayout.columnWeights = new double[] 0.0, 0.0, 0.0, Double.MIN_VALUE;

【讨论】:

以上是关于GridBagLayout 创建左右两个不同大小的JPanel的主要内容,如果未能解决你的问题,请参考以下文章

GridBagLayout:均匀分布的单元格

java gridbaglayout 一个组件变化会影响其它组件的大小,能不能做一个设置能让每个组件的大小和位置固定呢

如何创建具有 GridBagLayout 单元格精确尺寸的 ImageIcon

在 java 中使用 GridBagLayout 时设置最大组件大小

如何在 GridBagLayout 中调整 JPanel 的大小?

使 2 个 JButton 大小相等