简体   繁体   English

如何在GridBagLayout中设置组件的大小

[英]How to set the component's size inside a GridBagLayout

As I've read, it's really hard to treat to resize components inside GridLayout and GrigBagLayout. 正如我所读的,调整GridLayout和GrigBagLayout内部组件的大小确实很难。 In my case, I have a GridBagLayout with 2 columns and 2 rows. 就我而言,我有一个带有2列2行的GridBagLayout。 In the first row there are 2 labels and in the second row there are 2 components (type Tablero ) which extend JTable. 第一行中有2个标签,第二行中有2个扩展JTable的组件(类型Tablero )。

My problem is, I can't make these 2 JTables fill the JFrame until its bottom. 我的问题是,我不能让这两个JTable填充JFrame直到其底部。 I've tried setSize(), setPreferredSize(), setBounds() ... but they don't work at all. 我已经尝试过setSize(), setPreferredSize(), setBounds() ...但它们根本不起作用。 I've also tried to do it by constraints.fill = GridBagConstraints.VERTICAL/SOUTH/CENTER ... but I get absolutly nothing. 我也尝试通过constraints.fill = GridBagConstraints.VERTICAL/SOUTH/CENTER来做到这一点……但是我什么都没得到。 I've also tried to set the size of the JPanel inside of the JFrame, but it doesn't work... 我也尝试过在JFrame中设置JPanel的大小,但是它不起作用...

This is my code: 这是我的代码:

public static void main(String[] args)
{

    JFrame pantalla = new JFrame();

    pantalla.getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();

    JLabel tab1Label = new JLabel("Tablero 1");
    JLabel tab2Label = new JLabel("Tablero 2");

    Tablero tablero1 = new Tablero();
    Tablero tablero2 = new Tablero();
    tablero1.setPreferredSize(new Dimension(400,400));
    tablero1.setBounds(0, 0, 400, 400);

    tablero1.setBorder(new LineBorder(Color.gray, 2));

    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.insets = new Insets(0, 0, 20, 0);
    pantalla.getContentPane().add(tab1Label, constraints);

    constraints.gridx = 1;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.insets = new Insets(0, 0, 20, 0);
    pantalla.getContentPane().add(tab2Label, constraints);

    constraints.insets = new Insets(0, 0, 0, 0);

    constraints.fill = GridBagConstraints.VERTICAL;
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    pantalla.getContentPane().add(tablero1, constraints);

    constraints.fill = GridBagConstraints.VERTICAL;
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    pantalla.getContentPane().add(tablero2, constraints);

    pantalla.pack();
    pantalla.setSize(800, 400);
    pantalla.setVisible(true);
    pantalla.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

And here is the result: 结果如下:

在此处输入图片说明

I know there are other ways easier to make it, but to use GridBagLayout is actually a requeriment. 我知道还有其他更容易实现的方法,但是使用GridBagLayout实际上是一项必要条件。

Try to add constraints.weightx = 1; 尝试添加constraints.weightx = 1; and constraints.weighty = 1; constraints.weighty = 1; to your JTables constraints. 到您的JTables约束。

Also remove those lines: 还要删除这些行:

tablero1.setPreferredSize(new Dimension(400,400));
tablero1.setBounds(0, 0, 400, 400);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM