1. 簡介
Gui的核心技術:
- Swing AWT
為什么市面很少使用GUI技術?
- 因為界面不美觀。
- 幾 M 的小工具,需要安裝很大的jre環(huán)境!
為什么我們要學習?
- 可以寫出自己心中想要的一些小工具,比如:貪吃蛇小游戲。
- 工作時候,有可能需要維護到swing界面,概率極??!
- 了解MVC架構,了解監(jiān)聽!
2. 常用組件
- 窗口
- 彈窗
- 面板
- 文本框
- 列表框
- 按鈕
- 圖片
- 監(jiān)聽事件
- 鼠標
- 鍵盤事件
- 破解工具
3. AWT
3.1AWT介紹

img
3.2組件和容器
窗口
package com.yyh.gui.frame;
import java.awt.*;
/**
* Gui-frame窗口學習
* @author yyh
* @create 2021-10-29 16:28
**/
public class TestFrame01 {
public static void main(String[] args) {
//窗口
Frame frame = new Frame("yyh");
//設置可見性
frame.setVisible(true);
//設置窗口大小
frame.setSize(500,500);
//設置顏色
frame.setBackground(new Color(51, 146, 232));
//彈出初始位置
frame.setLocation(300,300);
//設置大小是否固定,默認為:true-不固定
frame.setResizable(true);
}
}
窗口無法關閉;停止java程序運行

窗口
- 多個窗口
package com.yyh.gui.frame;
import java.awt.*;
/**
* 測試多個窗口
* @author yyh
* @create 2021-10-29 16:39
**/
public class TestFrame02 {
//同時展示多個窗口
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200,Color.cyan);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200,Color.green);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200,Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200,Color.yellow);
}
static class MyFrame extends Frame{
//可能存在多個窗口,我需要一個計數器來進行計數
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
//id自增
super("yyh+"+(++id));
//設置可見性
setVisible(true);
//設置彈出位置 和 長寬
setBounds(x,y,w,h);
//設置背景顏色
setBackground(color);
}
}
}

多窗口
面板
package com.yyh.gui.panel;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* gui-panel面板學習
* @author yyh
* @create 2021-10-29 16:52
**/
public class TestPanel01 {
//panel 可以看做一個空間,但不能單獨存在
public static void main(String[] args) {
//布局的概念
Panel panel = new Panel();
Frame frame = new Frame();
//設置布局
frame.setLayout(null);
//坐標
frame.setBounds(300,300,500,500);
//背景色
frame.setBackground(new Color(47, 106, 236));
//panel設置坐標,相對于Frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(218, 214, 214));
//panel依托于Frame
frame.add(panel);
//設置可見
frame.setVisible(true);
//監(jiān)聽事件,監(jiān)聽窗口關閉事件 System.exit(0)
//適配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//結束程序
System.exit(0);
}
});
}
}
窗口關閉
//監(jiān)聽事件,監(jiān)聽窗口關閉事件 System.exit(0)
//適配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//結束程序
System.exit(0);
}
});

面板
3.3布局管理器
流式布局
package com.yyh.gui.layout;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 流式布局
* @author yyh
* @create 2021-10-29 17:42
**/
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//坐標
frame.setBounds(300,300,500,500);
//背景色
frame.setBackground(new Color(47, 106, 236));
//設置按鈕
Button button = new Button("yyh1");
Button button1 = new Button("yyh2");
Button button2 = new Button("yyh3");
//設置為流式布局
frame.setLayout(new FlowLayout()); //默認居中
// frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //左
//添加按鈕
frame.add(button);
frame.add(button1);
frame.add(button2);
//顯示
frame.setVisible(true);
//監(jiān)聽事件,監(jiān)聽窗口關閉事件 System.exit(0)
//適配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//結束程序
System.exit(0);
}
});
}
}

流式布局
東西南北中
package com.yyh.gui.layout;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 東南西北中布局
* @author yyh
* @create 2021-11-02 15:17
**/
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("yyh");
//東南西北中布局
frame.add(new Button("EAST"),BorderLayout.EAST);
frame.add(new Button("SOUTH"),BorderLayout.SOUTH);
frame.add(new Button("WEST"),BorderLayout.WEST);
frame.add(new Button("NORTH"),BorderLayout.NORTH);
frame.add(new Button("CENTER"),BorderLayout.CENTER);
//可見
frame.setVisible(true);
//自適應大小
//frame.pack();
//設置定位和大小
frame.setBounds(300,300,500,400);
//添加監(jiān)聽事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//結束程序
System.exit(0);
}
});
}
}

東南西北中
表格布局
package com.yyh.gui.layout;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 表格布局
* @author yyh
* @create 2021-11-02 15:58
**/
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("yyh");
//布局,兩列三行
frame.setLayout(new GridLayout(2,3));
for (int i = 1; i <= 6; i++) {
frame.add(new Button(i + ""));
}
//設置可見
frame.setVisible(true);
//設置定位和大小
frame.setBounds(300,300,500,400);
//添加監(jiān)聽事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//關閉程序
System.exit(0);
}
});
}
}

表格布局
嵌套布局
package com.yyh.gui.layout;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 嵌套布局
* @author yyh
* @create 2021-11-02 16:16
**/
public class TestLayout {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
this.loadMyframe();
}
//加載窗口
public void loadMyframe(){
//組件
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new BorderLayout());
Panel p3 = new Panel(new GridLayout(2,1));
Panel p4 = new Panel(new GridLayout(2,2));
//布局
this.setLayout(new GridLayout(2,1));
//上
this.add(p1);
p1.add(new Button("1"),BorderLayout.WEST);
p1.add(new Button("2"),BorderLayout.EAST);
p3.add(new Button("p3-1"));
p3.add(new Button("p3-2"));
p1.add(p3,BorderLayout.CENTER);
//下
this.add(p2);
p2.add(new Button("3"),BorderLayout.WEST);
p2.add(new Button("4"),BorderLayout.EAST);
for (int i = 1; i <= 4; i++) {
p4.add(new Button("p4-" + i));
}
p2.add(p4,BorderLayout.CENTER);
//設置可見
this.setVisible(true);
//設置定位和大小
this.setBounds(300,300,500,400);
//設置監(jiān)聽事件
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

嵌套布局
3.4 事件監(jiān)聽
#### 單按鈕事件監(jiān)聽
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 單按鈕監(jiān)聽事件
* @author yyh
* @create 2021-11-03 18:03
**/
public class TsetActionEvent {
public static void main(String[] args) {
//組件
Frame frame = new Frame("按鈕監(jiān)聽事件");
Button button = new Button("check!??!");
//監(jiān)聽
button.addActionListener(new MyActionEvent());
frame.add(button);
//設置可見
frame.setVisible(true);
//自適應大小
frame.pack();
frame.setBounds(300,300,500,400);
//退出程序
closeWindow(frame);
}
//退出程序
private static void closeWindow(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件監(jiān)聽類
class MyActionEvent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按鈕監(jiān)聽事件");
}
}

按鈕監(jiān)聽
多按鈕監(jiān)聽同一事件
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 多按鈕監(jiān)聽同一事件
* @author yyh
* @create 2021-11-04 9:39
**/
public class TsetActionEvent2 {
public static void main(String[] args) {
//組件
Frame frame = new Frame("yyh");
Button b1 = new Button("begin");
Button b2 = new Button("end");
//可以顯示的定義觸發(fā)后返回的命令,如果不顯示定義,則會走默認的值!可以多個按鈕只寫一個監(jiān)聽潴
b2.setActionCommand("stop");
//監(jiān)聽
b1.addActionListener(new MyActionEvent2());
b2.addActionListener(new MyActionEvent2());
//布局
frame.add(b1,BorderLayout.WEST);
frame.add(b2,BorderLayout.EAST);
frame.pack();
frame.setBounds(300,300,500,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件監(jiān)聽類
class MyActionEvent2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}

多按鈕監(jiān)聽同一事件
文本框事件監(jiān)聽
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 文本框
* @author yyh
* @create 2021-11-04 9:57
**/
public class Testfield {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField field = new TextField();
this.add(field);
//監(jiān)聽文本框
field.addActionListener(new MyAction());
//設置字體掩碼
field.setEchoChar('*');
this.setVisible(true);
this.setBounds(500,300,500,400);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//監(jiān)聽類
class MyAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField)e.getSource();
System.out.println(field.getText());
field.setText("");
}
}

