Swing - GridLayout 類



簡介

GridLayout 類將元件排列在矩形網格中。

類宣告

以下是 java.awt.GridLayout 類的宣告:

public class GridLayout
   extends Object
      implements LayoutManager, Serializable

類建構函式

序號 建構函式 & 描述
1

GridLayout()

建立一個網格佈局,預設情況下每個元件一列,一行。

2

GridLayout(int rows, int cols)

建立一個具有指定行數和列數的網格佈局。

3

GridLayout(int rows, int cols, int hgap, int vgap)

建立一個具有指定行數和列數的網格佈局。

類方法

序號 方法 & 描述
1

void addLayoutComponent(String name, Component comp)

將指定的元件及其名稱新增到佈局中。

2

int getColumns()

獲取此佈局中的列數。

3

int getHgap()

獲取元件之間的水平間隙。

4

int getRows()

獲取此佈局中的行數。

5

int getVgap()

獲取元件之間的垂直間隙。

6

void layoutContainer(Container parent)

使用此佈局佈置指定的容器。

7

Dimension minimumLayoutSize(Container parent)

使用此網格佈局確定容器引數的最小大小。

8

Dimension preferredLayoutSize(Container parent)

使用此網格佈局確定容器引數的首選大小。

9

void removeLayoutComponent(Component comp)

從佈局中刪除指定的元件。

10

void setColumns(int cols)

將此佈局中的列數設定為指定值。

11

void setHgap(int hgap)

將元件之間的水平間隙設定為指定值。

12

void setRows(int rows)

將此佈局中的行數設定為指定值。

13

void setVgap(int vgap)

將元件之間的垂直間隙設定為指定值。

14

String toString()

返回此網格佈局值的字串表示形式。

繼承的方法

此類繼承自以下類:

  • java.lang.Object

GridLayout 示例

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

SwingLayoutDemo.java

package com.tutorialspoint.gui;

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.showGridLayoutDemo();       
   }
   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 showGridLayoutDemo(){
      headerLabel.setText("Layout in action: GridLayout");      
      
      JPanel panel = new JPanel();
      panel.setBackground(Color.darkGray);
      panel.setSize(300,300);
      GridLayout layout = new GridLayout(0,3);
      layout.setHgap(10);
      layout.setVgap(10);
      
      panel.setLayout(layout);        
      panel.add(new JButton("Button 1"));
      panel.add(new JButton("Button 2")); 
      panel.add(new JButton("Button 3")); 
      panel.add(new JButton("Button 4")); 
      panel.add(new JButton("Button 5")); 
      controlPanel.add(panel);
      mainFrame.setVisible(true);  
   }
}

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

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

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

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

驗證以下輸出。

SWING  GridLayout
swing_layouts.htm
廣告