📜  Java AWT | GridBagLayout 类

📅  最后修改于: 2022-05-13 01:54:42.195000             🧑  作者: Mango

Java AWT | GridBagLayout 类

GridBagLayout 类是一个灵活的布局管理器。它用于水平、垂直或沿基线对齐组件。它不需要相同大小的组件。每个 GridBagLayout 对象管理一个矩形网格的单元格,动态的每个组件占据一个或多个单元格,称为它的显示区域。 GridBagLayout 组件与 GridBagConstraints 的实例相关联。这些约束用于定义组件的显示区域及其位置。除了它的约束对象之外,GridBagLayout 还考虑每个组件的最小和首选大小,以确定组件的大小。 GridBagLayout 组件也排列在矩形网格中,但可以有不同的大小,并且可以占据多行或多列。

构造函数:

  • GridBagLayout():用于创建网格包布局管理器。

常用方法:

  • removeLayoutComponent(Component cmp):从此布局中移除指定的组件。
  • getLayoutAlignmentY(Container p):返回沿 y 轴的对齐方式。
  • addLayoutComponent(Component cmp, Object cons):将指定名称的指定组件添加到布局中。
  • toString():返回此网格包布局值的字符串表示形式。
  • getLayoutAlignmentX(Container p):返回沿 x 轴的对齐方式。
  • getConstraints(Component cmp):获取指定组件的约束。
  • maximumLayoutSize(Container tar):给定指定目标容器中的组件,返回此布局的最大尺寸。
  • minimumLayoutSize(Container par):确定使用此网格包布局的父容器的最小尺寸。

下面的程序说明了 GridBagLayout 类:

  • 程序1:下面的程序将几个行和列组件排列在一个JFrame中,其实例类命名为“ Gridbagdemo ”。我们创建了 4 个名为“ Java ”、“ layout ”、“ manager ”、“ demo ”的JButton组件,然后通过add()方法将它们添加到JFrame 。我们通过setSize()setVisible()方法设置框架的大小和可见性。布局由方法setLayout()设置。
Java
// Java program to demonstrate GridBagLayout class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;
 
// class extends JFrame
public class GridbagDemo extends JFrame {
 
    GridbagDemo()
    {
 
        // Function to set title of JFrame.
        setTitle("GridBagLayoutDemo");
 
        // Creating Object of Jpanel class
        JPanel p = new JPanel();
 
        // set the layout
        p.setLayout(new GridBagLayout());
 
        // creates a constraints object
        GridBagConstraints c = new GridBagConstraints();
 
        // insets for all components
        c.insets = new Insets(2, 2, 2, 2);
 
        // column 0
        c.gridx = 0;
 
        // row 0
        c.gridy = 0;
 
        // increases components width by 10 pixels
        c.ipadx = 15;
 
        // increases components height by 50 pixels
        c.ipady = 50;
 
        // constraints passed in
        p.add(new JButton("Java Swing"), c);
 
        // column 1
        c.gridx = 1;
 
        // increases components width by 70 pixels
        c.ipadx = 90;
 
        // increases components height by 40 pixels
        c.ipady = 40;
 
        // constraints passed in
        p.add(new JButton("Layout"), c);
 
        // column 0
        c.gridx = 0;
 
        // row 2
        c.gridy = 1;
 
        // increases components width by 20 pixels
        c.ipadx = 20;
 
        // increases components height by 20 pixels
        c.ipady = 20;
 
        // constraints passed in
        p.add(new JButton("Manager"), c);
 
        // increases components width by 10 pixels
        c.ipadx = 10;
 
        // column 1
        c.gridx = 1;
 
        // constraints passed in
        p.add(new JButton("Demo"), c);
 
        // Creating Object of "wndcloser"
        // class of windowlistener
        WindowListener wndCloser = new WindowAdapter() {
 
            public void windowClosing(WindowEvent e)
            {
 
                // exit the system
                System.exit(0);
            }
        };
 
        // add the actionwindowlistener
        addWindowListener(wndCloser);
 
        // add the content
        getContentPane().add(p);
 
        // Function to set size of JFrame.
        setSize(600, 400);
 
        // Function to set visibility of JFrame.
        setVisible(true);
    }
 
    // Main Method
    public static void main(String[] args)
    {
 
        // calling the constructor
        new GridbagDemo();
    }
}


Java
// Java program to demonstrate GridBagLayout class.
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
  
