java - 如何使 JPanel 适合 JTabbedPane 大小?

标签 java swing jpanel

我正在尝试将 JPanel 添加到另一个 JPanel 中,并使其适合父“JPanel”大小。

我有一个 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,但我无法调整按钮的大小。我尝试使用 FlowLayoutGridBagLayout,但无法正确显示它们,因为我遇到了同样的问题。

提前谢谢您。

最佳答案

您的示例不完整,因此很难判断出了什么问题,但这也许会有所帮助:

JTable table = new JTable (12, 5);
JButton button = new JButton ("Button");

JPanel panel = new JPanel ();
panel.setLayout (new BorderLayout ());
panel.add (table, BorderLayout.CENTER);
panel.add (button, BorderLayout.SOUTH);

JTabbedPane tabbedPane = new JTabbedPane ();
tabbedPane.addTab ("Tab", panel);

JFrame frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
frame.getContentPane ().add (tabbedPane, BorderLayout.CENTER);
frame.pack ();
frame.setVisible (true);

关于java - 如何使 JPanel 适合 JTabbedPane 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799830/

相关文章:

java - 如何给某些细胞着色?

java - 如何用新的散点更新绘图区域?

java - 将 JPanel 转换为 png 或其他图像文件

java - 使用 log4j 在 Java 中记录运行时异常

java - org.springframework.security.access.SecurityConfig : Unsatisfied dependency expressed through constructor

java - JAXB SchemaFactory 源顺序必须遵循模式之间的导入顺序?

java - 在 Java 中使用 2 个 JButton 的问题

java - 如何在swing中自由布局动态添加按钮等组件?

java - JPanel TitledBorder 中的标题截断 - Java swing

java - 使用 Ant 的类路径、编译和运行?