天天看點

GridLayout(表格布局管理器)

GridLayout(表格布局管理器)

GridLayout要注意的事項:

    1.rows 和 cols 中的一個可以為零(但不能兩者同時為零),這表示可以将任何數目的對象置于行或列中。通過構造方法或 setRows 和 setColumns 方法将行數和列數都設定為非零值時,指定的列數将被忽略。列數通過指定的行數和布局中的元件總數來确定。

效果圖如下:

package com.cn.gui.layout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import com.cn.gui.util.FrameUtil;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年8月13日16:55:42
* Desc:
GridLayout(表格布局管理器)
GridLayout要注意的事項:
  1.rows 和 cols 中的一個可以為零(但不能兩者同時為零),這表示可以将任何數目的對象置于行或列中。通過構造方法或 setRows 和 setColumns 方法将行數和列數都設定為非零值時,指定的列數将被忽略。列數通過指定的行數和布局中的元件總數來确定。
*/
public class Demo3 {
  public static void main(String[] args) {
    JFrame frame = new JFrame("表格布局管理器窗體");
    //建立表格布局管理器
    /*rows 和 cols 中的一個可以為零(但不能兩者同時為零),這表示可以将任何數目的對象置于行或列中。
    通過構造方法或 setRows 和 setColumns 方法将行數和列數都設定為非零值時,指定的列數将被忽略。
    列數通過指定的行數和布局中的元件總數來确定。是以,例如,如果指定了三行和兩列,在布局中添加了九個元件,
    則它們将顯示為三行三列。僅當将行數設定為零時,指定列數才對布局有效。 */
    GridLayout gridLayout = new GridLayout(0, 4, 1, 5);//GridLayout(int rows, int cols)  把窗體交給表格布局管理器管理
    frame.setLayout(gridLayout);
    for(int i=0; i<10; i++){
      frame.add(new JButton(""+i));
    }
    frame.add(new JButton("+"));
    frame.add(new JButton("-"));
    frame.add(new JButton("*"));
    frame.add(new JButton("/"));
    frame.add(new JButton("="));
    frame.add(new JButton("."));
    FrameUtil.initFrame(frame, 300, 300);
  }
}