无法将3个JPanel添加到主面板 [英] Not able to add 3 JPanels to a main panel
问题描述
我有3个JPanels,我想将它们全部放在一个JPanel中。我使用GridBagLayout作为主面板。但是只添加了一个面板。为什么会这样?
I have 3 JPanels and I want to place them all in one JPanel. I used the GridBagLayout for the main panel. But only one panel is getting added. Why might this be?
gblayout=new GridBagLayout();
gbc=new GridBagConstraints();
panel1Customizer();
panel2customizer();
panel3Customizer();
setLayout(gblayout);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.weightx=1;
gbc.weighty=1;
gbc.gridheight=GridBagConstraints.REMAINDER;
add(panel1, gbc);
add(panel2, gbc);
gbc.gridwidth=GridBagConstraints.REMAINDER;
add(panel3, gbc);
自定义程序方法是将项目添加到这些面板中的方法。
The customizer methods are ones which add items into these panels.
推荐答案
我不确定,但我认为你需要在你的GridBagLayout中添加一个GridBagConstraints。试试看这个网站,了解如何使用GridBagLayout:
链接
I am not sure but I think you need to add a GridBagConstraints to your GridBagLayout. Try look at this site to get the idea on how to work with GridBagLayout: link
或者只是为您的JFrame使用另一个布局,可能是BorderLayout或GridLayout来正确排列您的面板
Or maybe just use another Layout for your JFrame, maybe BorderLayout or GridLayout to arrange your Panels correctly
这篇关于无法将3个JPanel添加到主面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!