GUI编程
GUI怎么学?
-
GUI是什么?
Graphical User Interface 图形用户接口
-
GUI怎么玩?
-
平时如何运用?
组件:
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标事件
- 键盘事件
- 外挂
- 破解工具
1. 简介
GUI的核心技术:Swing AWT
-
因为界面不美观
-
需要JRE环境
(所以不流行)
为什么要学习GUI?
- 可以写出自己心中想要的小工具
- 工作中可能需要维护到Swing界面 (概率极小)
- 了解MVC架构,了解监听器
2. AWT(Abstract Window Toolkit)
2.1 AWT介绍
- 包含很多类和接口 实现GUI编程
- 包含很多元素:窗口、按钮、文本框
- java.awt包中两个常用的类:Frame
2.2 组件跟容器
-
Frame(窗口):
package com.ma.guistudy; import java.awt.*; //GUI的第一个界面 public class TestFrame { public static void main(String[] args) { //Java中有Frame类,可以自行看源码进行学习 Frame frame = new Frame("我的第一个Java图形界面窗口"); //设置可见性 frame.setVisible(true); //设置窗口大小 frame.setSize(400,400); //颜色 Color frame.setBackground(Color.CYAN); //弹出的初始位置 frame.setLocation(200,200); //设置大小固定,不可变大小 frame.setResizable(false); } }
-
工具类:
package com.ma.guistudy; import java.awt.*; // 创建一个类继承Frame类,拥有Frame的所有特性,然后可以添加一些自己想要的属性 class MyFrame extends Frame { //因为不知一个窗口,所以加个编号,便于计数 static int id = 0; //构造器,添加初始化的属性 public MyFrame(int x, int y, int w, int h, Color color, boolean a, boolean b){ //调用Frame,即该类的父类 super("MyFrame"+(++id)); setLocation(x,y); setSize(w,h); setBackground(color); setVisible(a); setResizable(b); } } public class TestFrame2 { public static void main(String[] args) { MyFrame myFrame = new MyFrame(400, 400, 100, 100, Color.red, true, false); } }
-
Pannel(面板):
- 面板不能单独存在,必须在Frame窗口中
package com.ma.guistudy; import java.awt.*; public class PanelStudy { public static void main(String[] args) { Frame frame = new Frame(); //查看源代码学习(Pannel必须在Frame中) Panel panel = new Panel(); //窗口布局 frame.setLayout(null); //名字 frame.setTitle("窗口"); //坐标 frame.setBounds(300,300,500,500); //颜色 Color color = Color.black; frame.setBackground(color); //显示 frame.setVisible(true); //Panel是在窗口中的 //坐标 panel.setBounds(100,100,100,100); //颜色 Color color1 = Color.white; panel.setBackground(color1); //把面板添加到窗口中 frame.add(panel); } }
//添加监听事件,窗口关闭事件
//适配器模式:选择合适的相配
WindowListener windowListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//窗口关闭的时候需要做的事情
System.exit(0);
}
};
//监听事件--结束程序
frame.addWindowListener(windowListener);
2.3布局管理器
-
流式布局:
package com.ma.guistudy; import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); Panel panel = new Panel(); //按钮组件 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); frame.setSize(500,500); frame.setVisible(true); frame.add(button1); frame.add(button2); frame.add(button3); //Frame设置流式布局 frame.setLayout(new FlowLayout(FlowLayout.LEFT)); } }
-
东西南北中
package com.ma.guistudy; import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); Panel panel = new Panel(); //按钮组件 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.setSize(500,500); frame.setVisible(true); //东西南北中布局 frame.add(center,BorderLayout.CENTER); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); } }
-
表格布局
package com.ma.guistudy; import java.awt.*; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); Panel panel = new Panel(); //按钮组件 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); Button button4 = new Button("button4"); Button button5 = new Button("button5"); frame.setSize(500,500); frame.setVisible(true); //东西南北中布局 frame.setLayout(new GridLayout(1,0)); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); //自动布局 frame.pack(); } }
2.4 事件监听
- 事件监听:当某个事情发生的时候,做什么(例:按下按钮触发事件)
package com.ma.guistudy;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//事件监听
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮触发事件
Frame frame = new Frame();
Button button = new Button();
// 添加事件监听 这里()中需要一个new ActionListener
//原理:点击按钮调用ActionListener的实现类运行实现类中的功能
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.setVisible(true);
//关闭窗口
colseWindow(frame);
}
//窗口关闭事件
public static void colseWindow(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("喵喵喵");
}
}