一、概述
1、GUI
图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
2、Java GUI的核心技术:
Swing和AWT;
3、Java的GUI编程技术正在被淘汰的原因
界面不美观;
需要jre运行环境;
4、为什么我们要学习Java GUI
写出自己心中想要的小工具;
工作可能需要(极小概率);
了解MVC架构,了解监听;
二、AWT介绍
抽象窗口工具包AWT (Abstract Window Toolkit) 是 API为Java 程序提供的建立图形用户界面GUI (Graphics User Interface)工具集,AWT可用于Java的applet和applications中;
它支持图形用户界面编程的功能包括:
用户界面组件;
事件处理模型;
图形和图像工具,包括形状、颜色和字体类;
布局管理器,可以进行灵活的窗口布局而与特定窗口的尺寸和屏幕分辨率无关;
数据传送类,可以通过本地平台的剪贴板来进行剪切和粘贴;
AWT框架:
三、第一个Frame窗口
1、代码演示
package com.zibo.lession01;
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
//点进去,看源码知道怎么用
Frame frame = new Frame("这是窗口名字");
//设置而可见性
frame.setVisible(true);
//设置大小和位置
frame.setSize(400,400);
frame.setLocation(300,300);
//设置背景颜色
frame.setBackground(Color.CYAN);
//设置大小固定
frame.setResizable(false);
}
}
2、运行结果
四、Frame窗口的封装
1、代码演示
package com.zibo.lession01;
import java.awt.*;
//GUI第一个界面
public class TestFrame02 {
public static void main(String[] args) {
new MyFrame(200,200,300,300,Color.yellow);
new MyFrame(500,200,300,300,Color.red);
new MyFrame(200,500,300,300,Color.CYAN);
new MyFrame(500,500,300,300,Color.GREEN);
}
}
class MyFrame extends Frame{
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color backgroundColor){
//设置而可见性
setVisible(true);
//设置大小和位置
setBounds(x,y,w,h);
//设置背景颜色
setBackground(backgroundColor);
//设置大小固定
setResizable(false);
}
}
2、运行结果
五、Panel面板
package com.zibo.lession01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 相当于安卓的View,Html的div,算是一个容器
public class TestPanel {
public static void main(String[] args) {
//窗口
Frame frame = new Frame("窗口");
//面板
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//窗口的位置和大小
frame.setBounds(300,300,500,500);
//窗口的背景色
frame.setBackground(Color.cyan);
//面板的位置和大小
panel.setBounds(50,50,400,400);
panel.setBackground(Color.red);
//在窗口中添加面板
frame.add(panel);
//显示窗口
frame.setVisible(true);
//监听事件,监听窗口关闭事件
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭按钮的时候要做的事情
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
六、三种布局管理器
1、流式布局
代码演示:
package com.zibo.lession01;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("标题");
//组件-按钮
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
//设置为流式布局
frame.setLayout(new FlowLayout());
frame.setBounds(300,300,500,500);
//添加按钮
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.setVisible(true);
}
}
运行结果:
2、线性布局(东西南北中)
代码演示:
package com.zibo.lession01;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
frame.setLayout(new BorderLayout());
Button bnt1 = new Button("bnt1");
Button bnt2 = new Button("bnt2");
Button bnt3 = new Button("bnt3");
Button bnt4 = new Button("bnt4");
Button bnt5 = new Button("bnt5");
frame.setBounds(300,300,500,500);
frame.add(bnt1,BorderLayout.EAST);
frame.add(bnt2,BorderLayout.WEST);
frame.add(bnt3,BorderLayout.NORTH);
frame.add(bnt4,BorderLayout.SOUTH);
frame.add(bnt5,BorderLayout.CENTER);
frame.setVisible(true);
}
}
运行结果:
3、表格布局
代码演示:
package com.zibo.lession01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.setBounds(300,300,500,500);
// frame.pack();//自动布局大小和位置
frame.setVisible(true);
}
}
运行结果: