java - 如何在Java中组合两个网格位置?

标签 java swing form-layout

我想知道如何使用 Java 网格布局组合或合并两个网格位置。我正在使用 java 网格布局(0, 2) 以及 JFrame 和 java 表单组件(如 JLabel 和 JButton)创建一个基本的 java 应用程序。我想合并顶部的两个单元格以在应用程序的中心显示我的标题。

最佳答案

您可以使用GridBagLayout

/*
 * GridBagLayoutDemo.java is a 1.4 application that requires no other files.
 */

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

public class GridBagLayoutDemo {
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        JButton button;
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        if (shouldFill) {
            //natural height, maximum width
            c.fill = GridBagConstraints.HORIZONTAL;
        }

        button = new JButton("Button 1");
        if (shouldWeightX) {
            c.weightx = 0.5;
        }
        c.gridx = 0;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Button 2");
        c.gridx = 1;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Button 3");
        c.gridx = 2;
        c.gridy = 0;
        pane.add(button, c);

        button = new JButton("Long-Named Button 4");
        c.ipady = 40;      //make this component tall
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(button, c);

        button = new JButton("5");
        c.ipady = 0;       //reset to default
        c.weighty = 1.0;   //request any extra vertical space
        c.anchor = GridBagConstraints.PAGE_END; //bottom of space
        c.insets = new Insets(10,0,0,0);  //top padding
        c.gridx = 1;       //aligned with button 2
        c.gridwidth = 2;   //2 columns wide
        c.gridy = 2;       //third row
        pane.add(button, c);
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("GridBagLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

在上面的代码中,长命名按钮 4 跨越 3 列。
引用:-How to use GridBagLayout

关于java - 如何在Java中组合两个网格位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319435/

相关文章:

java - 使用java连接到本地网络设备

java - 如何检查特定字符串是否在其他字符串中多次出现?

java - 如何将 EDT 的结果传回另一个线程?

java - 在运行时更改 FormLayout 对齐方式

java - 使用 windowbuilder 调整表单大小

java - 如何更改默认 Eclipse WTP "Web Resources"动态文件夹

java - 使用 versions-maven-plugin 在我的父 pom.xml 中获取最新版本的插件

Java - OOP 和 MVC

java - 如何停止TimerTask中的对象?

java - 有没有办法将方法包含到表单布局上的响应步骤中