Swing - GroupLayout 類



介紹

GroupLayout 類以層次結構對元件進行分組,以便將它們放置在容器中。

類宣告

以下是 javax.swing.GroupLayout 類的宣告:

public class GroupLayout
   extends Object
      implements LayoutManager2

欄位

以下是 javax.swing.GroupLayout 類的欄位:

  • static int DEFAULT_SIZE - 指示對於特定範圍值,應使用元件或間隙的大小。

  • static int PREFERRED_SIZE - 指示對於特定範圍值,應使用元件或間隙的首選大小。

類建構函式

序號 建構函式 & 描述
1

GroupLayout(Container host)

為指定的容器建立一個 GroupLayout。

類方法

序號 方法 & 描述
1

void addLayoutComponent(Component component, Object constraints)

通知已將元件新增到父容器。

2

void addLayoutComponent(String name, Component component)

通知已將元件新增到父容器。

3

GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)

建立並返回一個 ParallelGroup,該組沿基線對齊其元素。

4

GroupLayout.ParallelGroup createParallelGroup()

建立並返回一個對齊方式為 Alignment.LEADING 的 ParallelGroup。

5

GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment)

建立並返回一個具有指定對齊方式的 ParallelGroup。

6

GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment, boolean resizable)

建立並返回一個具有指定對齊方式和調整大小行為的 ParallelGroup。

7

GroupLayout.SequentialGroup createSequentialGroup()

建立並返回一個 SequentialGroup。

8

boolean getAutoCreateContainerGaps()

如果容器與容器邊界的元件之間的間隙是自動建立的,則返回 true。

9

boolean getAutoCreateGaps()

如果元件之間的間隙是自動建立的,則返回 true。

10

boolean getHonorsVisibility()

返回在調整元件大小和定位元件時是否考慮元件可見性。

11

float getLayoutAlignmentX(Container parent)

返回沿 x 軸的對齊方式。

12

float getLayoutAlignmentY(Container parent)

返回沿 y 軸的對齊方式。

13

LayoutStyle getLayoutStyle()

返回用於計算元件之間首選間隙的 LayoutStyle。

14

void invalidateLayout(Container parent)

使佈局無效,表明如果佈局管理器已快取資訊,則應將其丟棄。

15

void layoutContainer(Container parent)

佈局指定的容器。

16

void linkSize(Component... components)

強制指定的元件具有相同的大小,而不管其首選、最小或最大大小如何。

17

void linkSize(int axis, Component... components)

強制指定的元件沿指定的軸具有相同的大小,而不管其首選、最小或最大大小如何。

18

Dimension maximumLayoutSize(Container parent)

返回指定容器的最大尺寸。

19

Dimension minimumLayoutSize(Container parent)

返回指定容器的最小尺寸。

20

Dimension preferredLayoutSize(Container parent)

返回指定容器的首選尺寸。

21

void removeLayoutComponent(Component component)

通知已從父容器中刪除元件。

22

void replace(Component existingComponent, Component newComponent)

用新元件替換現有元件。

23

void setAutoCreateContainerGaps(boolean autoCreateContainerPadding)

設定是否應自動建立容器與接觸容器邊界的元件之間的間隙。

24

void setAutoCreateGaps(boolean autoCreatePadding)

設定是否應自動建立元件之間的間隙。

25

void setHonorsVisibility(boolean honorsVisibility)

設定在調整元件大小和定位元件時是否考慮元件可見性。

26

void setHonorsVisibility(Component component, Boolean honorsVisibility)

設定是否考慮元件的可見性以進行調整大小和定位。

27

void setHorizontalGroup(GroupLayout.Group group)

設定沿水平軸定位和調整元件大小的組。

28

void setLayoutStyle(LayoutStyle layoutStyle)

設定用於計算元件之間首選間隙的 LayoutStyle。

29

void setVerticalGroup(GroupLayout.Group group)

設定沿垂直軸定位和調整元件大小的組。

30

String toString()

返回此 GroupLayout 的字串表示形式。

繼承的方法

此類繼承自以下類:

  • java.lang.Object

GroupLayout 示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > SWING > com > tutorialspoint > gui >

SwingLayoutDemo.java

package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingLayoutDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingLayoutDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();  
      swingLayoutDemo.showGroupLayoutDemo();       
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        
      statusLabel.setSize(350,100);
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showGroupLayoutDemo(){
      headerLabel.setText("Layout in action: GroupLayout");      
      JPanel panel = new JPanel();
      
      // panel.setBackground(Color.darkGray);
      panel.setSize(200,200);
      GroupLayout layout = new GroupLayout(panel);
      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);
      
      JButton btn1 = new JButton("Button 1");
      JButton btn2 = new JButton("Button 2");
      JButton btn3 = new JButton("Button 3");

      layout.setHorizontalGroup(layout.createSequentialGroup()
         .addComponent(btn1)
         .addGroup(layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(
         GroupLayout.Alignment.LEADING)
         .addComponent(btn2)
         .addComponent(btn3))));
   
      layout.setVerticalGroup(layout.createSequentialGroup()
         .addComponent(btn1)
         .addComponent(btn2)
         .addComponent(btn3));
      
      panel.setLayout(layout);        
      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }
}

使用命令提示符編譯程式。轉到 D:/ > SWING 並鍵入以下命令。

D:\SWING>javac com\tutorialspoint\gui\SwingLayoutDemo.java

如果未發生錯誤,則表示編譯成功。使用以下命令執行程式。

D:\SWING>java com.tutorialspoint.gui.SwingLayoutDemo

驗證以下輸出。

SWING GroupLayout
swing_layouts.htm
廣告