该怎么学?
- 这是什么?
- 如何使用?
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1. 简介
Gui的核心技术: Swing AWT
-
因为界面不美观
-
需要jre环境
为什么要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到Swing界面,概率极小
- 了解MVC架构,了解监听!
2. AWT
2.1 AWT介绍
包含很难多累和接口!
GUI 图形用户界面
3. Swing
代码
第一个窗口
package com.kuang.lesson;
import java.awt.*;
public class TestFrame {
public static void main(String[] args){
// Frame, JDK , 看源码
Frame frame = new Frame("我的第一个Java图形界面窗口");
// 需要设置可见性
frame.setVisible(true);
// 设置窗口大小
frame.setSize(400, 400);
// 设置背景颜色
frame.setBackground(new Color(86, 183, 232));
// 弹出的初始位置
frame.setLocation(200, 200);
// 设置窗口大小固定
frame.setResizable(false);
}
}
显示多个窗口
package com.kuang.lesson;
import java.awt.*;
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.white);
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));
setVisible(true);
setBackground(color);
setLocation(x, y);
setSize(w, h);
setResizable(false);
}
}
面板 Panel
面板解决了窗口关闭问题
package com.kuang.lesson;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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(125, 125, 125));
// Panel 设置坐标,相对于frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(86, 183, 232));
// 将面板添加到窗口中
frame.add(panel);
frame.setVisible(true);
// 监听事件,监听窗口关闭事件 System.exit(0)
// 适配器模式
frame.addWindowListener(new WindowAdapter() {
// 窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// 结束程序
System.exit(0);
}
});
}
}
总结:
- Frame 是一个顶级窗口
- panel 无法单独显示
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小, 定位
流式
package com.kuang.lesson;
import java.awt.*;
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.RIGHT));
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setSize(200, 200);
// 将按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
东西南北中
package com.kuang.lesson;
import java.awt.*;
public class TestBoardLayout {
public static void main(String[] args) {
Frame frame = new Frame();
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);
}
}
表格
package com.kuang.lesson;
import java.awt.*;
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));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();
frame.setVisible(true);
}
}
4. 事件监听
4.1 事件监听,触发事件,执行动作
package com.kuang.lesson2;
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();
//因为,addActionlistener() 需要一个ActionListener, 所以我们需要有构造一个ActionListener
MyActtionListener myActtionListener = new MyActtionListener();
button.addActionListener(myActtionListener);
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 MyActtionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
4.2 多个按钮共享一个事件
package com.kuang.lesson2;
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 TestActionEvent02 {
public static void main(String[] args) {
// 两个按钮,实现同一个监听事件
// 开始 停止
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
// 可以显示的定义出发会返回的命令,如果不显示定义,则会走默认的值
// 可以多个按钮只写一个监听类
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);
windowClose(frame);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了: MSG " + e.getActionCommand());
}
}
4.3 输入框 TextField 监听
package com.kuang.lesson2;
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 TestText01 {
public static void main(String[] args) {
// 启动
MyFrame myFrame = new MyFrame();
windowClose(myFrame);
}
// 关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
// 监听这个文本框输入的文字
MyActionListener02 myActionListener02 = new MyActionListener02();
// 按下enter 就会触发这个输入框的事件
textField.addActionListener(myActionListener02);
setVisible(true);
pack();
}
}
class MyActionListener02 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource(); // 获得一些资源
System.out.println(field.getText());// 获得输入框的文本
}
}
4.4 简易计算器,组合+内部类回顾复习
OOP 原则: 组合 > 继承
class A extends B{
}
class A {
public B b;
}
实例: 计算器
LOW
package com.kuang.lesson2;
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 TestCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
windowClose(calculator);
}
// 关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
// 计算器类
class Calculator extends Frame {
public Calculator(){
//构造方法
// 3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1, num2, num3));
// 1个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
// 监听器类
class MyCalculatorListener implements ActionListener{
private TextField num1, num2, num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1. 获取加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2. 将前两个值相加放到第三个框
num3.setText("" + (n1 + n2));
//3. 清除前两个框
num1.setText("");
num2.setText("");
}
}
高级写法 : 完全面向对象
package com.kuang.lesson2;
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 TestCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
windowClose(calculator);
}
// 关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
// 计算器类
class Calculator extends Frame {
// 属性
TextField num1, num2, num3;
// 方法
public void loadFrame(){
// 3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
// 1个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
// 监听器类
class MyCalculatorListener implements ActionListener{
//private TextField num1, num2, num3;
// 获取计算机这个对象,在一个类中组合另外一个
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1. 获取加数和被加数
//int n1 = Integer.parseInt(num1.getText());
//int n2 = Integer.parseInt(num2.getText());
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2. 将前两个值相加放到第三个框
//num3.setText("" + (n1 + n2));
calculator.num3.setText("" + (n1 + n2));
//3. 清除前两个框
//num1.setText("");
//num2.setText("");
calculator.num1.setText("");
calculator.num2.setText("");
}
}
匿名内部类
package com.kuang.lesson2;
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 TestCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.loadFrame();
windowClose(calculator);
}
// 关闭窗口事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
// 计算器类
class Calculator extends Frame {
// 属性
TextField num1, num2, num3;
// 方法
public void loadFrame(){
// 3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
// 1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
// 1个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
// 监听器类
// 内部类的最大好处就是畅通无阻地访问外部属性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1. 获取加数和被加数
//int n1 = Integer.parseInt(num1.getText());
//int n2 = Integer.parseInt(num2.getText());
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2. 将前两个值相加放到第三个框
//num3.setText("" + (n1 + n2));
num3.setText("" + (n1 + n2));
//3. 清除前两个框
//num1.setText("");
//num2.setText("");
num1.setText("");
num2.setText("");
}
}
}
4.5 画笔
package com.kuang.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPaint {
public static void main(String[] args) {
Mypaint mypaint = new Mypaint();
mypaint.loadFrame();
closeWindow(mypaint);
}
private static void closeWindow(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Mypaint extends Frame {
public void loadFrame(){
setBounds(200,200, 600, 400);
setVisible(true);
}
// 画笔
@Override
public void paint(Graphics g){
// 画笔,需要有颜色,画笔可以画画
g.setColor(Color.RED);
// 空心圆
g.drawOval(0, 0, 100, 100);
// 实心圆
g.fillOval(200, 200, 100 ,100);
g.setColor(Color.GREEN);
g.fillRect(0, 0, 100, 50);
}
}
4.6 鼠标监听事件
package com.kuang.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
// 鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
class MyFrame extends Frame {
// 花花需要画笔,需要监听鼠标地当前位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200, 200, 400, 300);
// 存储鼠标点
points = new ArrayList<>();
setVisible(true);
// 鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g){
// 画画, 监听鼠标事件
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x, point.y, 10, 10);
}
}
// 添加一个点到界面上
public void addPoint(Point point){
points.add(point);
}
// 适配器模式
private class MyMouseListener extends MouseAdapter{
// 鼠标 按下, 弹起, 按住不放
@Override
public void mousePressed(MouseEvent e){
MyFrame frame = (MyFrame)e.getSource();
// 这个我们点击的的时候,就会在界面上产生一个点
// 这个点就是鼠标的点
frame.addPoint(new Point(e.getX(), e.getY()));
// 每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
}
}
4.7 键盘监听
package com.kuang.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame(){
setBounds(1, 2, 300, 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// 获得当前键盘下地键是哪一个,当前的码
int keyCode = e.getKeyCode();
// 不用记这个值,直接静态属性 VK_XXX
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
// 根据按下不同的操作,产生不同的结果
}
});
}
}