第21天 GUI编程3
GUI编程
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列边框
- 按钮
- 监听事件
- 鼠标事件
- 键盘事件
- 破解工具
1、简介
GUI的核心技术:Swing AWT
-
因为界面不美观
-
需要 jre 环境
为什么要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面,概率极小
- 了解MVC架构,了解监听!
2、AWT
2.1、AWT介绍
- 包含了很多类和接口!GUI:
- 元素:窗口、按钮、文本框
- java.awt包
2.2 组件和容器
1、Frame
package com.hang.lesson1;
import java.awt.*;
//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(new Color(93, 183, 91));//可以直接输入color.选择颜色 或者new color()
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:窗口关不掉,停止Java程序即可
尝试回顾封装
package com.hang.lesson1;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口new
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.BLUE);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.gray);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.green);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.BLUE);
}
}
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、面板
解决了窗口关闭的问题
package com.hang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//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(36, 152, 152));
//panel坐标设置,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(236, 131, 144));
//frame.add一个面板panel
frame.add(panel);
frame.setVisible(true);
//监听窗口关闭事件 system.exit();
//适配器模式:WindowAdapter
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时时要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
2.3、布局管理器
- 流式布局FlowLayout
package com.hang.lesson1;
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(FlowLayout.LEFT));
frame.setSize(200,200);
//把按钮加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
- 东西南北中BorderLayout
package com.hang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
//窗口关闭时时要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
- 表格布局GridLayout
package com.hang.lesson1;
import java.awt.*;
public class TestGridLayout {
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);
}
}
练习题:
![def72b38e16e0c6a0a1b7219a58daad](C:\Users\ADMINI~1\AppData\Local\Temp\WeChat Files\def72b38e16e0c6a0a1b7219a58daad.png)
package com.hang.lesson1;
import java.awt.*;
public class TestExercise {
public static void main(String[] args) {
Frame frame = new Frame("作业练习题!");
frame.setSize(400,300);
frame.setBackground(new Color(230, 140, 125));
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
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("Wast-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("Wast-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"));
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
先分析后绘制
![20b88249c35fe2954426eb34eb987d3](C:\Users\ADMINI~1\AppData\Local\Temp\WeChat Files\20b88249c35fe2954426eb34eb987d3.jpg)
总结:
- Frame是一个窗口
- Panel无法单独显示,必须放在某个容器中
- 布局管理器
- 流式
- 东西南北中
- 表格
- 大小、定位、背景颜色、可见性、监听!
2.4、事件监听
事件监听:当某个时间发生的时候,会干什么
package com.hang.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
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("aaaaa");
}
}
多个按钮共享一个事件:
package com.hang.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 TestActionTwo {
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();
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 MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获取按钮的信息
if (e.getActionCommand().equals("Start")){
System.out.println("按钮被点击了:msg==>"+e.getActionCommand());
}else{
System.out.println("按钮被点击了:msg==>"+e.getActionCommand());
}
}
}
2.5、输入框TextField
package com.hang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileReader;
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("");
}
}
2.6简易计算器,组合+内部类回顾复习!
oop原则:组合,大于继承
package com.hang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
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);
setVisible(true);
pack();
}
}
//监听器类
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.hang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1= new TextField(10);
num2= new TextField(10);
num3= new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(this));
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
}
//监听器类
class MyCalculatorListener implements ActionListener{
//获取计算器这个对象,在一个类里面组建另一个类
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator= calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
//2.将这个值 + 法运算后,放到第三个框
//3.清除前两个框
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
calculator.num3.setText("+"+(n1+n2));
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类:更好的包装
package com.hang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1= new TextField(10);
num2= new TextField(10);
num3= new TextField(20);
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
pack();
}
//监听器类
//内部类最大的好处就是可以畅通无阻的访问外部类的属性和方法~
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
//2.将这个值 + 法运算后,放到第三个框
//3.清除前两个框
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText("+"+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}