面板未添加到主框架
我有一个主框架和 3 个面板。我想将这 3 个面板添加到主框架中。但是,仅添加了其中 2 个。第三个则不然。我不明白为什么。有人可以帮忙吗?
setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=GridBagConstraints.RELATIVE;
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=gbc.weighty=1;
add(topPanel1, gbc);
add(bottomPanel1, gbc);
gbc.gridheight=GridBagConstraints.REMAINDER;
add(buttonsPanel, gbc);
上面的代码是一个框架的构造函数。
I have a main frame and 3 panels. I want to add those 3 panels to the main frame. However, only 2 of them are getting added. The third one is not. I am not able to figure out why. Can someone help?
setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.gridheight=GridBagConstraints.RELATIVE;
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=gbc.weighty=1;
add(topPanel1, gbc);
add(bottomPanel1, gbc);
gbc.gridheight=GridBagConstraints.REMAINDER;
add(buttonsPanel, gbc);
the above code is the constructor of a frame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该根据所需的布局设置
gridx
或gridy
值。例如,如果您想垂直布局组件,请执行以下操作:
如果您不设置 gridx/y 值,则行为未指定(有时可能有效)。
You should set the
gridx
orgridy
value, according to your desired layout.E.g. if you want to layout the components vertically do something like this:
If you do not set the
gridx/y
values the behavior is unspecified (sometimes it may work).如果您想逐个添加三个面板,则必须删除
gbc.gridheight=GridBagConstraints.RELATIVE;
语句。You have to remove the
gbc.gridheight=GridBagConstraints.RELATIVE;
statement if you want to add three panels one after one.