推荐 Java Swing 布局用于两个相同大小的表格和按钮

Recommended Java Swing layouts to use for two same-size tables plus buttons

我正在试验 Swing,我想尝试构建一个 window 的外观和行为如下所示: 绘制所需布局:

本质上,对于任何熟悉 WinForms 的人来说,它应该就像左边的“table”锚定在顶部、左侧和底部,而右边的 table 锚定在顶部、右侧、和底部。

我尝试的第一件事是 FlowLayout,但是 table 无法正确调整 window 的大小。

为了创建两个 table 和它们之间的中央按钮,我试验了 BorderLayout,将 table 放在 EAST 和 [=14= 中] 和 CENTER 区域中的按钮。成功地使两个 table 的大小和大小随着 window 的调整而调整,但是按钮填满了它们之间的整个 space,并且宽度随着调整大小时而改变(我希望它保持不变)。

有什么让我入门的想法吗?

以下是基于GridBagLayout的基础demo:

import java.awt.*;
import javax.swing.*;

public class SwingTestPane extends JPanel {

    public SwingTestPane() {

        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWeights = new double[]{1, 0.0, 1, 0.0};
        gridBagLayout.rowWeights = new double[]{1};
        setLayout(gridBagLayout);

        GridBagConstraints c1 = new GridBagConstraints();
        c1.fill = GridBagConstraints.BOTH;
        c1.gridx = 0;
        c1.gridy = 0;
        JPanel ltPane = new LeftTablePane();
        add(ltPane, c1);

        GridBagConstraints c2 = new GridBagConstraints();
        c2.fill = GridBagConstraints.VERTICAL;
        c2.gridx = 1;
        c2.gridy = 0;

        JPanel lbPane = new LeftButtonsPane();
        add(lbPane, c2);

        GridBagConstraints c3 = new GridBagConstraints();
        c3.fill = GridBagConstraints.BOTH;
        c3.gridx = 2;
        c3.gridy = 0;

        JPanel rtPane = new RightTablePane();
        add(rtPane, c3);

        GridBagConstraints c4 = new GridBagConstraints();
        c4.fill = GridBagConstraints.VERTICAL;
        c4.gridx = 3;
        c4.gridy = 0;

        JPanel rbPane = new RightButtonsPane();
        add(rbPane, c4);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.getContentPane().add(new SwingTestPane());
        frame.pack();
        frame.setVisible(true);
    }
}

class LeftTablePane extends JPanel {

    LeftTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class LeftButtonsPane extends JPanel {

    LeftButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }
}
    
class RightTablePane extends JPanel {

    RightTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class RightButtonsPane extends JPanel {

    RightButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }
}

以下是基于BoxLayout的基础demo:

import java.awt.*;
import javax.swing.*;

public class SwingTestPane extends JPanel {

    public SwingTestPane() {

        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.X_AXIS);
        setLayout(boxLayout);

        JPanel ltPane = new LeftTablePane();
        add(ltPane);

        JPanel lbPane = new LeftButtonsPane();
        add(lbPane);

        JPanel rtPane = new RightTablePane();
        add(rtPane);

        JPanel rbPane = new RightButtonsPane();
        add(rbPane);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.getContentPane().add(new SwingTestPane());
        frame.pack();
        frame.setVisible(true);
    }
}

class LeftTablePane extends JPanel {

    LeftTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class LeftButtonsPane extends JPanel {

    LeftButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(getPreferredSize().width, 0);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(getPreferredSize().width, Integer.MAX_VALUE);
    }
}


class RightTablePane extends JPanel {

    RightTablePane() {
        setBackground(Color.CYAN);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 300);
    }
}

class RightButtonsPane extends JPanel {

    RightButtonsPane() {
        setBackground(Color.YELLOW);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(100, 300);
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(getPreferredSize().width, 0);
    }

    @Override
    public Dimension getMaximumSize() {
        return new Dimension(getPreferredSize().width, Integer.MAX_VALUE);
    }
}