一.JAVA GUI设计
GUI(图形用户界面):是指以图形化方式与用户进行交互的程序运行界面
GUI本身即一个界面,这个界面包含一个顶层容器Container,容器内包含menu,button,label等组件
二.GUI基本界面的实现
<span style="font-size:18px;background-color: rgb(255, 255, 255);">import java.awt.*;
public class FirstFrame {
public static void main(String[] args) {
Frame f = new Frame("第一个图形用户界面");
f.setBackground(Color.yellow);
f.setBounds(100, 200, 300, 200);
f.setSize(300,200);
f.setLocation(100,200);
f.setVisible(true);
}
} </span>
三.GUI布局方式
1.FlowLayout流式布局
流式布局,是Panel(及其子类)默认布局管理器
- 布局效果:组件在容器中按照加入次序逐行定位,行内从左到右,一行排满后换行。组件按原始大小进行显示
<span style="font-size:18px;background-color: rgb(255, 255, 255);">importjava.awt.*;
class TestFrame{
public static void main(String args[]){
Frame frame=new Frame("GUI布局");//容器
Label lbl=new Label("FlowLayout布局方式");//控制组件
lbl.setBackground(Color.pink);//设置背景颜色
frame.setLayout(new FlowLayout());//设置布局方式
frame.add(lbl); //将控制组件添加到容器中
frame.setSize(200,100);
frame.setVisible(true);</span>
2.BorderLayout 边界布局方式
边界布局 是Window及其子类类型容器的默认布局管理器。
布局效果:将整个容器范围划分成East、West、South、North、Center五个区域,组件只能被添加到指定的区域。
<span style="font-size:18px;background-color: rgb(255, 255, 255);"><span style="font-size:18px;">import java.awt.*;
public class BorderLayoutDemo extends Frame {
Button bNorth,bSouth,bWest,bEast,bCenter;
public BorderLayoutDemo(){
super("边框布局");
bNorth = new Button("北");
bSouth = new Button("南");
bWest = new Button("西");
bEast = new Button("东");
bCenter = new Button("中");
add(bNorth,"North");
add(bSouth,"South");
add(bWest,"West");
add(bEast,"East");
add(bCenter,"Center");
setBounds(200,200,300,300);
setVisible(true);
}
public static void main(String[] args){
new BorderLayoutDemo();
}</span></span>
3.GridLayout网格式布局
网格式布局,将容器区域划分成规则的矩形网格,每个单元格区域大小相等。组件被添加到每个单元格中,按组件加入顺序先从左到右填满一行后换行,行间从上到下。
<span style="font-size:18px;background-color: rgb(255, 255, 255);"><span style="font-size:18px;">import java.awt.*;
public class GridLayoutDemo extends Frame {
Button[] b = new Button[5];
public GridLayoutDemo(){
super("网格布局");
for(int i=0; i<b.length; i++){
b[i] = new Button("按钮"+i);
}
setLayout(new GridLayout(3,2));
add(b[0]);
add(b[1]);
add(b[2]);
add(b[3]);
add(b[4]);
pack();
setSize(300,100);
setLocation(100,200);
//setBounds(200.100,300,100);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutDemo();
}
} </span></span>
四.GUI事件处理机制
1.定义: 用户编程定义每个特定事件发生时程序应做出何种响应,并且这些响应代码会在对应的事件发生时由系统自动调用
2.事件处理机制三要素:
⑴事件源
即事件来自哪个组件或对象
⑵事件监听器
即负责监听事件并作出响应
⑶事件对象
即某个要被处理的事件
3.实现事件处理的三种方法
⑴实现事件监听接口
<span style="font-size:18px;background-color: rgb(255, 255, 255);">import java.awt.*;
import java.awt.event.*;
/*
* 监听事件:
* 方法一:实现xxxListener接口
*
* */
public class windows2 implements WindowListener{
public static void main(String[] args){
Frame f = new Frame("windows");
f.setLocation(400,500);
f.setSize(200, 400);
f.setLayout(new FlowLayout());
Button b = new Button("按钮");
f.addWindowListener(new windows2());
f.add(b);
f.setVisible(true);
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("关闭吗?");
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
</span>
实现xxxListener接口,缺点是,必须实现该接口内所有方法,许多不必实现的方法都得实现。
改进方法:继承事件监听器适配器
⑵继承事件监听器适配器
<span style="font-size:18px;background-color: rgb(255, 255, 255);">public class windows3 extends WindowAdapter{
public static void main(String[] args){
Frame f = new Frame("windows3");
f.setLocation(400,500);
f.setSize(200, 400);
f.setLayout(new FlowLayout());
f.addWindowListener(new windows3());
f.setVisible(true);
}
public void windowClosing(WindowEvent e){
// TODO Auto-generated method stub
System.out.println("关闭吗?");
}
}</span>
⑶使用匿名内部类
<span style="font-size:18px;background-color: rgb(255, 255, 255);">public class windows1 {
public static void main(String[] args){
Frame f = new Frame("windows1");
f.setLocation(300,400);
f.setSize(100,100);
f.setLayout(new BorderLayout());
Button b = new Button("点击");
f.add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("点击按钮了");
}
});
f.setVisible(true);
}
}</span>