文本框
簡易計算器
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 計算器
* @author yyh
* @create 2021-11-04 10:25
**/
public class TestCalculator {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
//組件
TextField t1 = new TextField(5);
TextField t2 = new TextField(5);
TextField t3 = new TextField(15);
Button button = new Button(" = ");
Label label = new Label(" + ");
public Calculator(){
//布局
this.setLayout(new FlowLayout());
this.add(t1);
this.add(label);
this.add(t2);
this.add(button);
this.add(t3);
//監(jiān)聽
button.addActionListener(new MyAction());
this.setVisible(true);
this.setBounds(500,300,500,400);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//監(jiān)聽器類---》內部類
//內部類好處,暢通無阻的訪問外部類
private class MyAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
t3.setText(""+(n1+n2));
t1.setText("");
t2.setText("");
}
}
}

簡易計算器
畫筆
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 畫筆
* @author yyh
* @create 2021-11-04 10:48
**/
public class TestPaint {
public static void main(String[] args) {
new MyPaint();
}
}
class MyPaint extends Frame{
public MyPaint(){
this.setVisible(true);
this.setBounds(500,300,500,400);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//畫筆
@Override
public void paint(Graphics g) {
//super.paint(graphics);
//畫筆需要有顏色
g.setColor(Color.red);
//可以畫畫
g.drawOval(100,100,100,100);//空心
g.fillOval(200,200,100,100);//實心
g.setColor(Color.green);
g.fillOval(200,200,100,100);//實心
//養(yǎng)成習慣,畫筆用完將它還原到最初的顏色
}
}

畫筆
鼠標事件監(jiān)聽
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
/**
* 鼠標監(jiān)聽事件
* @author yyh
* @create 2021-11-04 11:36
**/
public class TestMouse {
public static void main(String[] args) {
new MyFrame1("畫筆");
}
}
//鼠標類
class MyFrame1 extends Frame {
//畫畫需要畫筆,需要監(jiān)聽鼠標當前的位置
//需要集合來存儲這個點
//存鼠標的點
ArrayList points = new ArrayList<>();
public MyFrame1(String title) {
super(title);
setBounds(200, 200, 400, 400);
//鼠標監(jiān)聽器,針對這個窗口
this.addMouseListener(new MyMouse());
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
//畫畫,監(jiān)聽鼠標的事件
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 MyMouse extends MouseAdapter {
//鼠標 按下,彈起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame1 myFrame = (MyFrame1) e.getSource();
//當我們點擊的時候就會在界面上產生一個點
//這個點就是鼠標的點
myFrame.addPoint(new Point(e.getX(), e.getY()));
//每一次點擊鼠標都需要重新畫一遍
myFrame.repaint();//刷新
}
}
}

鼠標監(jiān)聽
窗口事件監(jiān)聽
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 窗口監(jiān)聽
* @author yyh
* @create 2021-11-04 15:10
**/
public class TestWindow {
public static void main(String[] args) {
new MyWindow("窗口監(jiān)聽");
}
}
class MyWindow extends Frame{
public MyWindow(String title){
super(title);
this.setBounds(200, 200, 400, 400);
this.setVisible(true);
this.setBackground(Color.cyan);
this.addWindowListener(new WindowAdapter() {
//關閉程序
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
//打開窗口
@Override
public void windowOpened(WindowEvent e) {
System.out.println("打開窗口");
}
//關閉窗口
@Override
public void windowClosed(WindowEvent e) {
System.out.println("關閉窗口");
}
//最小化
@Override
public void windowIconified(WindowEvent e) {
System.out.println("最小化");
}
//最大化
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("最大化");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("激活窗口");
}
});
}
}

窗口監(jiān)聽
鍵盤事件監(jiān)聽
package com.yyh.gui.listener;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 鍵盤監(jiān)聽
* @author yyh
* @create 2021-11-04 15:24
**/
public class TestKey {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
public MyKeyFrame(){
this.setBounds(200, 200, 400, 400);
this.setVisible(true);
this.setBackground(Color.cyan);
this.addWindowListener(new WindowAdapter() {
//關閉程序
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE){
System.out.println("空格--跳!!!");
}
}
});
}
}

鍵盤監(jiān)聽
總結:
1.Frame是一個頂級窗口2.Panel無法單獨顯示,必須添加到某個容器中。
3.布局管理器
- 流式布局
- 東西南北中布局
- 表格布局
- 嵌套布局
4.組件常用屬性:大小,定位,背景顏色,可見性,監(jiān)聽!
5.事件監(jiān)聽
- 按鈕
- 文本框
- 鼠標
- 窗口
- 鍵盤