java - keylistener 和 runnable 不工作

标签 java keylistener runnable

你能看出问题所在吗?线程运行良好,但砖 block 不想响应关键监听器。我尝试测试 keylistener 是否最终收到事件,但它甚至没有执行 system.out.println

import java.awt.*;//imports
import java.util.*;
import java.applet.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Start extends Applet implements Runnable, KeyListener// where i put the keylistener in
{

    DrawBackground dbg = new DrawBackground();
    Brick brick = new Brick();
    Thread gameLoop;

    public void init() {
        addKeyListener(this);// i add the key listener
    }

    public void start() {
        Thread gameLoop = new Thread(this);
        gameLoop.start();
    }

    public void run() {
        while (true) {

            brick.update(1);
            repaint();
            try {
                Thread.sleep(17);
            }
            catch (InterruptedException e) {
            }

        }
    }

    public void stop() {

    }

    public void paint(Graphics g)// with out any paint it works if im changing
                                 // somthing like a lable
    {
        dbg.paint(g, this);
        brick.paint(g, this);
    }

    public void keyPressed(KeyEvent e)// test to see if it works
    {
        System.out.println("why");
        if (e.getKeyCode() == 37) {
            brick.left();
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {

    }
}

这是我要移动的 Brick 类

import java.awt.*;
import java.util.*;
import java.applet.*;

public class Brick {

    public int dy = 40;
    public int yStart = -20;
    public int time = 0;
    public int dx = 0;
    public int xStart = 0;
    public int start = 1;

    public void paint(Graphics g, Start sp) {
        Dimension screenSize = sp.getSize();
        int sheight = screenSize.height;
        int swidth = screenSize.width;
        if (start == 1) {
            xStart = swidth - (int) (swidth / 2.5);
            start = 0;
        }
        int bWidth = swidth / 15;
        int bHeight = swidth / 15;
        int time = 0;
        g.setColor(Color.red);
        g.fillRect(xStart, yStart, bWidth, bHeight);
    }

    public void update(int x) {
        if (time == 60) {
            time = 0;
            yStart += dy;
        }
        else {
            time += x;
        }
    }

    public void left() {
        xStart -= dx;
    }
}

最佳答案

您似乎没有设置键盘焦点。请参阅this question.

在添加 keyListener 后,我总是使用 setFocusable(true) ,它对我有用,但该问题的答案有更好的解决方案。

关于java - keylistener 和 runnable 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207585/

相关文章:

java - 在 Cucumber 中的步骤定义之间共享状态

java - 如何通过按键盘上的 DELETE 删除 JTable 中的行

java - Java调用 "call"方法时无法存储Vector数据

java - 蒙戈 : how to count several arrays in one aggregation via java's mongoTemplate

java - 获取 HashMap 中的前 10 个值

switch 语句上的 Java Enum

每帧调用的Java函数

java - 设置插入符位置,其中插入符不在 JTextArea 中

java - 如何让动画在 JAVA 中通过键盘输入运行?

java - 处理程序、可运行和服务 : I put everything together and it doesn't really work