2010-10-28 35 views
0

我有一個用Java的Swing構建的GUI。我有一個持有我所有控件的JPanel:JTabbedPane的困難(錯誤壓縮內容)

JPanel rightSidePanel = new JPanel(new GridBagLayout()); 
    rightSidePanel.add(forwardKinematics, new GridBagConstraints()); 

這工作正常。但是,我想添加一個選項卡式控件。不幸的是,添加標籤打破了佈局:

JTabbedPane tabs = new JTabbedPane(); 
    tabs.addTab("Forward Kinematics", forwardKinematics); 

    JPanel rightSidePanel = new JPanel(new GridBagLayout()); 
    rightSidePanel.add(tabs, new GridBagConstraints()); 

現在,一些在forwardKinematics控件都被可怕的嘎吱作響起來,而不是擴大到他們的全尺寸。有什麼方法可以指定控件可以擴展並佔用足夠的空間嗎?

我正在使用網格包佈局。

下面是創建得到壓扁的UI控件(與不相關的摘錄刪除)代碼:

panel.add(label, new GridBagConstraints()); 

    GridBagConstraints gbc = new GridBagConstraints(); 

    final DefaultTableModel model = new DefaultTableModel(); 
    model.addColumn("foo"); 
    model.addColumn("bar"); 
    final JTable table = new JTable(model); 
    JScrollPane scrollPane = new JScrollPane(table); 
    table.setFillsViewportHeight(true); 
    gbc = new GridBagConstraints(); 
    gbc.gridy = 2; 
    gbc.ipady = 10; 
    gbc.ipadx = 10; 
    panel.add(scrollPane, gbc); 

    final JComboBox comboBox = new JComboBox(); 

    gbc = new GridBagConstraints(); 
    gbc.gridy = 1; 
    panel.add(comboBox, gbc); 

    JButton makeNewButton = new JButton("Make new from current state"); 
    gbc = new GridBagConstraints(); 
    gbc.gridy = 1; 
    panel.add(makeNewButton, gbc); 

    JButton deleteButton = new JButton("Delete Current Keyframe"); 
    gbc = new GridBagConstraints(); 
    gbc.gridy = 2; 
    panel.add(deleteButton, gbc); 

    JButton playAll = new JButton("Play All"); 
    gbc = new GridBagConstraints(); 
    gbc.gridy = 2; 
    panel.add(playAll, gbc); 

什麼我錯在這裏做什麼?

更新:我試過使用tabs.setPreferredSize()。這使得選項卡周圍的區域更大(好像我正在添加填充),但其中的內容仍然像以前一樣被擠壓。

+0

僅供參考,您不必爲每個項目創建新的GridbagConstraints。如果他們有類似的內容,你可以重用他們。 'add(control,gbc)'方法將複製約束本身。 – 2010-10-28 19:55:13

回答

1

默認構造GridBagConstraints使用NONEfill。在你的情況下,你可能想用BOTH代替。

如果您想使用GridBagLayout,至少請閱讀basic tutorial

+0

我在桌子上加了'BOTH'作爲'fill',但它沒有做任何事情。 – 2010-10-28 20:32:10

+0

也使用兩個標籤窗格。 – 2010-10-28 21:59:17

1

您似乎錯過了很多設置,您可能希望在您的網格包約束上進行設置。你將所有東西都設置爲gridy = 2,這將它們放在同一個單元格中。

gridbaglayout的操作類似於可配置的行和列的表格。