Java GUI设计第3篇:对窗口进行布局
在上一篇教程中,我们提到了布局方式。在Java中我们通常需要对窗口进行布局,这样可以让窗口中的各个组件摆放在我们想要的位置。
对窗口布局,我们需要使用java.awt中的布局管理器,布局管理器有很多种,我们接下来将介绍几种。
1.无布局管理器
所谓的无布局管理器,就是说,窗口中的所有组件可以*设置位置,*设置大小。JFrame中的一个方法可以设置布局管理器,为setLayout()。我们先创建一个窗口:
package teach3;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public MainFrame() {
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
MainFrame mf=new MainFrame();
mf.setVisible(true);
}
}
运行效果:
然后,我们设置布局管理器,我们只需要一个语句即可:
this.setLayout(null);
无布局管理器中只要设置为null即可。
然后,我们向其中添加两个按扭,并分别对两个按扭进行设置。设置一个组件的位置,大小我们只要用setBounds()方法即可完成。两个按扭的大小为(60,60),位置从左到右。
以下为代码:
package teach3;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton b1,b2;//定义两个按扭
public MainFrame() {
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
b1=new JButton("b1");//给按扭命名,也可以用setName()完成
b2=new JButton("b2");
b1.setBounds(0, 0, 60, 60);//设置位置和大小,前两个为位置,后两个为大小
b2.setBounds(60, 0, 60, 60);
this.add(b1);//添加按扭
this.add(b2);
}
public static void main(String[] args) {
MainFrame mf=new MainFrame();
mf.setVisible(true);
}
}
运行效果如下:
如果我们需要调整位置,只要将setBounds()中的参数改变即可。
以上是无布局管理器,这样的布局方式比较*,但是在窗口大小改变的时候,这些组件的位置通常不能改变。比如,对窗口改变大小:
两个按扭的位置并没有发生改变。
而且,在不同的系统中,也会产生不同的效果。
2.FlowLayout
使用FlowLayout布局时,窗口中的组件就会象我们写字一样,首先在第一行写,写不下了再到下一行。
接下来我们在窗口中创建5个按扭,并用FlowLayout进行布局。
package teach3;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton b[]=new JButton[5];//创建5个按扭,这里用数组
public MainFrame2() {
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());//设置布局方式为FlowLayout
for(int i=0; i<5; i++) {
b[i]=new JButton("Java");//创建对象
this.add(b[i]);//添加按扭
}
}
public static void main(String[] args) {
MainFrame2 mf=new MainFrame2();
mf.setVisible(true);
}
}
运行效果如下:
我们对窗口进行调整大小,可以发现,组件的位置会自己发生改变。
3.GridLayout
GridLayout就是将界面均匀地分成一个n行m列的矩阵,每一个格子中添加一个组件。
下面,我们将使用3*2的矩阵,并放满6个按扭。
package teach3;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame3 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton b[]=new JButton[6];
public MainFrame3() {
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3,2));//设置为GridLayout
for(int i=0; i<6; i++) {
b[i]=new JButton("Java");
this.add(b[i]);
}
}
public static void main(String[] args) {
MainFrame3 mf=new MainFrame3();
mf.setVisible(true);
}
}
运行效果如下:
对窗口调整大小之后,同样也能调整组件大小位置。
在定义GridLayout的时候,我们可以多两个参数,分别为列之间的间隔,行之间的间隔。比如,我们对上述代码进行修改,我们就可以在按扭之间产生一个间隔。
package teach3;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame3 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton b[]=new JButton[6];
public MainFrame3() {
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3,2,15,5));//添加的两个参数为间隔大小
for(int i=0; i<6; i++) {
b[i]=new JButton("Java");
this.add(b[i]);
}
}
public static void main(String[] args) {
MainFrame3 mf=new MainFrame3();
mf.setVisible(true);
}
}
运行效果如下:
按扭之间就产生了一些间隔。
4.BorderLayout
BorderLayout将界面分为5个区域,分为为North,South,West,East,Center,对应5个方向。可以添加5个组件。
下面的程序中有5个组件,使用BorderLayout:
package teach3;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MF4 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton b[]=new JButton[5];
public MF4() {
this.setSize(400, 300);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
for(int i=0; i<5; i++)
b[i]=new JButton("Java");//创建对象
//注意在使用BorderLayout布局时,添加组件要用方向
this.add(b[0],BorderLayout.NORTH);
this.add(b[1],BorderLayout.SOUTH);
this.add(b[2],BorderLayout.EAST);
this.add(b[3],BorderLayout.WEST);
this.add(b[4],BorderLayout.CENTER);
}
public static void main(String[] args) {
MF4 mf=new MF4();
mf.setVisible(true);
}
}
效果:
对于BorderLayout,同样可以设置间隔,与GridLayout一样,这里不再描述。