📜  Java摇摆 | GroupLayout 类

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

Java摇摆 | GroupLayout 类

GroupLayout 是一个 LayoutManager,它对组件进行分层分组并将它们排列在一个容器中。分组是通过使用 Group 类的实例来完成的。它通常用于开发 GUI(图形用户界面)构建器,例如 NetBeans IDE 提供的 GUI 构建器 Matisse。 GroupLayout 类支持两种类型的组:

  • 顺序组按顺序排列其子元素,一个接一个。
  • 并行组以不同的方式对齐其子元素。

类的构造函数:

  • GroupLayout(Container host):用于为指定的Container创建GroupLayout。

常用方法:

  1. addLayoutComponent(Component comp, Object cons):通知一个组件已经添加到父容器中。
  2. getHonorsVisibility():返回在调整和定位组件时是否考虑组件可见性。
  3. maximumLayoutSize(Container parent):返回指定容器的最大尺寸。
  4. getLayoutAlignmentX(沿水平轴):它返回沿 x 轴的对齐方式。
  5. minimumLayoutSize(Container parent):返回指定容器的最小尺寸。
  6. getLayoutStyle():返回用于计算组件之间首选间隙的 LayoutStyle。

下面的程序说明了 GroupLayout 类的使用:

  • 以下程序通过将JLabel组件排列在一个JFrame中来说明 GropuLayout 的使用,该 JFrame 的实例类为“ GroupLayoutDemo ”。我们创建了 2 个名为“ headerLabel ”、“ statusLabel ”的 JLabel 组件,并创建了 3 个名为“ btn1 ”、“ btn2 ”、“ btn3 ”的JButton组件,然后使用add()方法将它们添加到 JFrame。我们使用setSize()setVisible()方法设置框架的大小和可见性。使用setLayout()方法设置布局。
Java
// Java Program to illustrate the GroupLayout class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
// creating a class GroupLayoutDemo
public class GroupLayoutDemo {
  
    // Declaration of objects
    // of JFrame class
    private JFrame mainFrame;
  
    // Declaration of objects
    // of JLabel class
    private JLabel headerLabel, statusLabel, msglabel;
  
    // Declaration of objects
    // of JPanel class
    private JPanel controlPanel;
  
    // create a class GroupLayoutDemo
    public GroupLayoutDemo()
    {
          
        // used to prepare GUI
        prepareGUI();
    }
  
    public static void main(String[] args)
    {
          
        // Creating Object of "GroupLayoutDemo" class
        GroupLayoutDemo GroupLayoutDemo = new GroupLayoutDemo();
  
        // to show the group layout demo
        GroupLayoutDemo.showGroupLayoutDemo();
    }
  
