GUI编程--AWT-事件监听
事件监听
事件监听:当某个事情发生的时候,执行什么。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.luckylu.gui; 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( "测试" ); //因为,addAcionListener()需要一个ActionListener,所以我们需要构造一个ActionListener MyActionListerner myActionListerner = new MyActionListerner(); button.addActionListener(myActionListerner); //把按钮添加到窗口,并居中 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 MyActionListerner implements ActionListener{ // alt +enter 调出下列内容 @Override public void actionPerformed(ActionEvent e) { System.out.println( "aaa" ); //点击按钮后 显示结果aaa } } |
显示结果
多个按钮,共享一个监听
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.luckylu.gui; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestAcionTwo { 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) { //e.getActionCommand() 获取按钮的信息 System.out.println( "按钮被点击:msg=>" +e.getActionCommand()); } } |
实现结果
输入框TextField监听事件
输入框TextField,文本不能换行。
1 package com.luckylu.gui; 2 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 public class TestText01 { 8 public static void main(String[] args) { 9 //启动! 通常情况main方法只用来启动程序 10 new MyFrame2(); 11 } 12 } 13 class MyFrame2 extends Frame{ 14 public MyFrame2(){ 15 TextField textField = new TextField(); 16 add(textField); 17 //监听文本框输入的文字 18 MyActionListerner2 myActionListerner2 = new MyActionListerner2(); 19 //按下回车,就会触发这个输入框的事件 20 textField.addActionListener(myActionListerner2); 21 22 //可设置替换编码,用于密码设置 23 textField.setEchoChar('*'); 24 25 pack(); 26 setVisible(true); 27 } 28 } 29 class MyActionListerner2 implements ActionListener{ 30 @Override 31 public void actionPerformed(ActionEvent e) { 32 //e.getSource(); // 获取一些资源, 33 TextField field = (TextField) e.getSource(); // 获取一些资源,返回一些资源 34 System.out.println(field.getText()); //获取输入框文本 35 field.setText(""); // 不能设成null,Nul是字符串 36 37 } 38 }
显示结果
简易计算器,组合+内部类回顾学习!
OOP原则:组合大于继承!
1 class A extends B{ // 继承 2 3 } 4 5 class A{ // 组合 6 public B b; 7 }
简易计算器
方法1:面向过程的代码。
1 package com.luckylu.gui; 2 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 //简易计算器的类 8 public class TestCalc { 9 public static void main(String[] args) { 10 new Calculator(); 11 } 12 } 13 14 //计算器类 15 class Calculator extends Frame{ 16 public Calculator(){ 17 18 //三个文本框 19 TextField num1 = new TextField(10); // 多大字符数 20 TextField num2 = new TextField(10); // 多大字符数 21 TextField num3 = new TextField(11); // 多大字符数 22 //一个按钮 23 Button button = new Button("="); 24 button.addActionListener(new MyCalculatorListener(num1,num2,num3)); 25 26 //一个标签 27 Label label = new Label("+"); 28 //布局 29 setLayout(new FlowLayout()); 30 add(num1); 31 add(label); 32 add(num2); 33 add(button); 34 add(num3); 35 36 pack(); // 自适应 37 setVisible(true); //显示 38 39 } 40 } 41 42 //监听类 43 class MyCalculatorListener implements ActionListener{ 44 //获取三个变量 45 private TextField num1,num2,num3; 46 public MyCalculatorListener(TextField num1,TextField num2,TextField num3){ 47 this.num1=num1; 48 this.num2=num2; 49 this.num3=num3; 50 51 } 52 @Override 53 public void actionPerformed(ActionEvent e) { 54 //1.获得加数和被加数 55 int n1 = Integer.parseInt(num1.getText()); //获取文本,并转换文本类型为整数 56 int n2 = Integer.parseInt(num2.getText()); //获取文本,并转换文本类型为整数 57 58 //2.求和后,放入第三个框 59 num3.setText(""+(n1+n2)); 60 //3.清除前两个框 61 num1.setText(""); 62 num2.setText(""); 63 64 } 65 }
结果
方法2:优化为面向对象的代码(重点)
1 package com.luckylu.gui; 2 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 //简易计算器的类 8 public class TestCalc { 9 public static void main(String[] args) { 10 new Calculator().loadFrame(); //对象.方法 11 } 12 } 13 14 //计算器类 15 class Calculator extends Frame { 16 //属性 17 TextField num1, num2, num3; 18 19 //方法 20 public void loadFrame() { 21 // *组件* 22 //三个文本框 23 num1 = new TextField(10); // 多大字符数 24 num2 = new TextField(10); // 多大字符数 25 num3 = new TextField(11); // 多大字符数 26 //一个按钮 27 Button button = new Button("="); 28 //一个标签 29 Label label = new Label("+"); 30 31 // *监听* 32 button.addActionListener(new MyCalculatorListener(this)); 33 34 // *布局* 35 setLayout(new FlowLayout()); 36 add(num1); 37 add(label); 38 add(num2); 39 add(button); 40 add(num3); 41 42 pack(); // 自适应 43 setVisible(true); //显示 44 } 45 } 46 47 //监听类 48 class MyCalculatorListener implements ActionListener { 49 //获取计算器对象,在一个类中组合另外一个类; 50 Calculator calculator = null; 51 52 public MyCalculatorListener(Calculator calculator) { 53 this.calculator = calculator; 54 } 55 56 @Override 57 public void actionPerformed(ActionEvent e) { 58 //1.获得加数和被加数 59 //2.求和后,放入第三个框 60 //3.清除前两个框 61 62 int n1 = Integer.parseInt(calculator.num1.getText()); 63 int n2 = Integer.parseInt(calculator.num2.getText()); 64 calculator.num3.setText(""+(n1+n2)); 65 calculator.num1.setText(""); 66 calculator.num2.setText(""); 67 68 } 69 }
结果:同上
方法3:内部类,更高的包装。
package com.luckylu.gui;
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) {
new Calculator().loadFrame(); //对象.方法
}
}
//计算器类
class Calculator extends Frame {
//属性
TextField num1, num2, num3;
//方法
public void loadFrame() {
// *组件*
//三个文本框
num1 = new TextField("0",10); // 默认值0,10位数
num2 = new TextField("0",10); // 默认值0,10位数
num3 = new TextField(11); // 11位数
//一个按钮
Button button = new Button("=");
//一个标签
Label label = new Label("+");
// *监听*
button.addActionListener(new MyCalculatorListener());
// *布局*
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setTitle("简易加法计算器");
setBackground(Color.gray);
pack(); // 自适应
setVisible(true); //显示
addWindowListener(new WindowAdapter() { //关闭
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//监听器类
//内部类最好的好处,就是可以畅通无阻的访问外部类的属性;
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("");
}
}
}
结果:同上
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!