GridBagLayout行为不正确

时间:2011-07-04 11:52:29

标签: java swing jscrollpane layout-manager gridbaglayout

当我调整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

http://img17.imageshack.us/img17/8779/badgridbaglayout2.png

http://img28.imageshack.us/img28/9336/badgridbaglayout3.png

4 个答案:

答案 0 :(得分:3)

  

表格(或者更具体地说是scrollPane)搜索到一个小矩形(保持不变,但高度更像是5-10像素)。

GridBagLayout将尝试按其首选尺寸绘制组件。当框架收缩时,组件将被涂成“最小尺寸”。因此,有时组件的最小尺寸为0,它只会显示为一个小矩形。

您可以尝试将滚动窗格的最小大小设置为首选大小,也可以使用您可以控制此行为的约束。

阅读How to Use GridBagLayout上的Swing教程中的部分以获取更多信息。

如果您仍有问题,请发布展示问题的SSCCE

答案 1 :(得分:1)

TRy为GridBagConstraints指定ipadx和ipady值。

答案 2 :(得分:1)

我创建了这个程序 -

package pkg;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {
    public TestFrame() {
        JPanel panel = new JPanel(new GridBagLayout());

        JPanel rightTopPanel = new JPanel(new GridBagLayout());
        panel.add(rightTopPanel, new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JPanel rightBottomPanel = new JPanel(new GridLayout(1, 1));

        DefaultTableModel tableModel = new DefaultTableModel(0, 2);
        JTable table = new JTable(tableModel);

        JScrollPane scrollPane = new JScrollPane(table);
        rightBottomPanel.add(scrollPane);

        panel.add(rightBottomPanel, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JPanel leftPanel = new JPanel();
        panel.add(leftPanel, new GridBagConstraints(0, 0, 1, 2, 0.5, 1.0, GridBagConstraints.NORTH, 
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        add(panel);
        setSize(800, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        new TestFrame();
    }
}

它似乎运作良好。你在Help.createGridBagConstraints()方法中放了什么?似乎可能存在问题所在。

答案 3 :(得分:1)

唯一的解决方案是根本不使用任何布局并手动定位/调整组件大小,因为Swing中内置布局的 none 已正确实现。