GridLayout(表格布局管理器)

GridLayout(表格布局管理器)
GridLayout要注意的事项:
    1.rows 和 cols 中的一个可以为零(但不能两者同时为零),这表示可以将任何数目的对象置于行或列中。通过构造方法或 setRows 和 setColumns 方法将行数和列数都设置为非零值时,指定的列数将被忽略。列数通过指定的行数和布局中的组件总数来确定。
效果图如下:
GridLayout(表格布局管理器)_第1张图片
  代码如下:

  1. package com.cn.gui.layout;
  2. import java.awt.GridLayout;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import com.cn.gui.util.FrameUtil;
  6. /**
  7. * Author:Liu Zhiyong(QQ:1012421396)
  8. * Version:Version_1
  9. * Date:2016年8月13日16:55:42
  10. * Desc:
  11. GridLayout(表格布局管理器)
  12. GridLayout要注意的事项:
  13. 1.rows 和 cols 中的一个可以为零(但不能两者同时为零),这表示可以将任何数目的对象置于行或列中。通过构造方法或 setRows 和 setColumns 方法将行数和列数都设置为非零值时,指定的列数将被忽略。列数通过指定的行数和布局中的组件总数来确定。
  14. */
  15. public class Demo3 {
  16. public static void main(String[] args) {
  17. JFrame frame = new JFrame("表格布局管理器窗体");
  18. //创建表格布局管理器
  19. /*rows 和 cols 中的一个可以为零(但不能两者同时为零),这表示可以将任何数目的对象置于行或列中。
  20. 通过构造方法或 setRows 和 setColumns 方法将行数和列数都设置为非零值时,指定的列数将被忽略。
  21. 列数通过指定的行数和布局中的组件总数来确定。因此,例如,如果指定了三行和两列,在布局中添加了九个组件,
  22. 则它们将显示为三行三列。仅当将行数设置为零时,指定列数才对布局有效。 */
  23. GridLayout gridLayout = new GridLayout(0, 4, 1, 5);//GridLayout(int rows, int cols) 把窗体交给表格布局管理器管理
  24. frame.setLayout(gridLayout);
  25. for(int i=0; i<10; i++){
  26. frame.add(new JButton(""+i));
  27. }
  28. frame.add(new JButton("+"));
  29. frame.add(new JButton("-"));
  30. frame.add(new JButton("*"));
  31. frame.add(new JButton("/"));
  32. frame.add(new JButton("="));
  33. frame.add(new JButton("."));
  34. FrameUtil.initFrame(frame, 300, 300);
  35. }
  36. }

你可能感兴趣的:(javaSE,史上最全的JavaSE基础教程)