我正在Java Swing中实现一个UI。
因此我使用JTabbedPane
。
tabbedPane在启动时没有组件。当我向tabbedpane添加一个标签时,tabbedpane的宽度会增加,当我删除标签时,宽度会调整到启动时的宽度。这不应该发生。
tabbedpane放置在JPanel
上,它有一个网格布局。
布局代码:
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 );
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 );
希望有人可以提供帮助!
答案 0 :(得分:2)
尝试将ipadx
的{{1}}值设置为所需的宽度。
答案 1 :(得分:0)
我一直认为应该不惜一切代价避免使用GridBagLayout。它确实提供了通过嵌套面板与其他布局无法实现的任何功能。它提供的唯一的东西是很多挫折。
我建议使用BorderLayout将JTabbedPane放在面板中并将其置于CENTER位置。如果你坚持使用GridBayLayout,你可以尝试使用setMinimumSize()方法,GridBagLayout是尊重该方法的布局管理器之一(BoxLayout是另一个)。
答案 2 :(得分:-1)
您是否尝试过setSize()?
JTabbedPane pane;
pane.setSize(SOME_WIDTH_CONSTANT, SOME_HEIGHT_CONSTANT);