【问题标题】:How do I implement a key listener in the case of my code?对于我的代码,如何实现关键侦听器?
【发布时间】:2020-05-03 14:47:35
【问题描述】:
 public class Pong extends JPanel  {

    int x=0;
    int y=0;
    int a;
    int b;

    int border=30;
    boolean balldown=true;
    boolean ballright=true;
    int bounce=0;

    private void moveBall(){
        if (balldown==true){
            y++; 
            bounce++;
        }

        if (balldown==false){
            y--;
            bounce++;
        }

        if(y==getHeight()-border){
            balldown=false;
            bounce++;
        }

        if(y==0){
            balldown=true;
            bounce++;
        }

        if (ballright==true){
            x++;          
        }

        if (ballright==false){
            x--;
            bounce++;
        }

        if(x==getWidth()-30){
            ballright=false;
            bounce++;
        }
        if(x==0){
            ballright=true;
            bounce++;
        }   
    }

    @Override
    public void paint(Graphics g){

        super.paint(g);

        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 1080, 760);

        g.setColor(Color.WHITE);
        g.fillOval(x, y, 30, 30);

        g.setColor(Color.WHITE);
        g.fillRect(0, a, 30, 200);

        g.setColor(Color.WHITE);
        g.fillRect(980, b, 30, 200);

        g.fillRect(520, 0, 10, 760);
    }

    public  Pong() implements KeyListener {

     void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_UP) {
            a += 10;
            System.out.println("++++");
            return;
        }

        if (key == KeyEvent.VK_DOWN) {
            a -= 10;

        }
    }

}
public static void main(String[] args) throws InterruptedException {

    JFrame frame = new JFrame("Pong");
    frame.setSize(1024,760);
    frame.setVisible(true);
   // frame.createBufferStrategy(3);
    //BufferStrategy strategy=frame.getBufferStrategy();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Pong game=new Pong();
    frame.add(game);

    while(true){
        game.moveBall();
        game.repaint();
         Thread.sleep(1);
   }
 }

}

我是 jAVA 的初学者 我想实现一个 keylistener 来改变 2 个矩形的坐标,但我似乎无法让它工作。我在实现 KeyListener 的类上收到编译错误“;预期”。我知道这个错误意味着什么我不知道在这种情况下如何解决它

【问题讨论】:

  • 此行完全无效:public Pong() implements KeyListener - 您需要将其移至类声明:class Pong extends JPanel implements KeyListener

标签: java class abstract-class keylistener


【解决方案1】:

看起来您已将“实现 KeyListener”放入 Pong 构造函数中。将 implements 移到类声明中,并将 KeyListener / keyPressed 代码从构造函数 Pong() 中拉出

public class Pong extends JPanel implements KeyListener

【讨论】:

  • 我在发帖之前确实尝试过,它说“Pong 不是抽象的,并且不会覆盖 KeyListener 中的抽象方法 KeyReleased”如果我让 Pong 抽象,整个事情就会停止工作
  • 您不应该添加抽象 - 您只需要添加 KeyListener 的所有实现者必须具有的其他调用。定义“public void keyTyped(KeyEvent e) {}”和“public void keyReleased(KeyEvent e) {}”
  • 我明白了,非常感谢,现在没有错误了。但是我的 KeyPressed 方法仍然没有改变我的矩形的坐标 a。我写的方式有问题吗? @Override public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_UP) { a += 10; System.out.println("++++");返回; } if (key == KeyEvent.VK_DOWN) { a =a-10; }
  • 您是否将侦听器链接到源对象?查找适合您应用程序中组件的 addListener(x) 调用的详细信息。
  • 是的你我是对的没有那个。我仍然不知道如何添加监听器,但非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
  • 2015-12-11
相关资源
最近更新 更多