一、介绍
java事件处理是采取“委派事件模型”。当事件发生时,产生事件的对象,会把此“信息”传递给"事件的监听者"处理,这里所说的"信息"实际上就是java.awt.event事件类库里某个类所创建的对象,把它称为"事件的对象"。
机制示意图:
说明:事件源发生一个事件过后,就会产生一个事件对象,整个事件对象会传递给事件的监听者,监听者监听到这个事件就会进行处理
各概念介绍:
1. 事件源:事件源就是一个产生事件的对象,比如按钮,窗口,按键等
2. 事件:事件就是承载事件源状态改变时的对象,比如当键盘事件、鼠标事件、窗口事件等等,会生成一个事件对象,该对象保存着当前事件很多信息,比如KeyEvent(键盘事件)对象有含有被按下键的Code值。
java.awt.event包和javax,swing.event包中定义了各种事件类型
3. 事件类型:可以通过查阅jdk文档得知
二、事件监听器接口
(1)当事件源产生一个事件,可以传送给事件监听者处理
(2)事件监听者实际上就是一个类,该类实现了某个事件监听器接口,它就可以作为一个事件监听者,对接受到的事件进行处理
(3)事件监听器接口有多种,不同的事件监听器接口可以监听不同的事件,一个类可以实现多个监听接口
(4)这些接口在java.awt.event包和javax.swing.event包中定义。可自行查阅
三、案例
当我们用java绘制了一个坦克、如果让坦克通过键盘的控制动起来?
(1)第一步:让画板所在的类实现KeyListener接口,成为一个事件监听者
public class MyPanel extends JPanel implements KeyListener
(2)第二步:通过实现接口方法中处理事件
@Override
public void keyPressed(KeyEvent e) {int moveSpeed = enemyTank2.getMoveSpeed();//坦克移动速度int keyCode = e.getKeyCode();//键盘按键信息int x = enemyTank2.getX();//坦克的x坐标int y = enemyTank2.getY();//坦克的y坐标if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S){//下箭头和W按键控制坦克向下移动enemyTank2.setDirection(2);enemyTank2.setY(y+moveSpeed);} else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {//左箭头和A按键控制坦克向左移动enemyTank2.setDirection(3);enemyTank2.setX(x-moveSpeed);} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {//上箭头和W按键控制坦克向上移动enemyTank2.setDirection(0);enemyTank2.setY(y-moveSpeed);} else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {//右箭头和D按键控制坦克向右移动enemyTank2.setDirection(1);enemyTank2.setX(x+moveSpeed);}//通过1,2,3,4来控制坦克的速度if (keyCode == KeyEvent.VK_1) enemyTank2.setMoveSpeed(1);else if (keyCode == KeyEvent.VK_2) enemyTank2.setMoveSpeed(2);else if (keyCode == KeyEvent.VK_3) enemyTank2.setMoveSpeed(3);else if (keyCode == KeyEvent.VK_4) enemyTank2.setMoveSpeed(4);else if (keyCode == KeyEvent.VK_5) enemyTank2.setMoveSpeed(5);repaint();
}
(3)第三步:给画框类加入键盘事件监听者
public class draw extends JFrame {private MyPanel myPanel = null;public draw() {myPanel = new MyPanel();this.add(myPanel);//添加键盘监听者this.addKeyListener(myPanel);this.setSize(1000, 800);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}
}
四、案例完整代码
一共六个类,以下代码是经过改良的,和上述代码有些许不同
public class TankPosition {private int direction = 2;//坦克方向private int moveSpeed = 1;//坦克速度private int x;//x轴的坐标private int y;//y轴的坐标public TankPosition(int x, int y) {this.x = x;this.y = y;}public void moveUp(){direction = 0;y -= moveSpeed;}public void moveRight(){direction = 1;x += moveSpeed;}public void moveDown(){direction = 2;y += moveSpeed;}public void moveLeft(){direction = 3;x -= moveSpeed;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getDirection() {return direction;}public void setDirection(int direction) {this.direction = direction;}public int getMoveSpeed() {return moveSpeed;}public void setMoveSpeed(int moveSpeed) {this.moveSpeed = moveSpeed;}
}
public class MyTank extends TankPosition{public MyTank(int x, int y) {super(x, y);super.setDirection(0);}}
public class EnemyTank extends TankPosition{static public int number = 3;public EnemyTank(int x, int y) {super(x, y);}}
public class MyPanel extends JPanel implements KeyListener {private TankPosition goodTank1 = null;private List<EnemyTank> enemyTanks = new Vector<>();public MyPanel() {goodTank1 = new MyTank(100,500);createEnemyTank();}@Overridepublic void paint(Graphics g) {super.paint(g);g.fillRect(0,0,1000, 800);drawATank(goodTank1, g);for (EnemyTank enemyTank : enemyTanks) {drawATank(enemyTank, g);}}public void createEnemyTank(){//number表示敌人坦克数量int number = EnemyTank.number;int x = 0 ;int y = 100 ;for (int i = 1; i <= number; i++) {enemyTanks.add(new EnemyTank(x + i * 100, y));}}public void drawATank(TankPosition myTank, Graphics g){int type = myTank instanceof MyTank ? 1 : 0;g.setColor(type == 1 ? Color.cyan : Color.red);int x = myTank.getX();//坦克的x坐标int y = myTank.getY();//坦克的y坐标int direction = myTank.getDirection();//坦克的方向switch (direction){//判断方向,0,1,2,3分别表示上、右、下、左//根据不同方向绘制坦克case 0 :g.fill3DRect(x, y, 10,50, false);g.fill3DRect(x+30, y, 10,50, false);g.fill3DRect(x+10, y+10, 20,30, false);g.fillOval(x+10, y+15, 20, 20);g.drawLine(x+20, y+25, x+20, y);break;case 1 :g.fill3DRect(x, y, 50,10, false);g.fill3DRect(x, y+30, 50,10, false);g.fill3DRect(x+10, y+10, 30,20, false);g.fillOval(x+15, y+10, 20, 20);g.drawLine(x+25, y+20, x+50, y+20);break;case 2 :g.fill3DRect(x, y, 10,50, false);g.fill3DRect(x+30, y, 10,50, false);g.fill3DRect(x+10, y+10, 20,30, false);g.fillOval(x+10, y+15, 20, 20);g.drawLine(x+20, y+25, x+20, y+50);break;case 3 :g.fill3DRect(x, y, 50,10, false);g.fill3DRect(x, y+30, 50,10, false);g.fill3DRect(x+10, y+10, 30,20, false);g.fillOval(x+15, y+10, 20, 20);g.drawLine(x+25, y+20, x, y+20);break;default:System.out.println("暂时没有处理");}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {int moveSpeed = goodTank1.getMoveSpeed();//坦克移动速度int keyCode = e.getKeyCode();//键盘按键信息if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S){//下箭头和W按键控制坦克向下移动goodTank1.moveDown();} else if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {//左箭头和A按键控制坦克向左移动goodTank1.moveLeft();} else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {//上箭头和W按键控制坦克向上移动goodTank1.moveUp();} else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {//右箭头和D按键控制坦克向右移动goodTank1.moveRight();}//通过1,2,3,4来控制坦克的速度if (keyCode == KeyEvent.VK_1) goodTank1.setMoveSpeed(1);else if (keyCode == KeyEvent.VK_2) goodTank1.setMoveSpeed(2);else if (keyCode == KeyEvent.VK_3) goodTank1.setMoveSpeed(3);else if (keyCode == KeyEvent.VK_4) goodTank1.setMoveSpeed(4);else if (keyCode == KeyEvent.VK_5) goodTank1.setMoveSpeed(5);repaint();}@Overridepublic void keyReleased(KeyEvent e) {}
}
public class draw extends JFrame {private MyPanel myPanel = null;public draw() {myPanel = new MyPanel();this.add(myPanel);//添加键盘监听者this.addKeyListener(myPanel);this.setSize(1000, 800);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}
}
public class Execute {public static void main(String[] args) {new draw();}
}