Java图形化界面设计——布局管理器之GridLayout(网格布局)

l  使容器中的各组件呈M行×N列的网格状分布。

l  网格每列宽度相同,等于容器的宽度除以网格的列数。

l  网格每行高度相同,等于容器的高度除以网格的行数。

l  各组件的排列方式为:从上到下,从左到右。

l  组件放入容器的次序决定了它在容器中的位置。

l  容器大小改变时,组件的相对位置不变,大小会改变。

l  设置网格布局行数和列数时,行数或者列数可以有一个为零。若rows0cols3,则列数固定为3,行数不限,每行只能放3个控件或容器。若cols0rows3,则行数固定为3,列数不限,且每行必定有控件,若组件个数不能整除行数,则除去最后一行外的所有行组件个数为:Math.ceil(组件个数/rows)

Math.ceil(double x):传回不小于x的最小整数值。比如行数为3,组件数为13个,则Math.ceil(13/3)=5,即第一行,第二行组件数各为5个,剩下的组件放在最后一行。

l  若组件数超过网格设定的个数,则布局管理器会自动增加网格个数,原则是保持行数不变。

 

 

构造方法摘要

GridLayout() 创建具有默认值的网格布局,即每个组件占据一行一列。

GridLayout(int rows, int cols) :

创建具有指定行数和列数的网格布局。Rows为行数,cols为列数。

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

创建具有指定行数、列数以及组件水平、纵向一定间距的网格布局。

 

 

 

方法摘要

int

getColumns()  :获取此布局中的列数。

int

getHgap():获取组件之间的水平间距。

int

getRows() :获取此布局中的行数。

int

getVgap()  :获取组件之间的垂直间距。

void

removeLayoutComponent(Component comp) :从布局移除指定组件。

void

setColumns(int cols)  :将此布局中的列数设置为指定值。

void

setHgap(int hgap):将组件之间的水平间距设置为指定值。

void

setRows(int rows):将此布局中的行数设置为指定值。

void

setVgap(int vgap) :将组件之间的垂直间距设置为指定值。

String

toString():返回此网格布局的值的字符串表示形式。

 

实例一:

//GridLayoutDemo.java

import javax.swing.*;

import java.awt.*;

public class GridLayoutDemo extends JFrame {

  public GridLayoutDemo() {

     setLayout(new GridLayout(0,2));           //设置为网格布局,未指定行数

     setFont(new Font("Helvetica", Font.PLAIN, 14));

     getContentPane().add(new JButton("Button 1"));

     getContentPane().add(new JButton("Button 2"));

     getContentPane().add(new JButton("Button 3"));

     getContentPane().add(new JButton("Button 4"));

     getContentPane().add(new JButton("Button 5"));

  }

  public static void main(String args[]) {

    GridLayoutDemo f = new GridLayoutDemo();

    f.setTitle("GridWindow Application");

    f.pack();

    f.setVisible(true);

         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         f.setLocationRelativeTo(null);           //让窗体居中显示

  }

}

 

程序结果如下图所示:

 

 

 

实例二:布局一个简单的计算器

思路:对窗体应用边界布局,在NORTH上放置一个文本框,在CENTER上放置一个面板,面板上放置计算器的相应按钮

//GridFrame.Java

import java.awt.*;

import javax.swing.*;

class GridFrame extends JFrame{

         //定义面板,并设置为网格布局,44列,组件水平、垂直间距均为3

          JPanel p=new JPanel(new GridLayout(4,4,3,3));

          JTextArea t=new JTextArea();        //定义文本框

          //定义字符串数组,为按钮的显示文本赋值

         //注意字符元素的顺序与循环添加按钮保持一致

          String str[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};

     public GridFrame(String s){

                   super(s);  //为窗体名称赋值

        setLayout(new BorderLayout());     //定义窗体布局为边界布局

        JButton btn[];                             //声明按钮数组

        btn=new JButton[str.length];     //创建按钮数组

                   //循环定义按钮,并添加到面板中

        for(int i=0;i<str.length;i++){

               btn[i]=new JButton(str[i]);

               p.add(btn[i]);

         }

                    //将文本框放置在窗体NORTH位置

            getContentPane().add(t,BorderLayout.NORTH);     

            //将面板放置在窗体CENTER位置

            getContentPane().add(p,BorderLayout.CENTER);   

            setVisible(true);

            setSize(250,200);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            setLocationRelativeTo(null);               //让窗体居中显示

        }

      public static void main(String[] args){

         GridFrame gl=new GridFrame("网格布局计算机!");

         }

 

程序执行结果如下图:

 

                  注意:这里一定要体会通过字符串数组和循环添加按钮的方法,以后添加菜单以及事件处理均采用这种方法,这种方法的好处在于,如果需要修改按钮顺序,直接修改字符串就可以了。而不需要改动添加按钮的代码。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值