0

我在 Java Swing 中实现一个 UI。因此我使用一个JTabbedPane. tabbedPane 在启动时没有组件。当我将选项卡添加到选项卡窗格时,选项卡窗格的宽度会增加,当我删除选项卡时,宽度会调整为启动时的宽度。这不应该发生。

选项卡窗格放置在JPanel具有网格包布局的 a 上。

布局代码:

Container contentPane = mainFrame.getContentPane();
contentPane.setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();        
// add the component tree
initComponentTree();
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 1;
c.weighty = 1;
contentPane.add( componentTree, c );        
// add the tabbed pane
initTabbedPane();
c.gridx = 1;
c.weightx = 10;
contentPane.add( tabbedPane, c );       
// add the status panel
initStatusPanel();
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0;
c.weighty = 0;
contentPane.add( statusPanel, c );


希望有人能帮忙!

4

3 回答 3

2

尝试将ipadx的值设置GridBagConstraints为所需的宽度。

于 2012-09-11T11:48:23.023 回答
0

我一直认为应该不惜一切代价避免使用 GridBagLayout。它确实提供了通过将面板与其他布局嵌套来实现的功能。它唯一提供的就是很多挫败感。

我建议将您的 JTabbedPane 放在使用 BorderLayout 的面板中,并将其放在 CENTER 位置。如果您坚持使用 GridBayLayout,您可以尝试使用 setMinimumSize() 方法,GridBagLayout 是支持该方法的布局管理器之一(BoxLayout 是另一个)。

于 2012-09-11T15:36:36.947 回答
-1

你试过setSize()吗?

JTabbedPane pane;
pane.setSize(SOME_WIDTH_CONSTANT, SOME_HEIGHT_CONSTANT);
于 2012-09-11T11:11:35.720 回答