我有一个用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()
。这使得选项卡周围的区域更大(好像我正在添加填充),但其中的内容仍然像以前一样被挤压。
仅供参考,您不必为每个项目创建新的GridbagConstraints。如果他们有类似的内容,你可以重用他们。 'add(control,gbc)'方法将复制约束本身。 – 2010-10-28 19:55:13