Java的swing编程(二)

来源:互联网 发布:莆田电视台 网络直播 编辑:程序博客网 时间:2024/06/06 10:07

以下是学习java核心技术卷一swing编程的心得,摘录下来以备回顾

其中布局管理器概述部分是转载了他人(http://blog.sina.com.cn/s/blog_6f116c940101alna)转载的网络上的一篇文章但是该转载没有注明转载地址,我也不得而知,感谢原博主。

模型-视图-控制器设计模式

设计模式:在解决一个问题时,不需要从头做起,而是借鉴过去的经验,或者向做过的专家请教。设计模式就是一种方法,这种方法以一种结构化的形式展示专家们的心血。
AWT和swing设计中,应用的几种设计模式:
模型-视图-控制器模式
容器和组件是“组合模式”
带滚动条的是“装饰器模式”
布局管理器是“策略模式”
用户界面的各个组成部件当中,每个部件都有三个要素:内容(按键的状态或者文本框的文本)、外观(颜色大小等)、行为(对事件的反应)。
面向对象设计的基本原则:限制一个对象拥有的功能的数量。
模型-视图-控制器模式(MWC)的实现由三个独立的类构成:模型(model),存储内容、视图(view),显示内容、控制器(controller),处理用户输入。

以按键为例来学习模型类:
每个JButton对象都存储一个按键模型对象,用一下方法可以得到ta的引用:(每个属性的描述参看P327表9-1)
JButton button = new JButton("Blue");ButtonModel buttonModel = button.getModel();

布局管理器概述

1.BorderLayout

java.lang.Object
--java.awt.BorderLayout
将版面划分成东、西、南、北、中五个区域,将添加的组件按指定位置放置。
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.SOUTH
BorderLayout.NORTH
BorderLayout.CENTER
构造函数:
BorderLayout()
建立组件间无间距的BorderLayout
BorderLayout(int hgap,int vgap)
建立组件间水平间距为hgap,垂直间距为vgap的BorderLayout
public class BorderLayoutDemo {        public static void main(String[] args) {          //建立一个JFrame,JFrame的默认LayoutManager为BorderLayout           JFrame f=new JFrame("BorderLayout");          JButton btn=new JButton("BorderLayout.NORTH");          f.add(btn,BorderLayout.NORTH);          btn=new JButton("BorderLayout.SOUTH");          f.add(btn,BorderLayout.SOUTH);          btn=new JButton("BorderLayout.EAST");          f.add(btn,BorderLayout.EAST);          btn=new JButton("BorderLayout.West");          f.add(btn,BorderLayout.WEST);          btn=new JButton("BorderLayout.CENTER");          f.add(btn,BorderLayout.CENTER);          f.pack();          f.setVisible(true);          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      }    }  

2.FlowLayout

java.lang.Object
--java.awt.FlowLayout
组件按从左到右而后从上到下的顺序依次排列,一行不能放完则折到下一行。

构造函数:
FlowLayout()
建立一个默认为居中对齐,组件彼此有5单位的水平与垂直间距的FlowLayout
FlowLayout(int align)
建立一个可设置排列方式且组件彼此有5单位的水平与垂直间距的FlowLayout
FlowLayout(int align,int hgap,int vgap)
建立一个可设置排列方式与组件间距的FlowLayout
<span style="white-space:pre"></span>public class FlowLayoutDemo {        public static void main(String[] args) {          JFrame f=new JFrame("FlowLayout");          f.setLayout(new FlowLayout());          for(int i=0;i<7;i++){              JButton btn=new JButton("Button"+i);              f.add(btn);          }          f.setSize(300,150);          f.setVisible(true);          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      }    }  

3.GridLayout


java.lang.Object
--java.awt.GridLayout
矩形网格形式对容器的组件进行布置

构造函数:
GridLayout()
建立一个默认为一行一列的GridLayout
GridLayout(int rows,int cols)
建立一个指定行(rows)和列(cols)的GridLayout
GridLayout(int rows,int cols,int hgap,int vgap)
建立一个指定行(rows)和列(cols),且组件间水平间距为hgap、垂直间距为vgap的GridLayout
public class GridLayoutDemo {        public static void main(String[] args) {          JFrame f=new JFrame("GridLayout");          //设置f的布局管理器为3行3列的GridLayout,组件间水平与垂直间距为5           f.setLayout(new GridLayout(3,3,5,5));          for(int i=1;i<10;i++){              JButton btn=new JButton(String.valueOf(i));              f.add(btn);          }          f.pack();          f.setVisible(true);          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      }    }  
4.GridBagLayout

java.lang.Object
--java.awt.GridBagLayout
GridBagLayout以表格形式布置容器内的组件,将每个组件放置在每个单元格内,而一个单元格可以跨越多个单元格合并成一个单元格,即多个单元格可以组合成一个单元格,从而实现组件的自由布局。
构造函数:
GridBagLayout()
建立一个默认的GridBagLayout
每一个单元格都有各自的属性,而这些属性由GridBagConstrainsts类的成员变量来定义,且GridBagConstriaints中的所有成员变量都是public的。
java.lang.Object
--java.awt.GridBagConstratints
构造函数:
GridBagConstraints()
建立一个默认的GridBagConstraints
GridBagConstraints(intgridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int fill,Insets insets,int ipadx,int ipady)
建立一个指定其参数值的GridBagConstraints
GridBagConstraints的成员变量:
int gridx
int gridy
int gridwidth
int gridheight
double weightx
double weighty
int anchor
int fill
Insets insets
int ipadx
int ipady
gridx,gridy:设置组件所处行与列的起始坐标。例如gridx=0,gridy=0表示将组件放置在0行0列单元格内。
gridwidth,gridheight:设置组件横向与纵向的单元格跨越个数。
可以通过GridBagConstraints的RELETIVE,和REMAINDER来进行指定,它的用法是:
当把gridx值设置为GridBagConstriants.RELETIVE时,所添加的组件将被放置在前一个组件的右侧。同理,对gridy 值设置为GridBagConstraints.RELETIVE时,所添加的组件将被放置在前一个组件的下方,(这是一种根据前一个组件而决定当前组 件的相对放置方式)
对gridweight和gridheight也可以应用GridBagConstraints的REMAINDER方式,创建的组件会从创建的起点位置 开始一直延伸到容器所能允许的界限为止。该功能使得你可以创建跨越某些行或列的组件,从而改变相应方向上组件的数目,即使其后在布局的其他地方添加额外的组件也是如此。
weightx,weighty:设置窗口变大时的缩放比例。
nchor:设置组件在单元格中的对齐方式。由以下常量来定义
GridBagConstraints.CENTER
GridBagConstraints.EAST
GridBagConstraints.WEST
GridBagConstraints.SOUTH
GridBagConstraints.NORTH
GridBagConstraints.SOUTHEAST
GrisBagConstraints.SOUTHWEST
GridBagConstraints.NORTHEAST
GridBagConstraints.NORTHWEST
fill:当某个组件未能填满单元格时,可由此属性设置横向、纵向或双向填满。由以下常量来定义
GridBagConstraints.NONE
GridBagConstraints.HORIZONTAL
GridBagConstraints.VERTICAL
GridBagConstraints.BOTH
i nsets:设置单元格的间距。
java.lang.Object
--java.awt.Insets
Insets(int top,int left,int bottom,int right)
ipadx,ipady:将单元格内的组件的最小尺寸横向或纵向扩大。若一个组件的尺寸为30*10像素,ipadx=2,ipady=3,则单元格内的组件最小尺寸为34*16像素
public class GridBagLayoutDemo {        public static void main(String[] args) {          JFrame f=new JFrame("GridBagLayout");          f.setLayout(new GridBagLayout());          JButton btn=new JButton("first");          GridBagConstraints gbc=new GridBagConstraints();          //设定第一个单元格的属性值           gbc.gridx=0;          gbc.gridy=0;          gbc.gridwidth=1;          gbc.gridheight=1;          gbc.weightx=0;          gbc.weighty=0;          gbc.anchor=GridBagConstraints.NORTHWEST;          gbc.fill=GridBagConstraints.NONE;          gbc.insets=new Insets(0,0,0,0);          gbc.ipadx=0;          gbc.ipady=0;          f.add(btn,gbc);            //设定第二个单元格属性值           gbc.gridx=1;          gbc.gridy=0;          gbc.gridwidth=GridBagConstraints.REMAINDER;          gbc.gridheight=1;          gbc.weightx=1;          gbc.weighty=0;          gbc.anchor=GridBagConstraints.CENTER;          gbc.fill=GridBagConstraints.HORIZONTAL;          gbc.insets=new Insets(5,5,5,5);          gbc.ipadx=0;          gbc.ipady=0;          btn=new JButton("second");          f.add(btn,gbc);            //设定第三个单元格属性值           gbc.gridx=0;          gbc.gridy=1;          gbc.gridwidth=1;          gbc.gridheight=GridBagConstraints.REMAINDER;          gbc.weightx=0;          gbc.weighty=1;          gbc.anchor=GridBagConstraints.CENTER;          gbc.fill=GridBagConstraints.VERTICAL;          gbc.insets=new Insets(0,0,0,0);          gbc.ipadx=10;          gbc.ipady=10;          btn=new JButton("three");          f.add(btn,gbc);          f.pack();          f.setVisible(true);          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      }    }  

6.BoxLayout


java.lang.Object
--javax.swing.BoxLayout
以嵌套式盒子来管里容器的布局,通过将组件放入水平或垂直形盒子以多层嵌套的方式进行布局。
构造函数:
BoxLayout(Container target,int axis)
建立一个水平或垂直的BoxLayout,BoxLayout提供两个常数X_AXIS和Y_AXIS来表示水平或垂直排列。
说到BoxLayout,就不得不提到Box这个Container,Box这个Container默认的Layout为BoxLayout,而它只能使用这个Layout,否则编译时会有Error产生。
java.lang.Object
--javax.swing.Box
Box有水平的和垂直的两种形式。
构造函数:
Box(int axis)
建立一个Box Container(容器),并指定组件的排列方式,通过使用BoxLayout提供的两个常数X_AXIS和Y_AXIS来指定。

方法:
public static Box createHorizontalBox()
构造一个水平排列的Box组件。
public class BoxLayoutDemo {        public static void main(String[] args) {          JFrame f=new JFrame("BoxLayout");          //创建水平Box组件           Box box=Box.createHorizontalBox();          JButton btnA=new JButton("A");          JButton btnB=new JButton("B");          box.add(btnA);          box.add(btnB);          f.add(box);          f.pack();          f.setVisible(true);          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      }        }   



0 0
原创粉丝点击