我似乎無法讓我的Java Swing組件正確地一起工作。如何讓Java Swing組件填充可用空間?
我想要做的是有一個JPanel填充JTabbedPane中可用的所有空間。此刻,我的設置如下:
public class Gui extends JFrame {
private final EventBus eventBus = EventBus.getInstance();
private final ToolkitUtil toolkitUtil;
private final Menu menu;
private final InfoBar infoBar;
private final JTabbedPane pane;
...
private void buildLayout() {
GridBagConstraints gbc = new GridBagConstraints();
setJMenuBar(menu);
add(pane, BorderLayout.CENTER);
add(infoBar, BorderLayout.SOUTH);
pane.addTab("Plugins", new PluginPanel());
}
}
public class PluginPanel extends JPanel {
private final JPanel modelPanel;
private final JPanel editorPanel;
public PluginPanel() {
setLayout(new GridBagLayout());
modelPanel = new JPanel(new GridBagLayout());
editorPanel = new JPanel(new GridBagLayout());
buildLayout();
}
private void buildLayout() {
GridBagConstraints gbc = new GridBagConstraints();
modelPanel.setBorder(BorderFactory.createTitledBorder("Models"));
editorPanel.setBorder(BorderFactory.createTitledBorder("Editors"));
gbc.gridx = 0;
gbc.fill = GridBagConstraints.BOTH;
modelPanel.add(new JLabel("test label"), gbc);
add(modelPanel, gbc);
gbc.gridx = 1;
add(editorPanel, gbc);
}
}
這就產生了一個窗口那是我所希望的大小(動態正比於屏幕大小,不包括在上面的代碼)。放置在中間的標籤面板被擴展以填充所需的所有空間,這正是我想要的。但是,我在標籤面板中添加的面板只與其內容一樣大。如果我添加標籤或任何東西,它只會增長到與組件一樣大。我希望它們始終能夠擴展以填充標籤面板。
什麼傳奇!謝謝! – 2011-04-09 09:52:56
這就像一個GridBagLayout的新**譜**!非常感謝! +1! – desperateCoder 2013-05-17 16:24:10