题外话:不知道为啥,标题“大佬”俩字无法使用了,那就更改为大神吧
文章目录- GUI编程
- 1、简介
- 2、AWT
- 2.1、介绍(Abstact Window Tools)
- 2.2、组件和容器
- 1、frame
- 2、面板Panel
- 3、布局管理器
- 4、事件监听
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标事件
- 键盘事件
- 破解工具
Gui的核心技术: Swing AWT
不流行原因:
- 因为界面不美观
- 需要jre 环境,过大
为什么学习?
- 了解MVC架构,了解监听
- 可以写出自己心中想要的一些小工具
- 也可能需要维护到Swing界面(老项目)
- 包含了很多的类和接口,GUI(图形用户界面)
- 元素:窗口、按钮、文本框等
- java.awt包
AWT的类:
-
组件(Component):
-
组件内容:botton / textarea / Lable
-
容器(Container,可以用于存放组件):
- 窗口(Window)
- frame
- Dialog
- 面板(Panel)
- Applet
- 窗口(Window)
-
- 弹出一个窗口
public class Testframe {
public static void main(String[] args) {
//frame对象,通过看源码查看用法
frame frame = new frame("第一个Java图像界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置颜色,Color类
// frame.setBackground(Color.black);
frame.setBackground(new Color(85,150,85));
//弹出的初始位置
frame.setLocation(400,400);
//设置大小固定
frame.setResizable(false);
}
}
运行程序弹出一个窗口,
存在问题,无法关闭,只能结束运行
关闭按钮,可以通过后面学习的监听和按钮事件触发关闭
- 通过封装的方式,生成多个窗口
public class Testframe02 {
public static void main(String[] args) {
//展示多个窗口,通过调用Myframe类的方式,生成窗口
Myframe myframe1 = new Myframe(100, 100, 200, 200, Color.blue);
Myframe myframe2 = new Myframe(300, 100, 200, 200, Color.red);
Myframe myframe3 = new Myframe(100, 300, 200, 200, Color.yellow);
Myframe myframe4 = new Myframe(300, 300, 200, 200, Color.green);
}
}
//封装一个类,用于展示多个窗口,不用重复每个窗口类相同步骤
class Myframe extends frame{
static int id = 0; //可能存在多个,需要一个计数器
//传入参数,减少重复操作
public Myframe(int x, int y, int w, int h, Color color){
super("Myframe:"+(++id));
setBackground(color);
setBounds(x, y, w, h);
setVisible(true);
}
}
2、面板Panel
在窗口中添加面板,并解决关闭按钮无效的问题(通过监听事件)
//面板,可以看成一个空间,但是不能单独存在,需要在frame上
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(new Color(40,161,35));
//panel设置坐标,相对于frame
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(109, 174, 236));
//frame添加panel
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件,System.exit(0)
// //不使用适配器模式需要将 WindowListener() 所有方法全部重写
// frame.addWindowListener(new WindowListener());
//
// //使用适配器模式,只需要重写需要用到的方法
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3、布局管理器
-
流式布局
-
东西南北中
-
表格
流式布局
public class TestFlowLayout {
public static void main(String[] args) {
frame frame = new frame();
//组件按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
// frame.setLayout(new FlowLayout()); //参数为空默认居中布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //靠左布局
frame.setSize(200,200);
//把按钮添加到frame中
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东西南北中
public class TestBorderLayout {
public static void main(String[] args) {
frame frame = new frame("TestBorderLayout");
Button East = new Button("East");
Button West = new Button("West");
Button South = new Button("South");
Button North = new Button("North");
Button Center = new Button("Center");
frame.add(East,BorderLayout.EAST);
frame.add(West,BorderLayout.WEST);
frame.add(South,BorderLayout.SOUTH);
frame.add(North,BorderLayout.NORTH);
frame.add(Center,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
frame.setLocation(700,500);
}
}
表格布局
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.pack(); //自动填充大小JAVA函数
frame.setVisible(true);
}
}
作业:用布局管理器生成画面:
作业答案样例:
public class TestWorkLayout {
public static void main(String[] args) {
frame frame = new frame("WorkLayout");
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");
Button btn7 = new Button("btn7");
Button btn8 = new Button("btn8");
Button btn9 = new Button("btn9");
Button btn10 = new Button("btn10");
frame.setLayout(new GridLayout(2,3));
Panel up = new Panel(new GridLayout(2,1));
up.add(btn2);
up.add(btn3);
Panel down = new Panel(new GridLayout(2,2));
down.add(btn6);
down.add(btn7);
down.add(btn8);
down.add(btn9);
frame.add(btn1);
frame.add(up);
frame.add(btn4);
frame.add(btn5);
frame.add(down);
frame.add(btn10);
frame.setSize(500,500);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
存在多种方法,解并不唯一
总结:
- frame是一个顶级窗口
- Panel无法单独显示,必须添加到某个容器中
- 布局管理器
- 流式
- 东西南北中
- 表格
- 面板的大小、定位、背景颜色、可见性、监听等
事件监听:当某个事情发生的时候,做什么事
监听按钮的点击事件样例
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮的时候,触发一些事件
frame frame = new frame();
Button button = new Button();
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个ActionListener的class
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
windowClose(frame); //工具类关闭窗口
}
private static void windowClose(frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮共享一个事件
public class TestActionTwo {
public static void main(String[] args) {
//两个按钮,实现同一个监听
//开始 停止
frame frame = new frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,否则默认为label内容
//可以多个按钮是写一个监听类
//显示定义
// button2.setActionCommand("button2-stop");
//定义监听方法,调用同一个监听方法,根据按钮不同的内容执行不同的操作
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.SOUTH);
frame.add(button2,BorderLayout.NORTH);
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(600,400);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand():获取按钮的信息
System.out.println("按钮点击,msg:"+e.getActionCommand());
//通过判断getActionCommand的内容,可以多个按钮共同使用同一个监听器,根据内容不同执行不同方法
if (e.getActionCommand().equals("start")){
System.out.println("程序正在运行中");
}
if (e.getActionCommand().equals("stop")){
System.exit(0);
}
}
}



