Java : 分割屏幕

Java : divide the screen

我尝试做一个简单的秋千window,但是布局并不容易... 我的意思是我只想要一个有 3 个面板的 window :

但我无法成功得到我想要的。我使用了 gridLayout(3,1) 但无法指定高度。

public class Window extends JFrame implements Serializable  {
private JPanel _header;
private JPanel _content;
private JPanel _footer;

public Window() {
    GridLayout grid = new GridLayout(3,1);
    setLayout(grid);

    _header = new JPanel();
    _header.setBackground(Color.GRAY);
    getContentPane().add(_header);

    _content = new JPanel();
    JScrollPane jsp = new JScrollPane(_content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    getContentPane().add(jsp);

    _footer = new JPanel();
    _footer.setBackground(Color.GRAY);
    getContentPane().add(_footer);
    pack();
    validate();

    setTitle("Chat client");
    setVisible(true);
    setSize(500, 500);
    setLocationRelativeTo(null);
}

你能帮帮我吗? 最好的问候

GridLayout 始终均匀分布。您可以改为使用 GridBagLayout,这是所有 Java 布局管理器中最邪恶的。我给了它们 "weights" 的 20、60、20,这样您就可以看到哪些值是哪些。你可以很容易地使用 2、6、2,没关系,它只是一个比率。查看 GridBagLayout tutorial 了解更多信息。

示例

public class Window extends JFrame implements Serializable {
    private JPanel _header;
    private JPanel _content;
    private JPanel _footer;

    public Window() {
        GridBagLayout grid = new GridBagLayout();
        setLayout(grid);

        _header = new JPanel();
        _header.setBackground(Color.GRAY);
        // <=== add with constraints here
        getContentPane().add(_header, new GridBagConstraints(0, 0, 1, 1, 1, 20, GridBagConstraints.BASELINE, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        _content = new JPanel();
        JScrollPane jsp = new JScrollPane(_content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        // <=== add with constraints here
        getContentPane().add(jsp, new GridBagConstraints(0, 1, 1, 1, 1, 60, GridBagConstraints.BASELINE, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        _footer = new JPanel();
        _footer.setBackground(Color.GRAY);
        // <=== add with constraints here
        getContentPane().add(_footer, new GridBagConstraints(0, 2, 1, 1, 1, 20, GridBagConstraints.BASELINE, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        pack();
        validate();

        setTitle("Chat client");
        setVisible(true);
        setSize(500, 500);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Window();
            }
        });
    }
}

截图

GridBagLayout可以按比例分割垂直或水平space。

这是一个示例,在 window 的前 20% 显示红色 JPanel,在中间 60% 显示绿色 JPanel,在中间 60% 显示蓝色 JPanel 后 20%:

    JFrame window = new JFrame();

    window.setLayout(new GridBagLayout());

    JPanel top = new JPanel(), middle = new JPanel(), bottom = new JPanel();
    top.setBackground(Color.red);
    middle.setBackground(Color.green);
    bottom.setBackground(Color.blue);

    GridBagConstraints c = new GridBagConstraints();
    // we want the layout to stretch the components in both directions
    c.fill = GridBagConstraints.BOTH;
    // if the total X weight is 0, then it won't stretch horizontally.
    // It doesn't matter what the weight actually is, as long as it's not 0,
    // because the grid is only one component wide
    c.weightx = 1; 

    // Vertical space is divided in proportion to the Y weights of the components
    c.weighty = 0.2;
    c.gridy = 0;
    window.add(top, c);
    // It's fine to reuse the constraints object; add makes a copy.
    c.weighty = 0.6;
    c.gridy = 1;
    window.add(middle, c);
    c.weighty = 0.2;
    c.gridy = 2;
    window.add(bottom, c);


    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

结果: