GUI编程
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 外挂
- 破解工具
简介
GUI的核心技术:Swing AWT
-
因为界面不美观
-
运行需要jre环境
-
MVC的基础,了解MVC架构,了解监听
-
可以写出自己心中想要的一些小工具
-
工作时候,也可能需要维护到Swing界面,概率极小
AWT
AWT介绍
-
包含了很多的类和接口 GUI:图形用户界面
-
元素:窗口,按钮,文本框
-
java.awt
-
组件Component
- 按钮button
- 文本域Text Area
- Label
- ......
- 容器Container
- Window
- Frame
- 弹窗Dialog
- 面板Panel
- Applet
- Window
组件和容器
Frame
package com.dongfang.lesson01;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame JDK 看源码
Frame frame = new Frame("我的第一个Java图形界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color类
frame.setBackground(Color.cyan);
//弹出的初始位置 (0,0)在左上角
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题:发现窗口无法关闭,只能通过停止java程序
尝试回顾封装:
package com.dongfang.lesson01;
import java.awt.*;
public class TestFrame02 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 300, 300, Color.red);
MyFrame myFrame2 = new MyFrame(400, 100, 300, 300, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 400, 300, 300, Color.blue);
MyFrame myFrame4 = new MyFrame(400, 400, 300, 300, Color.green);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多个窗口,需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("My Frame"+(++id));
setBounds(x,y,w,h);//设置窗口大小和初始位置
setBackground(color);
setVisible(true);
}
}
![截图](C:\Users\34454\Pictures\Screenshots\屏幕截图 2024-09-22 103255.png)
面板Panel
package com.dongfang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//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(108, 175, 225));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(Color.yellow);
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件 System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
![截图](C:\Users\34454\Pictures\Screenshots\屏幕截图 2024-09-22 110349.png)
解决了关闭的问题
布局管理器
-
流式布局
package com.dongfang.lesson01; 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.LEFT));//按钮靠左 frame.setSize(200,200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); } }
-
东西南北中
package com.dongfang.lesson01; import java.awt.*; 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); } }
-
表格布局
package com.dongfang.lesson01; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); 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"); Button button6 = new Button("button6"); frame.setLayout(new GridLayout(3,2)); frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.pack();//Java函数,自动布局 frame.setVisible(true); } }
实现![截图](C:\Users\34454\Pictures\Screenshots\屏幕截图 2024-09-22 145509.png)
package com.dongfang.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//练习的Demo讲解
public class ExDemo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLUE);
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-button-1"));
p2.add(new Button("p2-button-2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
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() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
事件监听
package com.dongfang.lesson02;
import javax.swing.*;
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("aaa");
}
}
多个按钮,共享一个事件
package com.dongfang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e){
//获得按钮的信息
System.out.println("按钮被点击了:msg=>"+e.getActionCommand());
}
}
输入框TestField事件监听
package com.dongfang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 回车后清空输入框
}
}
简易计算器
oop原则:组合 大于 继承
完全改造为面向对象写法
package com.dongfang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
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);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalulator(this));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
/*
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 MyCalulator(this));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
*/
}
//监听器类
class MyCalulator implements ActionListener {
//获取计算器这个对象,在一个类中组合另外一个类
//private TextField num1,num2,num3;
Calculator calculator = null;
public MyCalulator(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e){
//获得加数和被加数
//int n1 = Integer.parseInt(num1.getText());
//int n2 = Integer.parseInt(num2.getText());
//将这个值+法运算后,放到第三个框
//num3.setText(""+(n1+n2));
//清除前两个框
//num1.setText("");
//num2.setText("");
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.dongfang.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
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);
//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 {
//获取计算器这个对象,在一个类中组合另外一个类
//private TextField num1,num2,num3;
/*
Calculator calculator = null;
public MyCalculator(Calculator calculator){
this.calculator = calculator;
}
*/
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
//int n1 = Integer.parseInt(num1.getText());
//int n2 = Integer.parseInt(num2.getText());
//将这个值+法运算后,放到第三个框
//num3.setText(""+(n1+n2));
//清除前两个框
//num1.setText("");
//num2.setText("");
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText("" + (n1 + n2));
num1.setText("");
num2.setText("");
/*
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 MyCalulator(this));
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
*/
}
}
}
/*
//监听器类
class MyCalulator implements ActionListener {
//获取计算器这个对象,在一个类中组合另外一个类
//private TextField num1,num2,num3;
Calculator calculator = null;
public MyCalulator(Calculator calculator){
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e){
//获得加数和被加数
//int n1 = Integer.parseInt(num1.getText());
//int n2 = Integer.parseInt(num2.getText());
//将这个值+法运算后,放到第三个框
//num3.setText(""+(n1+n2));
//清除前两个框
//num1.setText("");
//num2.setText("");
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("");
}
}
*/
标签:java,GUI,编程,public,Button,add,frame,new,Day01
From: https://www.cnblogs.com/dongfangyulv/p/18425964