时间:2022-05-26 08:23:43 | 栏目:JAVA代码 | 点击:次
在GUI中事件源是事件发生的场所,通常是各个组件,如被单击的按钮;事件是用户对界面的操作,如操作键盘是触发的键盘事件;而事件处理者则是对收到的事件经行处理的程序,也称监听器。
注意:
事件处理者,即监听器为了能够处理某种类型的事件,必须实现与该事件类型相对的接口,即成为一个实现某接口的类对象。
事件是通过事件处理者包含的方法传入的,而该方法就是实现接口时必须实现的方法。
如ActionListener接口中的 void actionPerformed( ActionEvent e )方法。
如:单击按钮对 应于动作事件即(ActionEvent),按钮事件处理者是实现例动作事件对应的Listener接口的 类对象,需要调用按钮的addActionListener()方法注册,该类是重写ActionListener 接口中的void actionPerformed( ActionEvent e )方法
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test{
public static void main(String[] args) {
JFrame frame = new JFrame("理解事件处理");
frame.setDefaultCloseOperation(3);
frame.setLayout(null);
//创建按钮对象
JButton button = new JButton("请单击本次按钮");
button.setBounds(120,60,120,30);
frame.add(button);
//创建按钮监听器并注册,参数为事件处理者对象
ButtonHandler buttonHandler = new ButtonHandler();
button.addActionListener(buttonHandler);//与单击事件相关的授权处理的方法
frame.setBounds(400,200,400,200);
frame.setVisible(true);
}
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("单击一次按钮");
}
}
效果图:

例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test{
public static void main(String[] args) {
JFrame frame = new JFrame("深入掌握事件处理");
frame.setDefaultCloseOperation(3);
frame.setLayout(null);
//创建提示 信息
JLabel label1 = new JLabel("请在窗体内移动鼠标");
label1.setBounds(15,5,200,25);
frame.add(label1);
JLabel label2 = new JLabel("或按住鼠标左键拖动鼠标");
label2.setBounds(15,30,200,25);
frame.add(label2);
//创建文本框对象,检测
JTextField text = new JTextField(30);
text.setBounds(15,55,200,30);
frame.add(text);
//注册监听器,参数为事件处理者对象
MouseListenerImp mouse = new MouseListenerImp(text);//事件处理者类 实例化
frame.addMouseListener(mouse);
frame.addMouseMotionListener(mouse);
frame.addWindowListener(mouse);
frame.setBounds(500,250,300,150);
frame.setVisible(true);
}
}
//编写事件处理对象类 实现鼠标窗体的相关接口
class MouseListenerImp implements MouseListener, MouseMotionListener, WindowListener{
JTextField text;
public MouseListenerImp(JTextField text){
this.text = text;
}
public void mouseDragged(MouseEvent e){ //提供拖拽时的鼠标坐标
String s = "托拽鼠标,坐标: x =" + e.getX() + "y = " + e.getY();
text.setText(s);//在文本框中可输出
}
public void mouseEntered(MouseEvent e) { //检查鼠标是否在窗体内
String s = "鼠标离开了窗体";
text.setText(s);//在文本框中可输出
}
public void mouseExited(MouseEvent e) { //检查鼠标是否在窗体内
String s = "鼠标进入了窗体";
text.setText(s);//在文本框中可输出
}
public void windowClosing(WindowEvent e) {//为了式窗口能正常关闭
System.exit(1);
}
//不打算实现的方法,但是因为实现接口,所以要写出来
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
效果图:



上述代码的class MouseListenerImp implements MouseListener, MouseMotionListener, WindowListener 同时实现了三个接口,要实现接口需要写全部重写接口中的方法,但如果将其中某个接口用Adapter类 代替则可有】优化代码 即 extends MouseAdapter
事件的种类:
ComponentEvent:组件尺寸的变化和移动ContainerEvent:组件增加或移动WindowEvent:关闭窗口,激活窗口闭合,最大化,最小化FocusEvent:焦点的获得与失去KeyEvent:键的按下或释放MouseEvent:鼠标单击与移动ActionEvent:单击按钮,在文本框中按Enter键ItemEvent:从选择框或列表框中选择一项AdjustEvent:移滚动条上的滑块以调节数值TextEvent:文本对象的改变