事件监听
我们首先谈谈事件监听。事件监听对于我们来说其实是很熟悉的,比如长按开机键,电脑就会开机;点击某个应用的图标,电脑就会启动某个应用;敲击键盘某个按键,键盘信息就被输入进电脑。然而,计算机是怎么知道我们做出了什么动作的呢,又是怎么知道怎么应对我们做出的动作的呢?这一系列理所当然的背后,就隐藏着事件监听的身影。
事件监听分为三个部分:
事件源对象、事件处理类 和 事件监听方法。
事件源对象:组件!!
在案例中,开机键、应用图标和键盘按键就是事件源对象。而在图形界面开发中:所有的容器组件和元素组件都可以成为事件源对象。在图形界面上,你的动作发生的组件,就是事件源对象。
事件监听方法:
事件监听方法是由事件源对象提供的,确定的组件就有确定的事件监听方法。事件监听方法的任务就是捕获事件源对象发生的动作,然后收集事件源对象的信息和动作的信息,将这些信息交给处理者。
addActionListener(ActionListener l) 动作事件监听方法,由组件对象直接调用
由按钮组件、输入框组件等确定的动作事件监听方法,可捕获类似按钮组件上的鼠标点击动作和捕获类似输入框组件上的键盘回车动作,然后收集事件源对象的信息和动作的信息,将这些信息交给监听方法的参数ActionListener的对象。
事件处理类(也叫事件接口)
动作监听方法中一定会带有一个类的对象作参数,就是事件处理类。当事件源对象捕获到动作信息后,会采取相应的措施。而这个措施,就由事件处理类来提供。
事件处理类的一般写法:
public class 类名 implements 接口名{
重写抽象方法...
}
没错,写事件处理类需要实现一个事件接口,这里的事件接口由事件源对象确定,包含了处理事件源对象上所发生动作方法。我们可以定义自己的接口,Java也给我们提供了许多的接口。
接口:
接口的定义格式(接口有很严格的格式要求,而且类实现(继承)接口也需要实现接口中所有的抽象方法)
Java中定义接口的关键字:interface
格式:
public interface 接口名 extends 接口,... {
//定义常量
public static final 数据类型 常量名 = 值;
//定义抽象方法
public abstract 返回值数据类型 抽象方法名(数据类型 参数名,...);
}
注意:1.接口不能实例化对象;
2.接口的访问修饰符只有一个public;
3.接口默认会提供public、static、final、abstract关键字(可省略不写)。
类实现(继承)接口的关键字:implements 。类可先继承一个父类,再实现一个接口, 丰富了类的继承关系。
笔记:
e.getSource();//获取事件源对象
e.getActionCommand();//获取指令
实例:
点击登录界面上的登录按钮,根据输入的账号和密码进行验证,如果正确则显示一个新窗体;如果错误则显示一个错误窗体。
//窗体类
package jianting;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Dimension;
public class Login {
public JTextField textName,textName2;
public static void main(String[] args) {
Login mylogin=new Login();
mylogin.InitUI();
}
public void InitUI(){
//组件类
JFrame frame=new JFrame();
frame.setTitle("Login");
frame.setSize(300,350);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
FlowLayout fl=new FlowLayout(FlowLayout.CENTER);
frame.setLayout(fl);
ImageIcon icon=new ImageIcon("C:\\Users\\lenovo\\Desktop\\179b2b02a0efd6cd.jpg");//取图片
JLabel lb=new JLabel(icon);//创建图片标签
lb.setPreferredSize(new Dimension(300,180));
frame.add(lb);//添加标签
JLabel labelName=new JLabel("账号:");
frame.add(labelName);
textName = new JTextField();
textName.setPreferredSize(new Dimension(210,30));//文本框大小
frame.add(textName);
JLabel labelName2=new JLabel("密码:");
frame.add(labelName2);
JPasswordField textName2 = new JPasswordField();
textName2.setPreferredSize(new Dimension(210,30));
frame.add(textName2);
//事件源
JButton bt =new JButton("登录");
frame.add(bt);
//监听 在程序运行时,人为操作前便已实例化好l0对象
LoginListener l0=new LoginListener(frame,textName,textName2);
bt.addActionListener(l0);
frame.setVisible(true);
}
}
//事件处理类
package jianting;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class LoginListener implements ActionListener{
public JFrame fr;
public String zhh,mima;
public JTextField t1,t2;
//参数都是人操作之前的参数,未更新
public LoginListener(JFrame frame,JTextField t1,JTextField t2){
this.fr=frame;this.t1=t1;this.t2=t2;
}
public void actionPerformed(ActionEvent e){
//更新后获取新值
zhh=t1.getText();mima=t2.getText();
if((zhh.equals("123"))&&(mima.equals("456"))){
newRightPage();
}
else newWrongPage();
fr.dispose();
}
public void newRightPage(){
JFrame fr=new JFrame();
fr.setSize(400,500);
fr.setTitle("登录成功!");
fr.setLocationRelativeTo(null);
fr.setVisible(true);
}
public void newWrongPage(){
JFrame fr=new JFrame();
fr.setSize(300,300);
fr.setTitle("账号或密码不正确!");
fr.setLocationRelativeTo(null);
fr.setVisible(true);
}
}
正确的账号密码显示正确登录窗体
错误账号密码显示错误登录窗体
QQ:375471598
微信:Q159837547
欢迎过来勾搭小邱同学,或者对文章提出修改意见,共同成长。