如何制作具有六行两列的Java GridBagLayout

Mad*_*tha 0 java user-interface swing gridbaglayout

我对Java GridBagLayout感到厌倦。我想创建一个面板,如下图所示,但是我听不懂。我无法放置左侧面板,也无法将面板加长。设置gridWidth时,宽度不会增加。我不能为此使用任何GUI构建器。我想获得如下图所示的布局。

在此处输入图片说明

这是不成功的代码:

public class gui3 extends Frame{
    Panel p1,p2,p3,p4,p5,p6,p7,pmain;
    gui3(){

    setVisible(true);
    setSize(500,500);
    setTitle(" Calculator ");


        GridBagLayout gb1=new GridBagLayout();
        GridBagConstraints gbc=new GridBagConstraints();
        setLayout(gb1);

        p1=new Panel();
        p1.setBackground(Color.BLACK);
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.gridwidth=3;
        //gbc.weightx =0.5;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p1,gbc);

        p2=new Panel();
        p2.setBackground(Color.BLUE);
        gbc.gridx=0;
        gbc.gridy=1;
        gbc.gridwidth=2;
       // gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p2,gbc);

        p3=new Panel();
        p3.setBackground(Color.GREEN);
        gbc.gridx=0;
        gbc.gridy=2;
        gbc.gridwidth=2;
      //  gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p3,gbc);

        p4=new Panel();
        p4.setBackground(Color.cyan);
        gbc.gridx=0;
        gbc.gridy=3;
        gbc.gridwidth=2;
      //  gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p4,gbc);

        p5=new Panel();
        p5.setBackground(Color.RED);
        gbc.gridx=0;
        gbc.gridy=4;
        gbc.gridwidth=2;
     //   gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p5,gbc);

        p6=new Panel();
        p6.setBackground(Color.pink);
        gbc.gridx=0;
        gbc.gridy=5;
        gbc.gridwidth=2;
       // gbc.weightx = 1;
       // gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p6,gbc);

        p7=new Panel();
        p7.setBackground(Color.yellow);
        gbc.gridx=6;
        gbc.gridy=0;
        gbc.gridheight=6;
     //   gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p7,gbc);


    }

}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 5

你的意思是...

布局

因此,基本上,您可以使用GridBagConstraints#gridheight(或gridwidth)设置组件将跨越的网格单元数

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

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

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(4, 4, 4, 4);
                gbc.gridx = 0;
                gbc.weightx = 1;
                gbc.gridy = 0;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                frame.add(createPane(Color.RED), gbc);
                gbc.gridy++;
                frame.add(createPane(Color.GREEN), gbc);
                gbc.gridy++;
                frame.add(createPane(Color.BLUE), gbc);
                gbc.gridy++;
                frame.add(createPane(Color.CYAN), gbc);
                gbc.gridy++;
                frame.add(createPane(Color.MAGENTA), gbc);
                gbc.gridy++;
                frame.add(createPane(Color.ORANGE), gbc);
                gbc.gridy++;
                frame.add(createPane(Color.PINK), gbc);

                gbc.gridx++;
                gbc.weightx = 0;
                gbc.gridy = 0;
                gbc.weighty = 1;
                gbc.gridheight = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.VERTICAL;
                frame.add(createPane(Color.YELLOW), gbc);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public JPanel createPane(Color color) {
        JPanel pane = new JPanel(){ 

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

        };
        pane.setBackground(color);
        return pane;
    }

}
Run Code Online (Sandbox Code Playgroud)

看看如何使用GridBagLayout了解更多详细信息

  • 当可用空间小于组件的首选大小时,会发生这种情况。真正避免这种情况的唯一方法是将组件放置在滚动窗格中 (2认同)