JSeparator 不会与 GridBagLayout 一起显示
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试设置分隔符的首选宽度:
然后,使 GridBagLayout 用尽分隔符的所有可用高度:
You could try to set the preferred width for the separator:
Then, make GridBagLayout use up all available height for the separator:
摘自 Sun 的
JSeparator
指南:也许你应该设置正确的约束?
Taken from Sun's guide for
JSeparator
:Maybe you should set right constraints?