什么叫事件:事件监听

事件监听

1)什么叫事件监听?你在电脑上设置个按钮,通过点击按钮来反馈里面所显示出来的信息。这就叫事件监听。

2)事件监听概念:通过某一事物源对象(当某种事件发生时),然后向监听器传送某种事件对象,监听器里面封装了某种事件信息,接到事件对象后进行某种处理,这就是事件监听。

3)简单来说以学校放课铃作为事物源对象,然后放课铃声响起来即事件发生。假如学生是一个监听器,学生作为监听器接收到放课铃声响起,然后学生对放课铃声进行下课处理,这就是事件监听。

一、通过点击按钮设置监听打印出HellWorld

import java.awt.BorderLayout;import java.awt.Button;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//通过点击按钮设置监听打印出HellWorldpublic class TestActionEvent {public static void main(String[] args) {Frame f=new Frame("Java Frame");Button b=new Button("Press Me");Monitor m=new Monitor();b.addActionListener(m);f.add(b,BorderLayout.CENTER);f.pack();f.setVisible(true);}}class Monitor implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("HellWorld");}}

二、通过设置setActionCommand区分同一监听对象

import java.awt.BorderLayout;import java.awt.Button;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//通过设置setActionCommand区分同一监听对象public class TestActionEvent2 {public static void main(String[] args) {Frame f=new Frame("Java Frame");Button b1=new Button("Start");Button b2=new Button("Stop");Monitor2 m=new Monitor2();b1.addActionListener(m);b2.addActionListener(m);b2.setActionCommand("GAME OVER");f.add(b1,BorderLayout.NORTH);f.add(b2,BorderLayout.SOUTH);f.pack();f.setVisible(true);}}class Monitor2 implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("HellWorld"+e.getActionCommand());}}

三、通过文本监听对象

import java.awt.Frame;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//通过文本监听对象public class TFActionEvent {public static void main(String[] args) {new TFFrame();}}class TFFrame extends Frame{public TFFrame() {TextField tf=new TextField();//创建文本tf.addActionListener(new TFActionEventListener());;//在文本中添加监听对象TFActionEventListenertf.setEchoChar('*');//设置回显字符,输入看不到,输出显示add(tf);//在Frame窗口添加文本pack();//打包setVisible(true);}}class TFActionEventListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {TextField tf = (TextField) e.getSource();/*getSource()方法在java.awt.event包中的EventObject类 且返回值是Object对象,所以此处需强转*/System.out.println(tf.getText());//获得文本tf.setText("");//每每输出完后显示为空}}

四、通过多个文本监听四则运算

import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TFMath {public static void main(String[] args) {new TFFrame1().launchFrame();}}class TFFrame1 extends Frame{TextField num1,num2,num3;public void launchFrame(){num1=new TextField(10);num2=new TextField(10); num3=new TextField(15); //创建三个文本分别设置长度 Label lblPlus=new Label("+");//设置加号Button btnEquals=new Button("=");//设置等号按钮btnEquals.addActionListener(new MyMonitor1(this));//this指向TFFrame1,随时可访问TextField里面的成员变量setLayout(new FlowLayout());add(num1);add(lblPlus);add(num2);add(btnEquals);add(num3);//比如1+2=3里面共五个元素,以流水线方式布局添加pack();setVisible(true);}}class MyMonitor1 implements ActionListener{TFFrame1 tf=null;public MyMonitor1(TFFrame1 tf) {this.tf = tf;}@Overridepublic void actionPerformed(ActionEvent e) {int n1=Integer.parseInt(tf.num1.getText());//将文本字符串转换成Int类型再赋值给n1int n2=Integer.parseInt(tf.num2.getText());tf.num3.setText(""+(n1+n2));;//num3获得输出结果}}

五、鼠标监听事件

import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Point;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.ArrayList;import java.util.Iterator;public class MyMouseApdapter {public static void main(String[] args) {new MyFrame4("Java Frame");//自定义窗口命名}}class MyFrame4 extends Frame{//1)设置窗口以及鼠标监听事件ArrayList points=null;//自定义ArrayList集合points为空public MyFrame4(String s) {super(s);points = new ArrayList();//points初始化setLayout(null);//设置布局管理器为空,否则Frame默认布局管理器为BorderLayousetBounds(300, 300, 300, 300);//设置窗口坐标以及大小setBackground(new Color(204,204,204));//设置背景三原色setVisible(true);//设置视图可见this.addMouseListener(new Monitor1());//监听MyFrame4事件鼠标/*MouseAdapter接口实现MouseListener,Monitor1继承MouseAdapter*/}//2)public void paint(Graphics g){//调用paint画笔方法Iterator i = points.iterator();//迭代points集合while (i.hasNext()) {//如果仍有元素可以迭代,则返回 truePoint p = (Point) i.next();//返回迭代的下一个Point(点)g.setColor(Color.BLUE);//设置画笔颜色g.fillOval(p.x, p.y, 10, 10);//设置画笔图形}}public void addPoint(Point p){//自定义addPoint方法points.add(p);//points添加Point(点)}}class Monitor1 extends MouseAdapter{public void mousePressed(MouseEvent e) {//重写mousePressed方法MyFrame4 f=(MyFrame4)e.getSource();//getSource方法返回类型Object,事件源指针指向MyFrame4f.addPoint(new Point(e.getX(),e.getY()));//MyFrame4调用自定义addPoint方法,监听鼠标坐标位置f.repaint();//Frame调用父类Component中的repaint方法/*repaint()方法是一个具有刷新页面效果的方法,若不调用repaint方法图形发生变化后不会立刻显示 repaint调用update()清除当前显示并再调用paint()方法 如果不调用repaint()就不能实现每一次的刷新显示,不能立刻显示就无法马上进行下一步的绘画*/}}

六、匿名监听Windows关闭事件

import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class TestWindows {public static void main(String[] args) {new MyWindows("MyWindowAdapter");}}class MyWindows extends Frame{public MyWindows(String s) {super(s);setLayout(null);setBounds(300, 300, 300, 300);setVisible(true);this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);//0正常退出,-1非正常退出setVisible(false);}});}}

 

相关推荐

相关文章