一、GUI编程简介
- GUI编程(Graphical User Interface),即用户图形界面编程。
- GUI淘汰原因:界面不够美观,需要jre环境。
- 学习GUI的好处:1. 为后期学习MVC架构打好基础;2. 了解监听;3. 工作时可能需要维护到Swing界面(概率极小)。
- GUI的基本组件:窗口、弹窗、面板、文本框、列表框、按钮、图片、交互组件、监听事件、鼠标事件、键盘事件。
- GUI的核心:AWT和Swing。
二、AWT
1. Awt介绍
AWT是用于创建图形用户界面的一个工具包,它提供了一系列用于实现图形界面的组件,如窗口、按钮、文本框、对话框等。在JDK中针对每个组件都提供了对应的java类,这些类都位于java.awt包中。
2. 组件和容器
Component类通常被称为组件,根据Component的不同作用,可将其分为基本组件类和容器类。基本组件类是诸如按钮、文本框之类的图形界面元素,而容器类则是通过Component的子类Container类表示容器,它是一种特殊的组件,可以用来容纳其他组件。
3. Frame
第一个Frame窗口
//GUI的第一个界面
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(0, 64, 255)); //方式二
//弹出的初始位置
frame.setLocation(200, 200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口关闭不掉,只能停止Java程序。
多个弹窗(回顾封装)
public class TestFrame2 {
public static void main(String[] args) {
//显示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.magenta);
}
}
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);
}
}
4. Panel(面板)
同时解决了关闭事件。
//Panel 可以看作是一个空间,但是不能单独存在
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(16, 217, 47));
//Panel设置坐标,相对于frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(226, 11, 11));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 system.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
5. 布局管理器
- 流式布局(FlowLayout)
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.setLayout(new FlowLayout(FlowLayout.RIGHT)); //靠右
frame.setSize(200, 200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中(BorderLayout)
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.setVisible(true);
frame.setSize(200, 200);
}
}
- 表格布局(GridLayout)
public class TestGridLayout {
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");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3, 2)); //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 Test {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setSize(400, 300);
frame.setLocation(200, 200);
frame.setBackground(Color.red);
frame.setVisible(true);
frame.setLayout(new GridLayout(2, 1));
//4个面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2, 1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2, 2));
//上面
panel1.add(new Button("East-1"), BorderLayout.EAST);
panel1.add(new Button("West-1"), BorderLayout.WEST);
panel2.add(new Button("North-1"));
panel2.add(new Button("North-2"));
panel1.add(panel2, BorderLayout.CENTER);
//下面
panel3.add(new Button("East-2"), BorderLayout.EAST);
panel3.add(new Button("West-2"), BorderLayout.WEST);
//下面中间4个
panel4.add(new Button("South-1"));
panel4.add(new Button("South-2"));
panel4.add(new Button("South-3"));
panel4.add(new Button("South-4"));
panel3.add(panel4, BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结
- Frame是一个顶级窗口。
- Panel 无法单独显示,必须添加到某个容器中。
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小,定位,背景颜色,可见性,监听。
6. 事件监听(addActionListener)
当某个事情发生的时候,要去做什么?
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为 addActionListener() 需要一个 ActionListener ,所以我们需要构造一个 ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button, BorderLayout.CENTER);
frame.pack();
windowClose(frame); //关闭窗口
frame.setVisible(true);
}
//关闭窗口的事件
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")