    private void prepareGUI()
    {
  
        // Initialization of object
        // "mainframe" of JFrame class.
        mainFrame = new JFrame("Java GroupLayout Examples");
  
        // Function to set the
        // size of JFrame.
        mainFrame.setSize(400, 400);
  
        // Function to set the
        // layout of JFrame.
        mainFrame.setLayout(new GridLayout(3, 1));
  
        // Initialization of object
        // "headerLabel" of JLabel class.
        headerLabel = new JLabel("", JLabel.CENTER);
  
        // Initialization of object
        // "statusLabel" of JLabel class.
        statusLabel = new JLabel("", JLabel.CENTER);
  
        // Function to set the
        // size of JFrame.
        statusLabel.setSize(350, 100);
  
        // to add action WindowListner in JFrame
        mainFrame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent windowEvent)
            {
                System.exit(0);
            }
        });
  
        // Initialization of object
        // "controlPanel" of JPanel class.
        controlPanel = new JPanel();
  
        // Function to set the
        // layout of JFrame.
        controlPanel.setLayout(new FlowLayout());
  
        // Adding Jlabel "headerlabel"
        // on JFrame.
        mainFrame.add(headerLabel);
  
        // Adding JPanel "controlPanel"
        // on JFrame.
        mainFrame.add(controlPanel);
  
        // Adding JLabel "statusLabel"
        // on JFrame.
        mainFrame.add(statusLabel);
  
        // Function to set the visible of JFrame.
        mainFrame.setVisible(true);
    }
      
    private void showGroupLayoutDemo()
    {
  
        // Function to set the text
        // on the header of JFrame.
        headerLabel.setText("Layout in action: GroupLayout");
  
        // Creating Object of
        // "Panel" class
        JPanel panel = new JPanel();
  
        // Function to set the size of JFrame.
        panel.setSize(200, 200);
  
        // Creating Object of
        // "layout" class
        GroupLayout layout = new GroupLayout(panel);
  
        // it used to set Auto
        // Create Gaps
        layout.setAutoCreateGaps(true);
  
        // it used to set Auto
        // Create Container Gaps
        layout.setAutoCreateContainerGaps(true);
  
        // Creating Object
        // of "btn1" class
        JButton btn1 = new JButton("Button 1");
  
        // Creating Object of
        // "btn2" class
        JButton btn2 = new JButton("Button 2");
  
        // Creating Object of "btn3" class
        JButton btn3 = new JButton("Button 3");
  
        // It used to set the
        // Horizontal group
        layout.setHorizontalGroup(layout.createSequentialGroup()
      
        // Adding the JButton "btn1"
        .addComponent(btn1)
     
        // Adding the sequential Group
        .addGroup(layout.createSequentialGroup()
      
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
      
        // Adding the JButton "btn2"
        .addComponent(btn2)
  
        // Adding the JButton "btn3"
        .addComponent(btn3))));
  
        // set the vertical layout group
        layout.setVerticalGroup(layout.createSequentialGroup()
  
        // Adding the JButton "btn1"
        .addComponent(btn1)
  
        // Adding the JButton "btn2"
        .addComponent(btn2)
  
        // Adding the JButton "btn3"
        .addComponent(btn3));
  
        // Function to set the Layout of JFrame.
        panel.setLayout(layout);
  
        // Adding the control panel
        controlPanel.add(panel);
  
        // Function to set the visible of JFrame.
        mainFrame.setVisible(true);
    }
}


Java
// Java Program to illustrate the GroupLayout class
import java.awt.Component;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
 
// creating a class GroupLayoutExample
public class GroupLayoutExample {
     
    // Main Method
    public static void main(String[] args)
    {
 
        // Function to set the Default Look
        // And Feel Decorated of JFrame.
        JFrame.setDefaultLookAndFeelDecorated(true);
 
        // Creating Object of
        // "JFrame" class
        JFrame frame = new JFrame("GroupLayoutExample");
 
        // Function to set the Default
        // Close Operation of JFrame.
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
        // Creating Object of "JLabel" class
        JLabel label = new JLabel("Label:");
 
        // Creating Object of
        // "JTextField" class
        JTextField textField = new JTextField();
 
        // Creating Object of
        // "JCheckBox" class
        JCheckBox checkBox1 = new JCheckBox("CheckBox1");
 
        // Creating Object of "JCheckBox" class
        JCheckBox checkBox2 = new JCheckBox("CheckBox2");
 
        // Creating Object of "JButton" class
        JButton findButton = new JButton("Button 1");
 
        // Creating Object of "JButton" class
        JButton cancelButton = new JButton("Button 2");
 
        // used to set the Border of a checkBox1
        checkBox1.setBorder(BorderFactory.createEmptyBorder(0, 0,
                                                          0, 0));
 
        // used to set the Border of a checkBox2
        checkBox2.setBorder(BorderFactory.createEmptyBorder(0, 0,
                                                          0, 0));
 
        // Creating Object of "GroupLayout" class
        GroupLayout layout = new GroupLayout(frame.getContentPane());
 
        // to get the content pane
        frame.getContentPane().setLayout(layout);
 
        // it used to set Auto Create Gaps
        layout.setAutoCreateGaps(true);
 
        // it used to set Auto Create Container Gaps
        layout.setAutoCreateContainerGaps(true);
 
        // it used to set the horizontal group
        layout.setHorizontalGroup(layout.createSequentialGroup()
 
        // Adding the label
        .addComponent(label)
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the textfield
        .addComponent(textField)
 
        // Adding the Sequential Group
        .addGroup(layout.createSequentialGroup()
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the checkBox1
        .addComponent(checkBox1))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the checkBox2
        .addComponent(checkBox2))))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the findButton
        .addComponent(findButton)
     
        // Adding the CancelButton
        .addComponent(cancelButton)));
 
        layout.linkSize(SwingConstants.HORIZONTAL,
                        findButton, cancelButton);
         
        layout.setVerticalGroup(layout.createSequentialGroup()
         
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(BASELINE)
 
        // Adding the label
        .addComponent(label)
 
        // Adding the textField
        .addComponent(textField)
 
        // Adding the findButton
        .addComponent(findButton))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the sequential Group
        .addGroup(layout.createSequentialGroup()
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(BASELINE)
 
        // Adding the checkBox1
        .addComponent(checkBox1)
 
        // Adding the checkBox2
        .addComponent(checkBox2))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(BASELINE)))
 
        // Adding the CancelButton
        .addComponent(cancelButton)));
 
        frame.pack();
        frame.show();
    }
}


