前面虽然实现了窗口,但是一般的应用有很多其他东西,如按钮,输入框之类的。
而这些都是建立在一个东西上面的,那就是布局管理器。
常用的布局管理器有3个
BorderLayout:边界布局管理器
FlowLayout:流式布局管理器
GridLayout:网格布局管理器
今天先说说BorderLayout
我们先修改下UI类,这次是最后一次修改,这次会将UI类与管理器和控件彻底分开,再也不用操作UI类
packageFrame;importjavax.swing.JFrame;public classUI{
JFrame frame;
String title;intframe_w;intframe_h;intlocation_x;intlocation_y;
UI(){
frame=newJFrame();
frame.setTitle("Word");//设置标题
frame.setSize(500, 500);//设置窗口大小
frame.setLocation(400, 200);//设置窗口出现在屏幕的坐标
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击退出时关闭程序//设置布局
MyLayout layout=newMyLayout(frame);
layout.creatLayout();
frame.setVisible(true);//设置窗口可见
}public voidsetFrame_Title(String title){this.title=title;
frame.setTitle(title);
}public void setFrame_Size(int frame_w,intframe_h){this.frame_w=frame_w;this.frame_h=frame_h;
frame.setSize(frame_w, frame_h);
}public void setFrame_Location(int location_x,intlocation_y){this.location_x=location_x;this.location_y=location_y;
}publicString getFrame_Title(){return this.title;
}public intgetFrame_SizeW(){return this.frame_w;
}public intgetFrame_SizeH(){return this.frame_h;
}public intgetFrame_LocationX(){return this.location_x;
}public intgetFrame_LocationY(){return this.location_y;
}
}
多了2行代码
MyLayout layout=new MyLayout(frame);
layout.creatLayout();
这里就是加载自己定义的布局类MyLayout
而creatLayout就是将布局类的设置的启动器吧,这样说复杂了。就是对外留的接口函数
public classMyLayout{
JFrame frame;publicMyLayout(JFrame frame) {//TODO Auto-generated constructor stub
this.frame=frame;
}//留给UI类操作的函数
public voidcreatLayout(){
ShowBorderLayout();
}//BorderLayout的用法
voidShowBorderLayout(){
JButton East=new JButton("东方");
JButton West=new JButton("西方");
JButton North=new JButton("北方");
JButton South=new JButton("南方");
JButton Center=new JButton("中央");
frame.add(East, BorderLayout.EAST);
frame.add(West, BorderLayout.WEST);
frame.add(North, BorderLayout.NORTH);
frame.add(South,BorderLayout.SOUTH);
frame.add(Center, BorderLayout.CENTER);/**如果不是五个按钮全部添加,则会以扩充中部填充,但中部不会被其他四个填充
**/}}
这里的构造函数的参数是JFrame 能理解吧? 因为对布局的操作都是基于JFrame的 所以将UI类的JFrame传了过来。
ShowBorderLayout就是边界布局的具体代码。JBotton就是按钮控件。
显示一下效果
这里是添加了5个按钮 如果不全呢? 改下代码
packageFrame;importjava.awt.BorderLayout;importjavax.swing.JButton;importjavax.swing.JFrame;public classMyLayout{
JFrame frame;publicMyLayout(JFrame frame) {//TODO Auto-generated constructor stub
this.frame=frame;
}//留给UI类操作的函数
public voidcreatLayout(){
ShowBorderLayout();
}//BorderLayout的用法
voidShowBorderLayout(){
JButton East=new JButton("东方");
JButton West=new JButton("西方");
JButton North=new JButton("北方");
JButton South=new JButton("南方");
JButton Center=new JButton("中央");//frame.add(East, BorderLayout.EAST);
frame.add(West, BorderLayout.WEST);
frame.add(North, BorderLayout.NORTH);
frame.add(South,BorderLayout.SOUTH);
frame.add(Center, BorderLayout.CENTER);/**如果不是五个按钮全部添加,则会以扩充中部填充,但中部不会被其他四个填充
**/}voidShowFlowLayout(){
}
}
注释掉东方的按钮。效果是
就是这样,其他就没有什么注意的地方了。
这里的MyLayout类就是操作具体界面的类了,现在已经不用再管UI类了,改变窗口的属性可以再主函数中改,改变布局就在这个类中改。
这里不管是边界还是流式布局还是网格布局管理器,都直接写个ShowXXXLayout的函数就可。
这样就很好的封装了UI类,方便以后修改代码和维护。
就是java编程里面的一个很重要的思想。