日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【Java AWT 图形界面编程】LayoutManager 布局管理器 ④ ( GridLayout 网格布局 | GridBagLayout 网格包布局 )

發(fā)布時間:2024/1/18 java 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java AWT 图形界面编程】LayoutManager 布局管理器 ④ ( GridLayout 网格布局 | GridBagLayout 网格包布局 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、GridLayout 網(wǎng)格布局
  • 二、GridLayout 構(gòu)造函數(shù)
  • 三、GridLayout 網(wǎng)格布局代碼示例
  • 四、GridBagLayout 網(wǎng)格包布局





一、GridLayout 網(wǎng)格布局



GridLayout 網(wǎng)格布局管理器 可以將 當前的 Container 容器 劃分成 網(wǎng)格 , 每個網(wǎng)格 區(qū)域 相同 ;

向 使用了 GridLayout 網(wǎng)格布局管理器 的 Container 容器 中添加 Component 組件時 , 默認的添加順序是 從左到右 , 從上到下 ;

放置在 GridLayout 網(wǎng)格中的組件 , 組件的大小由網(wǎng)格的區(qū)域大小決定 , 默認情況下 組件會填充滿所在的單個網(wǎng)格區(qū)域 ;





二、GridLayout 構(gòu)造函數(shù)



GridLayout 構(gòu)造函數(shù) :

  • GridLayout() : 單行網(wǎng)格布局 ;
/*** 創(chuàng)建一個默認為每個組件一列的網(wǎng)格布局,* 在單行中。* @since JDK1.1*/public GridLayout() {this(1, 0, 0, 0);}
  • GridLayout(int rows, int cols) : 網(wǎng)格布局 中的 行數(shù) 和 列數(shù) 使用指定的值 , 網(wǎng)格的 水平 和 垂直 間隔使用默認值 ;
/*** 創(chuàng)建具有指定行數(shù)和的網(wǎng)格布局* 列。布局中的所有組件都被賦予相同的大小。* <p>* <code>rows</code>和<code>cols</code>中的一個(而不是兩個)可以* 為零,這意味著任何數(shù)量的物體都可以放置在行或列。* @param rows 值為0的行表示* 任意數(shù)量的行。* @param cols 列,值為0表示* 任意數(shù)量的列。*/public GridLayout(int rows, int cols) {this(rows, cols, 0, 0);}
  • GridLayout(int rows, int cols, int hgap, int vgap) : 網(wǎng)格布局 中的 行數(shù) 和 列數(shù) 使用指定的值 , 網(wǎng)格的 水平 和 垂直 間隔使用指定的值 ;
/*** 創(chuàng)建具有指定行數(shù)和的網(wǎng)格布局* 列。布局中的所有組件都被賦予相同的大小。* < p >* 此外,水平和垂直間隙設(shè)置為* 指定的值。水平間隔放置在每個之間* 列的。垂直的間隙被放置在每一個之間* 行。* < p >* <code>行</code>和<code>cols</code>中的一個(而不是兩個)可以* 為零,這意味著任何數(shù)量的物體都可以放置在* 行或列。* < p >* 所有<code>GridLayout</code>構(gòu)造函數(shù)都遵循此構(gòu)造函數(shù)。* @param rows 值為0的行表示* 任意數(shù)量的行* @param cols 列,值為0表示* 任意數(shù)量的列* @param hgap 水平間隙* @param vgap 垂直差距* @exception IllegalArgumentException if the value of both* <code>rows</code> and <code>cols</code> is* set to zero*/public GridLayout(int rows, int cols, int hgap, int vgap) {if ((rows == 0) && (cols == 0)) {throw new IllegalArgumentException("rows and cols cannot both be zero");}this.rows = rows;this.cols = cols;this.hgap = hgap;this.vgap = vgap;}



三、GridLayout 網(wǎng)格布局代碼示例



代碼示例 :

import java.awt.*;public class HelloAWT {public static void main(String[] args) {// Frame 默認的布局管理器就是 BorderLayoutFrame frame = new Frame("AWT 界面編程");// 用于存放 文本框Panel panel = new Panel();// 該文本框可以存放 30 個字符TextField textField = new TextField(30);panel.add(textField);frame.add(panel, BorderLayout.NORTH);// 用于存放 網(wǎng)格布局中的組件// 需要設(shè)置該容器的 布局管理器為 網(wǎng)格布局管理器Panel panel2 = new Panel();panel2.setLayout(new GridLayout(3, 5, 4, 4));for (int i = 0; i < 10; i++) {panel2.add(new Button(i + ""));}panel2.add(new Button("+"));panel2.add(new Button("-"));panel2.add(new Button("*"));panel2.add(new Button("/"));panel2.add(new Button("="));frame.add(panel2, BorderLayout.CENTER);// 自定設(shè)置合適的大小frame.pack();frame.setVisible(true);} }

執(zhí)行結(jié)果 :





四、GridBagLayout 網(wǎng)格包布局



GridBagLayout 網(wǎng)格包布局 , 是在 GridLayout 網(wǎng)格布局的基礎(chǔ)上 , 單個組件可以占用多個網(wǎng)格 , 占用的多個網(wǎng)格的大小形狀也可以任意設(shè)置 , 每個組件都可以占用多行和多列的網(wǎng)格 , 即 m x n 大小的網(wǎng)格 , 如 : 占用 1 x 2 的網(wǎng)格 , 占用 3 x 4 的網(wǎng)格 ;

如果 GridBagLayout 網(wǎng)格包布局所在的 窗口 大小改變 , 對應的 網(wǎng)格 也會被 拉伸或壓縮 ;

向 使用 GridBagLayout 網(wǎng)格包布局 的 Container 容器中 添加 Component 組件時 , 需要指定添加的 組件具體占的 網(wǎng)格 行列數(shù) ; 可借助 GridBagConstaints 配置 組件 的 行列大小 ;

總結(jié)

以上是生活随笔為你收集整理的【Java AWT 图形界面编程】LayoutManager 布局管理器 ④ ( GridLayout 网格布局 | GridBagLayout 网格包布局 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。