如何在我的代码中实现键盘监听器?

huangapple 未分类评论49阅读模式
英文:

How do I implement a key listener in the case of my code?

问题

public class Pong extends JPanel implements KeyListener {

    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() {
        // ... (ball movement logic)
    }

    @Override
    public void paint(Graphics g) {
        // ... (painting logic)
    }

    public Pong() {
        addKeyListener(this); // Register the KeyListener to the panel
        setFocusable(true);   // Ensure that the panel receives keyboard focus
        setFocusTraversalKeysEnabled(false);
    }

    @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 -= 10;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // Handle key release if needed
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // Handle key typing if needed
    }

    public static void main(String[] args) throws InterruptedException {
        // ... (main method logic)
    }
}

Please note that the provided code might still require additional adjustments or corrections to run perfectly, as I've only translated and restructured the original code to include the KeyListener implementation.

英文:
 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(&quot;Pong&quot;);
    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);
   }
 }

}

Im a beginner at jAVA
I want to implement a keylistener that will change the coordonates of 2 rectangles but I cant seem to make it work. I get the compilation error that says "; expected " on the class that implements KeyListener. I know what the error means I dont know how to solve it in this case

答案1

得分: 0

看起来你把 "implements KeyListener" 放在了 Pong 构造函数里。将 implements 部分移到类声明中,并且将 KeyListener / keyPressed 的代码从构造函数 Pong() 中移出。

public class Pong extends JPanel implements KeyListener
英文:

It looks like you've put the "implements KeyListener" into the Pong contructor. Move the implements to the class declaration and pull the KeyListener / keyPressed code out of the constructor Pong()

public class Pong extends JPanel implements KeyListener

huangapple
  • 本文由 发表于 2020年5月3日 22:47:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/61576408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定