我正在尝试将 a 添加JPanel
到另一个中并使其适合父级 `JPanel 大小。
我有一个JPanel
包含 aJTable
和 a 的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
)放入 a 的选项卡时JTabbedPane
,它只显示JTable
和JButton
,但在面板的中心。我希望attributesPanel
的尺寸与打开的标签的尺寸相同。
这是我用来将 JPanel 添加到 JTabbedPane 中的代码:
TabbedPane tabbedPane = new TabbedPane();
tabbedPane.setComponentAt(1, attributesPanel);
我尝试使用GridLayout
,它很适合,JPanel
但我无法调整按钮的大小。我尝试使用 aFlowLayout
和 a GridBagLayout
,但我无法正确显示它们,因为我遇到了同样的问题。
先感谢您。