GUI编程入门
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1. 简介
GUI的核心包: Swing AWT
- 因为界面不美观
- 需要jre环境!
为什么我们要学习?
- 可以写出想要的一些小工具
- 了解MVC架构, 了解监听!
2. AWT
2.1 Awt介绍
- 包含了很多的类和接口! GUI:图形用户界面编程
- 元素: 窗口,按钮,文本
- java.awt
2.2 组件和容器
2.2.1 Frame
public static void main(String[] args) {
//Frame, JDK, 看源码
Frame frame = new Frame("我的第一个Java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//需要设置窗口大小
frame.setSize(400, 400);
//设置背景颜色 Colors
frame.setBackground(new Color(1, 1, 1));
//弹出的初始位置
frame.setLocation(200, 200);
//设置窗口大小固定, true可变 false不可变
frame.setResizable(false);
}
问题:窗口关闭不了
尝试回顾封装
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.black);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.blue);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.pink);
}
}
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 面板Panle
解决了关闭事件
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, 37, 138));
//panel设置坐标, 相当于frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(180, 26, 24));
//frame.add(panel)把panel放在Frame中
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0);
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
2.3 布局管理器
- 流式布局(从左到右)
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);
}
- 东南西北中
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(200, 200);
//设置窗口可见
frame.setVisible(true);
}
- 表格布局
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 static void main(String[] args) {
//总的 Frame
Frame frame = new Frame();
frame.setSize(400, 300);
frame.setLocation(300, 400);
frame.setBackground(new Color(1, 1, 1));
frame.setVisible(true);
frame.setLayout(new GridLayout(2, 1));
//4个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2, 1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2, 2));
//上面
p1.add(new Button("East-1"), BorderLayout.EAST);
p1.add(new Button("West-1"), BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2, BorderLayout.CENTER);
//下面
p3.add(new Button("East-2"), BorderLayout.EAST);
p3.add(new Button("West-2"), BorderLayout.WEST);
p4.add(new Button("p4-btn-1"));
p4.add(new Button("p4-btn-2"));
p4.add(new Button("p4-btn-3"));
p4.add(new Button("p4-btn-4"));
p3.add(p4, BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
总结:
1. Frame是一个顶级窗口
2. Panel无法单独显示, 必须添加到某个容器中
3. 布局管理器
- 流式
- 东西南北中
- 表格
- 大小, 定位, 背景颜色, 可见性, 监听
2.4 事件监听
事件监听:当某个事情发生的时候, 干什么?
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");
}
}
多个按钮,共享一个事件
public class TestActionEvent02 {
public static void main(String[] args) {
// 两个按钮实现同一个监听
// 开始 停止
//按下按钮, 触发一些事件
Frame frame = new Frame("开始-停止");
Button button1 = new Button("start");
Button button2 = new Button("end");
//可以显示的定义触发会返回的命令, 如果不显示定义,则会走默认的值
//可以多个按钮只写一个监听类
button2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1, BorderLayout.NORTH);
frame.add(button2, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyMonitor implements ActionListener {
@Override
//e.getActionCommand() 获按钮信息
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了: msg" + e.getActionCommand());
if (e.getActionCommand().equals("start")) {
}
}
}
2.5 输入框 TextField 监听
public class TestText01 {
public static void main(String[] args) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame() {
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下Enter 就会触发这个输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//可以向下转型
TextField field = (TextField) e.getSource();//获得一些资源, 返回的一个对象
System.out.println(field.getText());//获得输入框的文本
field.setText("");//null是对象 而空是字符串
}