2010-10-28 64 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的操作类似于可配置的行和列的表格。