我有一个用 Java 的 Swing 构建的 GUI。我有一个 JPanel 来保存我的所有控件:
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
中的一些控件严重压缩,而不是扩展到其完整尺寸。有什么方法可以指定控件可以扩展并占用所需的空间吗?
我正在使用 gridbag 布局。
下面是创建被压缩的 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
使用 NONE
进行填充
。在您的情况下,您可能想使用 BOTH
来代替。
如果您想使用GridBagLayout
,至少阅读basic tutorial .
关于java - JTabbedPane 出现问题(错误地压缩内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4045037/