GridBagLayout不显示组件 [英] GridBagLayout does not display a component

查看:116
本文介绍了GridBagLayout不显示组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个组件来显示圆形进度条.当我将其放入BorderLayout时,一切看起来都很好.

I wrote a component to display a circular progressbar. When I put it in BorderLayout everything looks fine.

但是设置GridBagLayout时看不到.

    p.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    p.add(north(), gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    p.add(west(), gbc);
    gbc.gridx = 1;
    gbc.gridy = 1;
    p.add(bar, gbc);

我不知道这是设置GridBagConstraints的问题还是其他原因.

I have no idea whether it is the problem of setting the GridBagConstraints or other reasons.

推荐答案

但是设置GridBagLayout时看不到.

But it can't be seen when a GridBagLayout is set.

可能是因为您没有覆盖组件的getPreferredSize()方法,所以首选大小为(0,0).

Probably because you didn't override the getPreferredSize() method of your component so the preferred size will be (0, 0).

GridBagLayout将尝试以其首选大小显示组件.

The GridBagLayout will attempt to display the component at its preferred size.

我猜想您在添加组件时需要使用fill约束,以便可以自动调整其大小以填充单元格中的可用空间.阅读Swing教程中如何使用GridBagLayout 有关更多信息和示例.

I would guess you would need to use the fill constraint when adding the component so it can be automatically resized to fill the space available in the cell. Read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.

当我将其放在BorderLayout中时,一切看起来都很好.

When I put it in BorderLayout everything looks fine.

添加到BorderLayout的CENTER时,将调整组件的大小以填充可用空间.

When added to the CENTER of a BorderLayout the component will be resized to fill the space available.

这篇关于GridBagLayout不显示组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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