java - 从网格布局到GridBagLayout

标签 java swing layout

我对 Java Swing 比较陌生,我在理解网格布局如何做某些事情时遇到了一些困难,如果它们不能,那么据称功能更强大的 gridbag 布局如何做到这一点。

这是我尝试使用网格布局的程序

import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;
public class Swing24
{
public static void main(String[] args)
{
JFrame f1= new JFrame("Grid Layout Test");

f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setResizable(true);
f1.setLocation(500,200);
f1.setSize(600,600);

JPanel p1 = new JPanel();
p1.setBackground(Color.black);
f1.add(p1);


JButton b1= new JButton("Button 1");
b1.setBackground(Color.white);

JButton b2= new JButton("Button 2");
b2.setBackground(Color.white);

JButton b3= new JButton("Button 3");
b3.setBackground(Color.white);

JLabel lb1=new JLabel(" Label 1");
lb1.setForeground(Color.orange);
//lb1.setOpaque(true);
lb1.setBackground(Color.yellow);

JLabel lb2=new JLabel(" Label 2");
lb2.setBackground(Color.orange);
lb2.setOpaque(true);


GridLayout glm1=new GridLayout(2,3,0,0); 
p1.setLayout(glm1);

p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(lb1);
p1.add(lb2);


f1.setVisible(true);

}
}

上面的程序允许我将容器分为 2 行和 3 列。基本上我可以使用网格布局将容器分为 m 行和 n 列。但它连续添加组件(按钮和标签)。

问题1:如何在大小为(10,10)的网格中直接向单元格(4,3)添加按钮? 问题2:一个按钮可以在网格布局中占据多个单元格吗?

如果上述任何一个问题的答案都不可能,那么 gridbag 布局如何帮助解决问题。 我尝试使用带有按钮的网格袋布局。但它被放置在中心!比如说,我怎样才能将它放置到容器中的单元格(4,3)中,该容器可以分为大小(10,10)<10行和10列>

最佳答案

1) 您无法将组件添加到特定单元格,但在 that question 中您可以为此找到某种技巧。

2)这是另一个trick单元格内有嵌套的 lyout,用于合并。

借助GridBagLayout,您可以做所有您想做的事情。观看GridBagConstraints它可以帮助您正确布局组件。

查看GridBagConstraints的属性:

gridwidthgridheightgridxgridyanchor

但是,如果您只想向容器添加一个组件,则需要一些技巧来处理 cell(4,3) 周围的空白区域。

另请阅读 GridBagLayout 的教程.

编辑:你可以尝试这样的事情

public class Form extends JFrame {

    public Form() {
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;
        for(int i =0;i<10;i++){
            c.gridx = i;
            for(int j =0;j<10;j++){
                c.gridy = j;
                if(i == 3 && j == 2){
                    c.fill = GridBagConstraints.NONE;
                    getContentPane().add(new JButton("btn"),c);
                } else {
                    c.fill = GridBagConstraints.BOTH;
                    JPanel p = new JPanel();
                    p.setBorder(BorderFactory.createLineBorder(Color.red));
                    getContentPane().add(p,c);
                }
            }
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }


    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Form().setVisible(true);
            }
        });
    }   
}

enter image description here

EDIT2:它不是真正的单元格(4,3),但比例相同

public Form() {
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 0.2;
    c.weighty = 0.3;
    c.fill = GridBagConstraints.BOTH;
    getContentPane().add(new JLabel(" "),c);

    c.gridx++;
    c.gridy++;
    c.fill = GridBagConstraints.NONE;
    getContentPane().add(new JButton("btn"),c);

    c.weightx = 0.7;
    c.weighty = 0.6;
    c.gridx++;
    c.gridy++;
    c.fill = GridBagConstraints.BOTH;
    getContentPane().add(new JLabel(" "),c);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
}

或真实单元格(4,3),但组件数量多于 3 个且少于 100:

public Form() {
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1;
    c.weighty = 1;
    for(int i =0;i<2;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridx++;
    }

    for(int i =0;i<3;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridy++;
    }

    c.gridx = 3;
    c.gridy = 4;
    getContentPane().add(new JButton("btn"),c);

    for(int i =0;i<7;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridx++;
    }

    for(int i =0;i<6;i++){
        getContentPane().add(new JLabel(" "),c);
        c.gridy++;
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
}

关于java - 从网格布局到GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20194084/

相关文章:

java - 加工。粒子系统-如何让粒子一颗一颗进来

java - 升级到 javaserver faces。轻松的任务?

java - 如何将 1D 数组转换为 3D 数组?

css - 移动设备上 Web 应用程序的布局选项

java - 安装为 Windows 服务时使用远程调试 (jdwp) 启动 Tomcat

java - 将 JLabel 放置在 JFrame 的左上角

java - 数据库更新时使用 AbstractTableModel 更新 JTable

java - 使用缓冲区策略获取图形对象进行绘制

android - 如果我想让我的应用程序在所有 Android 设备上运行,是否有必要创建 "small, normal, large and xlarge android xml layouts"文件夹

css - 使用 css 响应式 div 宽度和高度