输出:

  • 以下程序通过将JLabel组件排列在一个JFrame中来说明 GropuLayout 的使用,该 JFrame 的实例类为“ GroupLayoutExample ”。我们创建了 1 个JLabel 、 1 个JTextField和 2 个JCheckbox组件。还创建了两个JButton组件,分别为“ FindButton ”、“ CancelButton ”,然后使用 add() 方法将它们添加到 JFrame。使用setLayout()方法设置布局。

Java

// Java Program to illustrate the GroupLayout class
import java.awt.Component;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
 
// creating a class GroupLayoutExample
public class GroupLayoutExample {
     
    // Main Method
    public static void main(String[] args)
    {
 
        // Function to set the Default Look
        // And Feel Decorated of JFrame.
        JFrame.setDefaultLookAndFeelDecorated(true);
 
        // Creating Object of
        // "JFrame" class
        JFrame frame = new JFrame("GroupLayoutExample");
 
        // Function to set the Default
        // Close Operation of JFrame.
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
        // Creating Object of "JLabel" class
        JLabel label = new JLabel("Label:");
 
        // Creating Object of
        // "JTextField" class
        JTextField textField = new JTextField();
 
        // Creating Object of
        // "JCheckBox" class
        JCheckBox checkBox1 = new JCheckBox("CheckBox1");
 
        // Creating Object of "JCheckBox" class
        JCheckBox checkBox2 = new JCheckBox("CheckBox2");
 
        // Creating Object of "JButton" class
        JButton findButton = new JButton("Button 1");
 
        // Creating Object of "JButton" class
        JButton cancelButton = new JButton("Button 2");
 
        // used to set the Border of a checkBox1
        checkBox1.setBorder(BorderFactory.createEmptyBorder(0, 0,
                                                          0, 0));
 
        // used to set the Border of a checkBox2
        checkBox2.setBorder(BorderFactory.createEmptyBorder(0, 0,
                                                          0, 0));
 
        // Creating Object of "GroupLayout" class
        GroupLayout layout = new GroupLayout(frame.getContentPane());
 
        // to get the content pane
        frame.getContentPane().setLayout(layout);
 
        // it used to set Auto Create Gaps
        layout.setAutoCreateGaps(true);
 
        // it used to set Auto Create Container Gaps
        layout.setAutoCreateContainerGaps(true);
 
        // it used to set the horizontal group
        layout.setHorizontalGroup(layout.createSequentialGroup()
 
        // Adding the label
        .addComponent(label)
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the textfield
        .addComponent(textField)
 
        // Adding the Sequential Group
        .addGroup(layout.createSequentialGroup()
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the checkBox1
        .addComponent(checkBox1))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the checkBox2
        .addComponent(checkBox2))))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the findButton
        .addComponent(findButton)
     
        // Adding the CancelButton
        .addComponent(cancelButton)));
 
        layout.linkSize(SwingConstants.HORIZONTAL,
                        findButton, cancelButton);
         
        layout.setVerticalGroup(layout.createSequentialGroup()
         
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(BASELINE)
 
        // Adding the label
        .addComponent(label)
 
        // Adding the textField
        .addComponent(textField)
 
        // Adding the findButton
        .addComponent(findButton))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(LEADING)
 
        // Adding the sequential Group
        .addGroup(layout.createSequentialGroup()
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(BASELINE)
 
        // Adding the checkBox1
        .addComponent(checkBox1)
 
        // Adding the checkBox2
        .addComponent(checkBox2))
 
        // Adding the Parallel Group
        .addGroup(layout.createParallelGroup(BASELINE)))
 
        // Adding the CancelButton
        .addComponent(cancelButton)));
 
        frame.pack();
        frame.show();
    }
}

输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/7/docs/api/javax/swing/GroupLayout.html