addActionListener(this) 中的this问题

addActionListener(this) 中的this问题,第1张

this是当前对象,先不谈this可以换成什么,先谈谈这里的this干什么用的。

首先A.addActionListener(this)的作用就是给A加个监听器,所谓监听器其实就是一个普通类,只不过这个类有一个特别的方法而已,也就是ActionListener要求必须实现的方法。

当A发生事件的时候会调用监听器中定义的那个方法。

所以this在这里的作用就是提供了一个可以被调用的方法而已,你当然可以不用this用其他类的对象,只要这个对象实现了ActionListener接口,也就是写了ActionListener规定的方法。

一般我们开发时会专门写一些处理事件的类,比如你可以定义一个AButtonActionListener,专门处理A发生的事件,这样的话就可以:

A.addActionListener(new AButtonActionListener() )

当然,上面只是个例子,具体如何用要看你想做什么了。

你也可以把事件监听器写成匿名类:

b1.addActionListener(new Button())

b2.addActionListener(new Button())

改成:

b1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

t1.append("\n 你按下了“显示”按钮")}

})

b2.addActionLIstener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

t1.append("\n 你按下了“显示”按钮")}

})

这样写可以把每个按钮的事件分开写在不同的actionPerformed,不然就太乱了;

这样写的话不用再类上面写implements ActionListener

但会出现错误提示:1,把ActionListenner导入;

2,实现接口中的方法,(点这个的时候actionPerformed方法会自动写出来,你把要实现的内容写在里面就行了)

只要点左边的差号,根据提示就可以了

ActionListener是给你添加的组件添加监听,如果组件发生了动作,以便根据捕获的动作做出相应的响应。你把要做的响应写在actionPerformed方法中,就会自动按照你写的动作执行了。

import java.awt.*

import java.awt.event.*

import javax.swing.*

class Animation extends JFrame implements Runnable, ActionListener, KeyListener {

private Container cont

private JButton btnUp

private JButton btnDown

private ImageIcon imgPlane

private ImageIcon imgBullet

private int planeX

private int planeY

private int bulletX

private int bulletY

private int planeSpeed

private int bulletSpeed

private int planeChangeSpeed

private Thread thread

private static Animation animation//定义一个主界面的全局变量

public Animation() {

addKeyListener(this)

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE)

GameStartInit()

}

public void GameStartInit() {

cont = this.getContentPane()

this.setBounds(0, 0, 400, 300)

cont.setLayout(null)

imgPlane = new ImageIcon("plane.gif")

imgBullet = new ImageIcon("bullet.gif")

planeX = 0

planeY = 150

bulletX = 400

bulletY = 150

planeSpeed = 8

bulletSpeed = 8

planeChangeSpeed = 6

btnUp = new JButton("up")

btnDown = new JButton("down")

btnUp.setBounds(0, 240, 80, 30)

btnDown.setBounds(90, 240, 80, 30)

btnUp.addActionListener(this)

btnDown.addActionListener(this)

cont.add(btnUp)

cont.add(btnDown)

thread = new Thread(this)

thread.start()

this.setVisible(true)

this.requestFocus()//这里在定义完界面之后,再让主界面得到焦点,你原先键盘失灵的原因是,在运行的时候是按钮得到焦点,那么按键盘对按钮是没反应的

}

public void paint(Graphics grp) {

super.paint(grp)

imgPlane.paintIcon(this, grp, planeX, planeY)

imgBullet.paintIcon(this, grp, bulletX, bulletY)

}

public void run() {

while ((bulletX >0) &&(planeX <400)) {

repaint()

planeX = planeX + planeSpeed

bulletX = bulletX - bulletSpeed

try {

Thread.sleep(200)

} catch (Exception e) {

}

}

thread = null

}

public void actionPerformed(ActionEvent aEvt) {

JButton ob = (JButton) aEvt.getSource()

if (ob == btnUp) {

planeY -= planeChangeSpeed

}

if (ob == btnDown) {

planeY += planeChangeSpeed

}

animation.requestFocus()//每次按钮事件过后,都让主界面得到焦点,因为键盘事件是加载主界面上的

}

public void keyPressed(KeyEvent e) {

switch (e.getKeyCode()) {

case KeyEvent.VK_UP:

planeY = planeY - 5

break

case KeyEvent.VK_DOWN:

planeY = planeY + 5

break

case KeyEvent.VK_LEFT:

planeX = planeX - 5

break

case KeyEvent.VK_RIGHT:

planeX = planeX + 5

break

}

}

public void keyTyped(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

public static void main(String args[]) {

animation = new Animation()//定义了一个全局变量

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11604153.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存