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

如何在Java中组合两个网格位置?,java,swing,form-layout,Java,Swing,Form Layout,我想知道如何使用Java网格布局组合或合并两个网格位置。我正在使用java网格布局0、2以及JFrame和java表单组件(如JLabel和JButton)创建一个基本的java应用程序。我希望将顶部的两个单元格合并在一起,以便在应用程序的中心显示我的标题 使布局只有3行和一列 然后在第一行添加标题标签 在第二行和第三行中添加一个面板,并将其布局设置为网格,其中包含两个coulmn和一行使布局仅包含三行和一列 然后在第一行添加标题标签 在第二行和第三行添加一个面板,并将其布局设置为grid,其中

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

使布局只有3行和一列 然后在第一行添加标题标签
在第二行和第三行中添加一个面板,并将其布局设置为网格,其中包含两个coulmn和一行

使布局仅包含三行和一列 然后在第一行添加标题标签
在第二行和第三行添加一个面板,并将其布局设置为grid,其中有两个Coulmn和一行

您可以使用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列。
参考:-

您可以使用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列。
参考:-

GridLayout不可能,但您可以使用GridBagLayout。我可以在一个java类中使用多个网格布局吗?是的,像JFrame或JPanel这样的每个容器都有自己的布局管理器。@Ryan,别忘了接受和回答,这样人们就知道问题已经解决了。我通过将文本的部分放在一个网格位置、另一半和下一个网格位置来显示标题。这适用于网格布局。网格布局是不可能的,但您可以改用GridBagLayout。我可以在一个java类中使用多个网格布局吗?是的,像JFrame或JPanel这样的每个容器都有自己的布局管理器。@Ryan,别忘了接受和回答,这样人们就知道问题已经解决了。我通过将文本的部分放在一个网格位置、另一半和下一个网格位置来显示标题。这适用于网格布局。有趣的解决方案。如果您觉得有帮助,请将问题设置为solvedinteresting解决方案。如果您觉得有帮助,请将问题设置为Solved此解决方案有效,但我不完全理解网格袋布局。@Ryan,但我不完全理解网格袋布局。-在给出答案并回答后的6分钟内,您没有时间阅读教程。因此,请花时间阅读教程。然后,如果您仍然不明白,可以问一个具体的问题。这个解决方案有效,但我不完全理解网格袋布局。@Ryan,但我不完全理解网格袋布局。-在给出答案和您回复后的6分钟内,您没有时间阅读教程。因此,请花时间阅读教程。如果你仍然不明白,你可以问一个具体的问题。