我试图将JPanel
添加到另一个中,并使其适合父`JPanel大小。如何将JPanel放入JTabbedPane大小?
我有一个JPanel
其中包含使用该码的JTable
和JButton
:
JScrollPane attributeTable = new JScrollPane(this);
JPanel attributesPanel = new JPanel();
JPanel textFieldPanel=new JPanel();
JPanel buttonsPanel = new JPanel();
JButton newAttributeButton = new JButton("New Attribute");
attributesPanel.add(textFieldPanel, BorderLayout.CENTER);
attributesPanel.add(buttonsPanel, BorderLayout.SOUTH);
newAttributeButton.addActionListener(AttributesTableController.getInstance());
GroupLayout layout = new GroupLayout(textFieldPanel);
textFieldPanel.setLayout(layout);
// Turn on automatically adding gaps between components
layout.setAutoCreateGaps(true);
// Turn on automatically creating gaps between components that touch
// the edge of the container and the container.
layout.setAutoCreateContainerGaps(true);
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
// The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
// Putting the labels in a parallel group along the horizontal axis
// positions them at the same x location.
//
// Variable indentation is used to reinforce the level of grouping.
hGroup.addGroup(layout.createParallelGroup().
addComponent(attributeTable).addComponent(newAttributeButton));
layout.setHorizontalGroup(hGroup);
// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(attributeTable));
vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER).
addComponent(newAttributeButton));
layout.setVerticalGroup(vGroup);
当我有此面板(名为attributesPanel
)成JTabbedPane
的一个选项卡,它只是显示JTable
和JButton
,但在面板的中心。我希望attributesPanel
的尺寸与打开标签的尺寸相同。
这是我使用到的JPanel添加到我的JTabbedPane的代码:
TabbedPane tabbedPane = new TabbedPane();
tabbedPane.setComponentAt(1, attributesPanel);
我使用GridLayout
尝试,它安装在JPanel
很好,但我没能调整按钮。我尝试了FlowLayout
和GridBagLayout
,但我无法正确显示它们,因为我遇到了同样的问题。
预先感谢您。
请编辑您的问题包括[SSCCE(http://sscce.org/)展示您描述的问题,例如(http://stackoverflow.com/a/11949899/230513)。 – trashgod 2013-02-10 16:40:41