JSeparator 不会与 GridBagLayout 一起显示

发布于 2024-08-25 06:59:07 字数 720 浏览 10 评论 0原文

我想使用 GridBagLayout 在两个组件之间添加一个垂直的 JSeparator。我的代码如下:

public MainWindowBody(){
    setLayout(new GridBagLayout());

    JPanel leftPanel = new InformationPanel();
    JPanel rightPanel = new GameSelectionPanel();

    JSeparator sep = new JSeparator(JSeparator.VERTICAL);
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.NORTH;

    add(leftPanel,gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.VERTICAL;

    add(sep,gbc);

    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.NONE;

    add(rightPanel,gbc);
}

但是 JSeperator 没有显示,有什么想法吗?

谢谢

I want to add a vertical JSeparator between two components using a GridBagLayout. The code I have is as follows:

public MainWindowBody(){
    setLayout(new GridBagLayout());

    JPanel leftPanel = new InformationPanel();
    JPanel rightPanel = new GameSelectionPanel();

    JSeparator sep = new JSeparator(JSeparator.VERTICAL);
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.NORTH;

    add(leftPanel,gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.VERTICAL;

    add(sep,gbc);

    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.NONE;

    add(rightPanel,gbc);
}

But the JSeperator doesn't show, any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2024-09-01 06:59:07

您可以尝试设置分隔符的首选宽度:

sep.setPreferredSize(new Dimension(5,1));

然后,使 GridBagLayout 用尽分隔符的所有可用高度:

gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1;

You could try to set the preferred width for the separator:

sep.setPreferredSize(new Dimension(5,1));

Then, make GridBagLayout use up all available height for the separator:

gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1;
何处潇湘 2024-09-01 06:59:07

摘自 SunJSeparator 指南:

在大多数实现中,垂直
分隔符的首选高度为 0,
水平分离器有一个
首选宽度为 0。这意味着
分隔符不可见,除非您
设置其首选大小或放置
它在布局的控制下
管理器,例如 BorderLayout 或
拉伸它以填充的 BoxLayout
其可用显示区域。

垂直分隔符确实有一点
宽度(和水平一点
高度),所以你应该看到一些空间
分隔符在哪里。然而,
实际的分割线没有画出来
除非宽度和高度都是
非零。

也许你应该设置正确的约束?

Taken from Sun's guide for JSeparator:

In most implementations, a vertical
separator has a preferred height of 0,
and a horizontal separator has a
preferred width of 0. This means a
separator is not visible unless you
either set its preferred size or put
it in under the control of a layout
manager such as BorderLayout or
BoxLayout that stretches it to fill
its available display area.

The vertical separator does have a bit
of width (and the horizontal a bit of
height), so you should see some space
where the separator is. However, the
actual dividing line isn't drawn
unless the width and height are both
non-zero.

Maybe you should set right constraints?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文