- AWT 教程
- AWT - 首頁
- AWT - 概述
- AWT - 環境
- AWT - 控制元件
- AWT - 事件處理
- AWT - 事件類
- AWT - 事件監聽器
- AWT - 事件介面卡
- AWT - 佈局
- AWT - 容器
- AWT - 選單
- AWT - 圖形
- AWT 有用資源
- AWT - 快速指南
- AWT - 有用資源
- AWT - 討論
AWT GridBagLayout 類
簡介
GridBagLayout 類以水平和垂直方式排列元件。
類宣告
以下是 java.awt.GridBagLayout 類的宣告
public class GridBagLayout
extends Object
implements LayoutManager2, Serializable
欄位
以下是 java.awt.BorderLayout 類的欄位
double[] columnWeights -- 此欄位儲存對列權重的覆蓋。
int[] columnWidths -- 此欄位儲存對列最小寬度的覆蓋。
protected Hashtable comptable -- 此雜湊表維護元件與其網格包約束之間的關聯。
protected GridBagConstraints defaultConstraints -- 此欄位儲存一個包含預設值的網格包約束例項,因此如果元件沒有與其關聯的網格包約束,則該元件將被分配 defaultConstraints 的副本。
protected java.awt.GridBagLayoutInfo layoutInfo -- 此欄位儲存網格包的佈局資訊。
protected static int MAXGRIDSIZE -- 網格包佈局可以佈局的最大網格位置數(水平和垂直)。
protected static int MINSIZE -- 網格包佈局可以佈局的最小網格。
protected static int PREFERREDSIZE -- 網格包佈局可以佈局的首選網格大小。
int[] rowHeights -- 此欄位儲存對行最小高度的覆蓋。
double[] rowWeights -- 此欄位儲存對行權重的覆蓋。
類建構函式
| 序號 | 建構函式和說明 |
|---|---|
| 1 | GridBagLayout()
建立一個網格包佈局管理器。 |
類方法
| 序號 | 方法和說明 |
|---|---|
| 1 | void addLayoutComponent(Component comp, Object constraints) 使用指定的約束物件將指定的元件新增到佈局。 |
| 2 | void addLayoutComponent(String name, Component comp) 將指定的元件及其名稱新增到佈局。 |
| 3 | protected void adjustForGravity(GridBagConstraints constraints, Rectangle r) 根據約束幾何形狀和填充調整 x、y、寬度和高度欄位到正確的值。 |
| 4 | protected void AdjustForGravity(GridBagConstraints constraints, Rectangle r) 此方法已過時,僅出於向後相容性而提供;新程式碼應改為呼叫 adjustForGravity。 |
| 5 | protected void arrangeGrid(Container parent) 佈局網格。 |
| 6 | protected void ArrangeGrid(Container parent) 此方法已過時,僅出於向後相容性而提供;新程式碼應改為呼叫 arrangeGrid。 |
| 7 | GridBagConstraints getConstraints(Component comp) 獲取指定元件的約束。 |
| 8 | float getLayoutAlignmentX(Container parent) 返回沿 x 軸的對齊方式。 |
| 9 | float getLayoutAlignmentY(Container parent) 返回沿 y 軸的對齊方式。 |
| 10 | int[][] getLayoutDimensions() 確定佈局網格的列寬和行高。 |
| 11 | protected java.awt.GridBagLayoutInfo getLayoutInfo(Container parent, int sizeflag) 為當前管理的子代填充 GridBagLayoutInfo 的例項。 |
| 12 | protected java.awt.GridBagLayoutInfo GetLayoutInfo(Container parent, int sizeflag) 此方法已過時,僅出於向後相容性而提供;新程式碼應改為呼叫 getLayoutInfo。 |
| 13 | Point getLayoutOrigin() 確定佈局區域的原點,位於目標容器的圖形座標空間中。 |
| 14 | double[][] getLayoutWeights() 確定佈局網格的列和行的權重。 |
| 15 | protected Dimension getMinSize(Container parent, java.awt.GridBagLayoutInfo info) 根據 getLayoutInfo() 中的資訊計算主體的最小尺寸。 |
| 16 | protected Dimension GetMinSize(Container parent, java.awt.GridBagLayoutInfo info) 此方法已過時,僅出於向後相容性而提供;新程式碼應改為呼叫 getMinSize。 |
| 17 | void invalidateLayout(Container target) 使佈局無效,表明如果佈局管理器已快取資訊,則應將其丟棄。 |
| 18 | void layoutContainer(Container parent) 使用此網格包佈局佈局指定的容器。 |
| 19 | Point location(int x, int y) 確定佈局網格中哪個單元格包含由 (x, y) 指定的點。 |
| 20 | protected GridBagConstraints lookupConstraints(Component comp) 檢索指定元件的約束。 |
| 21 | Dimension maximumLayoutSize(Container target) 返回給定指定目標容器中元件的此佈局的最大尺寸。 |
| 22 | Dimension minimumLayoutSize(Container parent) 使用此網格包佈局確定父容器的最小尺寸。 |
| 23 | Dimension preferredLayoutSize(Container parent) 使用此網格包佈局確定父容器的首選尺寸。 |
| 24 | void removeLayoutComponent(Component comp) 從此佈局中刪除指定的元件。 |
| 25 | void setConstraints(Component comp, GridBagConstraints constraints) 設定此佈局中指定元件的約束。 |
| 26 | String toString() 返回此網格包佈局值的字串表示形式。 |
繼承的方法
此類繼承自以下類的方法
java.lang.Object
GridBagLayout 示例
使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > AWT > com > tutorialspoint > gui > 中
AwtLayoutDemo.javapackage com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public class AwtLayoutDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private Label msglabel;
public AwtLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo();
awtLayoutDemo.showGridBagLayoutDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
msglabel = new Label();
msglabel.setAlignment(Label.CENTER);
msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showGridBagLayoutDemo(){
headerLabel.setText("Layout in action: GridBagLayout");
Panel panel = new Panel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new Button("Button 1"),gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(new Button("Button 2"),gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.ipady = 20;
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new Button("Button 3"),gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(new Button("Button 4"),gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
panel.add(new Button("Button 5"),gbc);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
使用命令提示符編譯程式。轉到 D:/ > AWT 並鍵入以下命令。
D:\AWT>javac com\tutorialspoint\gui\AwtlayoutDemo.java
如果沒有錯誤,則表示編譯成功。使用以下命令執行程式。
D:\AWT>java com.tutorialspoint.gui.AwtlayoutDemo
驗證以下輸出