java - 使用 GridBagLayout 使组件跨多行

标签 java swing layout-manager gridbaglayout

我想让蓝色分量填充白色间隙。我尝试使用 gridheight = 2 但没有任何反应。我的看法是,有三个单元格,我希望该组件扩展到不存在的第四个单元格。我怎样才能绕过它?

enter image description here

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUIFrame extends JFrame 
{

public GUIFrame(String title)
{
    super(title);
}

public void init()
{
    setLayout(new GridBagLayout());
    GridBagConstraints gbConstraints = new GridBagConstraints();

    DisplayPanel display = new DisplayPanel();
    ControlPanel control = new ControlPanel();
    GalleryPanel gallery = new GalleryPanel();


    gbConstraints.gridx = 0;
    gbConstraints.gridy = 0;
    gbConstraints.weightx = 0.8;
    gbConstraints.weighty = 0.75;
    gbConstraints.fill=gbConstraints.BOTH;
    add(display,gbConstraints);

    gbConstraints.gridx = 1;
    gbConstraints.gridy = 0;
    gbConstraints.weightx = 0.2;
    gbConstraints.weighty = 0.75;
    gbConstraints.fill=gbConstraints.BOTH;
    add(gallery,gbConstraints);


    gbConstraints.gridx = 0;
    gbConstraints.gridy = 1;
    gbConstraints.weightx = 1;
    gbConstraints.weighty = 0.3;
    gbConstraints.fill=gbConstraints.BOTH;
    add(control,gbConstraints);




    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1600,900);
    setVisible(true);
}


}

最佳答案

调整标准很简单:

   Grid Height = 1       
-----------------------
|   0.7 width   | 0.3 |-----> Grid Height = 2
|   0.7 height  | w   |
----------------| 1.0 |
|   0.7 w 0.3 h |  h  |
-----------------------
        |
    Grid Height = 1

这是一个工作示例:

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

public class GridBagLayoutExample {

    private final int hGap = 5;
    private final int vGap = 5;

    private GridBagConstraints gbc;

    public GridBagLayoutExample () {
        gbc = new GridBagConstraints ();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;   
        gbc.insets = new Insets( hGap, vGap, hGap, vGap ); 
    }

    private void displayGUI () {
        JFrame frame = new JFrame ( "GridBagLayout Example" );
        frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );

        JPanel contentPane = getPanel ( Color.WHITE );
        contentPane.setLayout ( new GridBagLayout () );

        JPanel blackPanel = getPanel ( Color.BLACK );
        addComp ( contentPane, blackPanel, 0, 0, 1, 1
                            , GridBagConstraints.BOTH, 0.7, 0.7 );

        JPanel grayPanel = getPanel ( Color.GRAY );
        addComp ( contentPane, grayPanel, 0, 1, 1, 1
                            , GridBagConstraints.BOTH, 0.7, 0.3 );

        JPanel bluePanel = getPanel ( Color.BLUE );
        addComp ( contentPane, bluePanel, 1, 0, 1, 2
                            , GridBagConstraints.BOTH, 0.3, 1.0 );

        frame.setContentPane ( contentPane );
        frame.pack ();
        frame.setLocationByPlatform ( true );
        frame.setVisible ( true );
    }

    private void addComp(JPanel panel, JComponent comp
                                , int x, int y, int gWidth
                                    , int gHeight, int fill
                                        , double weightx, double weighty) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = gWidth;
        gbc.gridheight = gHeight;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;      

        panel.add(comp, gbc);
    }

    private JPanel getPanel ( Color bColor ) {
        JPanel panel = new JPanel();
        panel.setOpaque ( true );
        panel.setBackground ( bColor );

        return panel;
    }

    public static void main ( String [] args ) {
        Runnable runnable = new Runnable () {
            @Override
            public void run () {
                new GridBagLayoutExample ().displayGUI ();
            }
        };
        EventQueue.invokeLater ( runnable );
    }
}

关于java - 使用 GridBagLayout 使组件跨多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42594962/

相关文章:

java - 如何在 Java 中使用递归从数组中删除

java - 如何在 hibernate 框架中插入新行?

java - Java Swing 组件的基本问题?

java - JOptionPane 在不同的行上有多个输入

java - 如何将按钮的文本调整为按钮的大小 - Java 的 Swing

java - spring 3 在 @value 中使用 bean 和占位符拼写 if-then-else

java - 每次用户单击按钮时从 txt 或 CSV 文件中随机选择记录/行的最佳方法是什么?

java - 在 Java 中生成多个形状......?

java - JTextField:当文本字段已满/自动跳过/自动制表符时聚焦下一个组件

java - 向容器添加多个 jpanels,在它们之间添加空格/边框?