GridBagLayout does not display a component

风格不统一 提交于 2020-05-17 06:26:07

问题


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

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

    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);

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


回答1:


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

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

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

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.

When I put it in BorderLayout everything looks fine.

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



来源:https://stackoverflow.com/questions/61573185/gridbaglayout-does-not-display-a-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!