我正在Java Swing中实现一个UI。因此我使用JTabbedPane
。 tabbedPane在启动时没有组件。当我添加一个选项卡到tabbedpane,tabbedpane的宽度增加,当我删除选项卡时,宽度调整到在启动时的宽度。这不应该发生。
选项卡式窗格位于具有网格包布局的JPanel
上。将JTabbedPane的宽度设置为常量
布局代码:
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);
希望有人能帮助!
发布一些代码,显示您的gridbagconstraints。 – basiljames
请参阅此链接:http://stackoverflow.com/questions/2510310/how-to-make-jtabbedpane-autoresize-to-fit-page-dimensions – Vinesh