想不出一个简单的 4 框 JPanel 布局

Can not figure out a simple 4 box JPanel layout

我已经尝试了几个小时让 JPanel 在 Java 中包含此配置中的这 4 个其他面板(见图)

对我来说似乎很简单,前几天我用 C# 做到了,这很容易。设置位置、宽度、高度,以及某一侧是否锚定,大功告成,我开始喜欢 java 而不是 C,直到我 运行 喜欢这个。

我已经尝试了无数 GridBagLayout 的组合,多个嵌套的 BoxLayout 实例。他们似乎都在做非常 st运行ge 的事情,比如让每个面板都变成一个 4 x 4 的小正方形,或者在它们周围有疯狂的填充,或者那些需要使用 window 重新调整大小的面板,不要。

是否有某种神奇的组合可以实现这一点? null 布局是否进行锚定或百分比尺寸。

我得到的最接近的是带有 GridBagLayout 的底部图像,它在加载时看起来不错,但在您调整 window.

大小时会出现这种情况

这是获取上述图像的代码

class MainPanel extends JPanel {
public MainPanel(){
    this.setBackground(new Color(216,216,216));
    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JPanel topTitle = new JPanel();
    topTitle.setPreferredSize(new Dimension(140, 40));
    topTitle.setMinimumSize(new Dimension(140, 40));
    topTitle.setBackground(new Color(174, 216, 249));
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 0;
    this.add(topTitle,c);

    JPanel mainHeader = new JPanel();
    mainHeader.setPreferredSize(new Dimension(1060, 40));
    mainHeader.setMinimumSize(new Dimension(1060, 40));
    mainHeader.setBackground(Color.WHITE);
    c.gridx = 1;
    c.gridy = 0;
    this.add(mainHeader,c);

    JPanel sideNav = new JPanel();
    sideNav.setPreferredSize(new Dimension(140, 760));
    sideNav.setMinimumSize(new Dimension(140, 760));
    sideNav.setBackground(new Color(110,110,110));
    c.gridx = 0;
    c.gridy = 1;
    this.add(sideNav,c);

    JPanel dataPanel = new JPanel();
    dataPanel.setPreferredSize(new Dimension(1060, 760));
    dataPanel.setMinimumSize(new Dimension(1060, 760));
    dataPanel.setBackground(new Color(216,216,216));
    c.gridx = 1;
    c.gridy = 1;
    this.add(dataPanel,c);
}

}

为了实现这一点,我建议使用 FormLayout

FormLayout 是一个强大、灵活和精确的通用布局管理器。它将组件放置在列和行的网格中,允许指定的组件跨越多个列或行。并非所有 columns/rows 都必须具有相同的 width/height。

注意:在Intellij中使用Windowbuilder in Eclipse or GUI Form自动放置和设置组件属性很好。

最小尺寸的 GUI

GUI 拉得又宽又高

一切都是为了获得适当的调整大小权重和填充值..

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FourPanelLayout {

    private JComponent ui = null;

    FourPanelLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new GridBagLayout());
        // It appears you don't want space around the panels.
        // If not, commment out or remove this line.
        ui.setBorder(new EmptyBorder(4,4,4,4));
        
        // create the panels, each with a transparent image to suggest a size
        JPanel bluePanel = new JPanel();
        bluePanel.setBackground(Color.CYAN);
        bluePanel.add(new JLabel(new ImageIcon(getTransparentImage(40, 20))));
        
        JPanel darkGrayPanel = new JPanel();
        darkGrayPanel.setBackground(Color.DARK_GRAY);
        darkGrayPanel.add(new JLabel(new ImageIcon(getTransparentImage(40, 20))));
        
        JPanel whitePanel = new JPanel();
        whitePanel.setBackground(Color.WHITE);
        whitePanel.add(new JLabel(new ImageIcon(getTransparentImage(40, 20))));

        JPanel grayPanel = new JPanel();
        grayPanel.setBackground(Color.GRAY);
        grayPanel.add(new JLabel(new ImageIcon(getTransparentImage(360, 80))));
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 0.0f;
        gbc.weighty = 0.0f;
        gbc.gridx = 0;
        gbc.gridy = 0;
        
        ui.add(bluePanel, gbc);
        
        gbc.weightx = .5f;
        gbc.gridx = 1;
        ui.add(whitePanel, gbc);
        
        gbc.weighty = .5f;
        gbc.gridy = 1;
        ui.add(grayPanel, gbc);

        gbc.weightx = 0f;
        gbc.gridx = 0;
        //gbc.gridy
        ui.add(darkGrayPanel, gbc);
    }
    
    /* We use transparent images to give panels a natural size. */
    private Image getTransparentImage(int w, int h) {
        return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                FourPanelLayout o = new FourPanelLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}