// Constructor of GridBagLayout class.
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 condition
    if (RIGHT_TO_LEFT) {
 
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }
 
    // Declaration of objects of JButton class
    JButton button;
 
    // set the layout
    pane.setLayout(new GridBagLayout());
 
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
 
    // if condition
    if (shouldFill) {
 
        // natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
    }
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 1");
 
    // if condition
    if (shouldWeightX) {
        c.weightx = 0.5;
    }
 
    // column 0
    c.gridx = 0;
 
    // row 0
    c.gridy = 0;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 2");
 
    // column 1
    c.gridx = 1;
 
    // row 0
    c.gridy = 0;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 3");
 
    // column 1
    c.gridx = 2;
 
    // row 0
    c.gridy = 0;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Long-Named Button 4");
 
    // increases components height by 40 pixels
    c.ipady = 40;
 
    // column width 0
    c.weightx = 0.0;
 
    // row width 3
    c.gridwidth = 3;
 
    // column 1
    c.gridx = 0;
 
    // row 1
    c.gridy = 1;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 5");
 
    // increases components height by 0 pixels
    c.ipady = 0;
 
    // request any extra vertical space
    c.weighty = 1.0;
 
    // bottom of space
    c.anchor = GridBagConstraints.PAGE_END;
 
    // top padding
    c.insets = new Insets(10, 0, 0, 0);
 
    // column 2
    c.gridx = 1;
 
    // 2 columns wide
    c.gridwidth = 2;
 
    // row 3
    c.gridy = 2;
 
    // Adding JButton "button" on JFrame.
    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()
{
 
    // to set a Jframe default
    JFrame.setDefaultLookAndFeelDecorated(true);
 
    // Create and set up the window.
    JFrame frame = new JFrame("GridBagLayoutDemo");
 
    // Function to close the operation of JFrame.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    // adding the content pane.
    addComponentsToPane(frame.getContentPane());
 
    // Display the window.
    frame.pack();
 
    // Function to set visible status of JFrame.
    frame.setVisible(true);
}
 
    // Main Method
    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();
            }
        });
    }
}


输出:

  • 程序2:下面的程序将几个行和列组件排列在一个JFrame中,其实例类命名为“ Gridbagdemo ”。我们创建了 5 个JButton组件,然后通过add()方法将它们添加到JFrame中。我们通过 setTitle、 setSize()setVisible()方法设置框架的标题、大小和可见性。布局由方法setLayout()设置。

Java

// Java program to demonstrate GridBagLayout class.
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
  
// Constructor of GridBagLayout class.
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 condition
    if (RIGHT_TO_LEFT) {
 
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }
 
    // Declaration of objects of JButton class
    JButton button;
 
    // set the layout
    pane.setLayout(new GridBagLayout());
 
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
 
    // if condition
    if (shouldFill) {
 
        // natural height, maximum width
        c.fill = GridBagConstraints.HORIZONTAL;
    }
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 1");
 
    // if condition
    if (shouldWeightX) {
        c.weightx = 0.5;
    }
 
    // column 0
    c.gridx = 0;
 
    // row 0
    c.gridy = 0;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 2");
 
    // column 1
    c.gridx = 1;
 
    // row 0
    c.gridy = 0;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 3");
 
    // column 1
    c.gridx = 2;
 
    // row 0
    c.gridy = 0;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Long-Named Button 4");
 
    // increases components height by 40 pixels
    c.ipady = 40;
 
    // column width 0
    c.weightx = 0.0;
 
    // row width 3
    c.gridwidth = 3;
 
    // column 1
    c.gridx = 0;
 
    // row 1
    c.gridy = 1;
 
    // Adding JButton "button" on JFrame.
    pane.add(button, c);
 
    // Initialization of object
    // "button" of JButton class.
    button = new JButton("Button 5");
 
    // increases components height by 0 pixels
    c.ipady = 0;
 
    // request any extra vertical space
    c.weighty = 1.0;
 
    // bottom of space
    c.anchor = GridBagConstraints.PAGE_END;
 
    // top padding
    c.insets = new Insets(10, 0, 0, 0);
 
    // column 2
    c.gridx = 1;
 
    // 2 columns wide
    c.gridwidth = 2;
 
    // row 3
    c.gridy = 2;
 
    // Adding JButton "button" on JFrame.
    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()
{
 
    // to set a Jframe default
    JFrame.setDefaultLookAndFeelDecorated(true);
 
    // Create and set up the window.
    JFrame frame = new JFrame("GridBagLayoutDemo");
 
    // Function to close the operation of JFrame.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    // adding the content pane.
    addComponentsToPane(frame.getContentPane());
 
    // Display the window.
    frame.pack();
 
    // Function to set visible status of JFrame.
    frame.setVisible(true);
}
 
    // Main Method
    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();
            }
        });
    }
}

输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https: Java/awt/GridBagLayout.html