无法弄清楚一个简单的4盒JPanel布局

时间:2016-08-31 04:42:12

标签: java swing layout-manager gridbaglayout

我已经花了好几个小时在Java中获得JPanel以包含这个配置中的这4个其他面板(见图片)

  • 蓝框不应该改变大小。
  • 白盒不应该改变高度,但可以扩大。
  • 深灰色的盒子永远不应该改变宽度,但是可以变得更高。
  • 浅灰色的盒子可以变得更高或更宽。

对我来说似乎很简单,前几天我在C#中做到了,这是轻而易举的事。设置位置,宽度,高度,以及某一方是否被锚定,繁荣完成,我开始喜欢java而不是C,直到我遇到这个。

我尝试了GridBagLayout个多个嵌套BoxLayout个实例的无数组合。它们似乎做了很奇怪的事情,比如让每个面板都是一个4 x 4的小方块,或者周围有疯狂的填充物,或者需要用窗口重新调整尺寸的那些,不要。

是否有某种神奇的组合可以实现这一目标? null布局是否执行锚定或百分比维度。

我最接近的是带有GridBagLayout的底部图片,它在加载时看起来不错,但是在重新调整窗口大小时会这样做。

enter image description here

enter image description here

以下是获得上述图片的代码

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);
}

}

2 个答案:

答案 0 :(得分:4)

最小尺寸的GUI

GUI at minimum size

GUI拉得越来越宽高

GUI stretched wider & taller

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

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);
    }
}

答案 1 :(得分:3)

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

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

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

enter image description here