调整大小后,gridbaglayout的组件可以填充父框架吗? [英] Can components of a gridbaglayout fill parent frame upon resize?

查看:144
本文介绍了调整大小后,gridbaglayout的组件可以填充父框架吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFrame,其根JPanelGridBagLayout实例化.在运行时,面板会根据一些描述填充组件,并且在描述中给出了宽度,高度和x,y坐标,以便与gridwidthgridheightgridxgridy字段一起使用在GridBagConstraints中.组件本身也可以是带有其自己的子组件的JPanelGridBagConstraints,GUI在树中描述,因此可以递归地填充Frame.

I've got a JFrame whose root JPanel is instantiated with a GridBagLayout. At runtime, the panel is populated with components based on some description, and the width, height and x,y coords are given in the description to be used with the gridwidth, gridheight, gridx and gridy fields in GridBagConstraints. The components themselves can be also be JPanels with their own child components and GridBagConstraints, the GUI is described in a tree so Frame is populated recursively.

我遇到的问题是,当调整框架的大小时,内部组件不会拉伸以填充其给定的宽度和高度.我在下面给出了带有截图的布局代码示例.

The problem I'm having is that when the frame is resized, the inner components are not stretched to fill their given widths and heights. I've given an example of the layout code below, with screenshots.

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.*;

public class GridBagTest extends JPanel {

    private final GridBagConstraints gbc = new GridBagConstraints();

    public GridBagTest(){

        setLayout(new GridBagLayout());
        add(gbcComponent(0,0,1,2), gbc);    
        add(gbcComponent(1,0,2,1), gbc);            
        add(gbcComponent(1,1,1,1), gbc);            
        add(gbcComponent(2,1,1,1), gbc);            

    }

    //Returns a JPanel with some component inside it, and sets the GBC fields
    private JPanel gbcComponent(int x, int y, int w, int h){

        gbc.gridx = x; 
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;
        gbc.fill = GridBagConstraints.BOTH;
        JPanel panel = new JPanel();
        JTextField text = new JTextField("(" + w + ", " + h + ")");
        panel.setBorder(new TitledBorder("(" + x + ", " + y + ")"));        
        panel.add(text);
        return panel;

    }

    public static void main (String args[]){

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new GridBagTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

我需要它,在图片2中,调整了容纳组件的内部JPanel的大小,以填充它们在GridBagLayout上各自的宽度和高度,同时也理想地拉伸了它们的组件.

I need it so in picture 2, the inner JPanels holding the components are resized to fill their respective widths and heights on the GridBagLayout, ideally stretching their components as well.

推荐答案

这通常是由于您未设置weightx和weighty约束.

This is usually due to your not setting weightx and weighty constraints.

尝试:

gbc.gridx = x; 
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;

// **** add here:
gbc.weightx = 1.0;
gbc.weighty = 1.0;

如果需要,这将允许您的GUI在x和y方向上扩展.

This will allow your GUI to expand in the x and y directions if need be.

这篇关于调整大小后,gridbaglayout的组件可以填充父框架吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