MENU

事件模型

July 26, 2018 • Read: 2943 • Java阅读设置

ActionEvent


事件模型如何理解呢,举个例子,你老婆出门了,让你在家看孩子,难道你每过一分钟就去看一次孩子吗,对于计算机来说,我做一个按钮,难道就一直监听这个按钮使用没有吗,这样的话对于资源消耗太大了,所以最好的办法就是在孩子手上系个铃铛,孩子一有动静铃铛就响,你过去看就行了。反映到计算机上,你可以把某个代码写好,然后告诉某个对象比方说button,当button一执行,就让他自动调用你写好的代码,这样对计算机的压力就并不是很重,这个叫钩子函数,也叫回调函数

import java.awt.*;
import java.awt.event.*;
public class TestActionEvent {
    public static void main(String[] args) {
        Frame f = new Frame("Test");
        Button b = new Button("Press Me");
        Monitor bh = new Monitor();
        b.addActionListener(bh);
        f.add(b,BorderLayout.CENTER);
        f.pack();
        f.setVisible(true);
    }
}
class Monitor implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("a button has been pressed");
    }
}

当事件发生时,就会自动调用actionPerformed函数,参数是一个事件对象,这个对象中包装了很多关于事件的信息,比方说发生的时间,发生的情况等等

import java.awt.*;
import java.awt.event.*;
public class TestActionEvent {
    public static void main(String[] args) {
        Frame f = new Frame("Test");
        Button b1 = new Button("start");
        Button b2 = new Button("stop");
        Monitor2 bh = new Monitor2();
        b1.addActionListener(bh);
        b2.addActionListener(bh);
        b2.setActionCommand("Game Over");
        f.add(b1,BorderLayout.NORTH);
        f.add(b2,BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
    }
}
class Monitor2 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("a button has been pressed " + e.getActionCommand());
    }
}

MouseEvent

java中事件监听的接口名称都非常有规律,监听动作的叫ActionListener,监听鼠标的叫MouseListener,监听键盘的叫KeyListener

MouseLinsenter中有四个方法

但是如果我们写自己的鼠标监听器类,继承这个接口需要实现四个方法,非常麻烦,所以一般我们都直接继承MouseAdapter类,这个类已经实现了MouseListener的所有方法,我们只要重写其方法即可

import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyMouseAdapter {
    public static void main(String[] args) {
        new MyFrame1("drawing...");
    }
}
class MyFrame1 extends Frame {
    ArrayList<Point> points = null;
    MyFrame1(String s) {
        super(s);
        points = new ArrayList();
        setLayout(null);
        setBounds(300,300,400,300);
        setBackground(new Color(204,204,255));
        setVisible(true);
        addMouseListener(new Monitor());
    }
    public void paint(Graphics g) {
        Iterator<Point> i = points.iterator();
        while(i.hasNext()) {
            Point p = (Point)i.next();
            g.setColor(Color.blue);
            g.fillOval(p.x,p.y,8,8);
        }
    }
    public void addPoint(Point p) {
        points.add(p);
    }
}
class Monitor extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
        MyFrame1 f = (MyFrame1)e.getSource();
        f.addPoint(new Point(e.getX(),e.getY()));
        f.repaint();
    }
}

这是一个画点的程序,鼠标在Frame上点击一下就会在当前位置点出一个点,需要注意的是,repaint方法的执行过程可以理解为:update()->paint(),首先更新,然后画点,所以repaint方法在画图的时候必须加上

WindowEvent

之前我们做的所有窗口程序,都无法通过右上角的“X”关掉窗口,只能停止程序关闭窗口,下面我们就通过WindowEvent来设置关闭,下图是WindowListener接口中的方法

import java.awt.*;
import java.awt.event.*;
public class WindowsClose {
    public static void main(String[] args) {
        new MyFrame55("MyFrame");
    }
}
class MyFrame55 extends Frame {
    MyFrame55(String s) {
        super(s);
        setLayout(null);
        setBounds(300,300,400,300);
        setBackground(new Color(204,204,255));
        setVisible(true);
        addWindowListener(new MyWindowMonitor());
    }
    class MyWindowMonitor extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            System.exit(0);
        }
    }
}

同样的,因为接口的方法太多,我们自己实现太麻烦,可以通过继承WindowAdapter类来重写某些方法。这个程序实现了关闭窗口的效果

KeyEvent

下图是KeyListener接口的方法,同样的,也有一个类实现了这个接口,叫KeyAdapter,接下来我们直接做一个摁键盘上下左右,然后打印对应的字符串

import java.awt.*;
import java.awt.event.*;
public class TestKey {
    public static void main(String[] args) {
        new KeyFrame().launchFrame();
    }
}
class KeyFrame extends Frame {
    public void launchFrame() {
        setSize(200,200);
        setLocation(300,300);
        addKeyListener(new MyKeyMonitor());
        setVisible(true);
    }
    class MyKeyMonitor extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch(keyCode) {
            case KeyEvent.VK_UP:System.out.println("UP");break;
            case KeyEvent.VK_DOWN:System.out.println("DOWN");break;
            case KeyEvent.VK_LEFT:System.out.println("LEFT");break;
            case KeyEvent.VK_RIGHT:System.out.println("RIGHT");break;
            }
        }
    }
}

键盘上的每一个值,在KeyEvent类中都有对应的虚拟数值,只要进行比对就行

Last Modified: May 12, 2021
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment