I have a GUI built with Java's Swing. I have a JPanel that holds all of my controls:
JPanel rightSidePanel = new JPanel(new GridBagLayout());
rightSidePanel.add(forwardKinematics, new GridBagConstraints());
This works fine. However, I would like to add a tabbed control. Unfortunately, adding the tabs breaks the layout:
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("Forward Kinematics", forwardKinematics);
JPanel rightSidePanel = new JPanel(new GridBagLayout());
rightSidePanel.add(tabs, n开发者_如何学Pythonew GridBagConstraints());
Now, some of the controls in forwardKinematics
are horribly crunched up, instead of expanding to their full size. Is there some way that I'm supposed to specify the controls can expand and take up as much space as they want?
I'm using gridbag layouts.
Here is the code that creates the UI controls that get squished (with irrelevant excerpts removed):
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);
What am I doing wrong here?
Update: I tried using tabs.setPreferredSize()
. That makes the area surrounding the tab bigger (as if I'm adding padding), but the content within them stays squished as ever.
Default constructed GridBagConstraints
uses NONE
for fill
. In your case you probably want to use BOTH
instead.
If you want to use GridBagLayout
, at least read the basic tutorial.
You seem to be missing a LOT of settings you probably want to set on your gridbag constraints. You're setting everything to be in gridy = 2, which puts them all in the same cell.
The gridbaglayout operates like a table with configurable rows and columns.
精彩评论