当我调整 JFrame 的大小时,表格(或更具体地说是 scrollPane)会挤压成一个小矩形(保持不变,但高度更像 5-10 像素)。
我还注意到 JFrame 有一个特定的高度阈值,当 JFrame 的高度低于此阈值时,所有 GridBagLayouts 的行为都正确(例如,所有面板的正确宽度和高度)。当高度高于此阈值时,两个右侧面板都会获得额外的宽度,而当降低 JFrame 的高度时,rightTopPanel 比 rightBottomPanel 失去高度的速度要快得多。
当 rightTopPanel 获得最小高度(大约 5-10 像素)时,阈值似乎是点。
JPanel panel = new JPanel(new GridBagLayout());
JPanel rightTopPanel = new JPanel(new GridBagLayout());
panel.add(rightTopPanel, Helper.createGridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));
JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));
tableModel = new DefaultTableModel(0, 2);
JTable table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
rightBottomPanel.add(scrollPane);
panel.add(rightBottomPanel, Helper.createGridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.BOTH));
JPanel leftPanel = new JPanel();
panel.add(leftPanel, Helper.createGridBagConstraints(0, 0, 1, 2, 0.5, 1.0));
add(panel);
Helper.createGridBagConstraints 只是创建 GridBagConstraints 的辅助方法,具有许多“可选”参数。
GridBagConstraints createGridBagConstraints(int gridx, int gridy)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty)
GridBagConstraints createGridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int fill)
编辑:这似乎是 JScrollPane 中的一个问题。如果我不添加它,剩余的面板会正确调整大小。
编辑2:因为,到目前为止,没有人明白我的问题是什么,这里是截图:http: //img155.imageshack.us/img155/8847/badgridbaglayout1.png