JSeparator不会显示GridBagLayout [英] JSeparator wont show with GridBagLayout

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

问题描述

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

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

但JSeperator没有显示任何想法?

But the JSeperator doesn't show, any ideas?

谢谢

推荐答案

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

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

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

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

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

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

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

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