将 keylistener 添加到 main() 不起作用

Adding a keylistener to main() not working

我正在 Java 中制作一个游戏,球在没有用户控制的情况下不断向前移动,但用户可以使用向上和向下箭头键控制球的垂直移动,但是我我坚持通过我的主要方法将它的 keylistener class/whatever 添加到我的 JFrame。基本上,我做 add(keylistener_class/method) 和 Java(非常正确),抱怨我的 class 或方法不是组件。那么我的问题是我应该如何将 keylistener 添加到我的 Jframe 中?

干杯, 马特

代码:

    package org.ultraluminous.vertiball;

import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; public class Game extends JPanel{ static int x = 0; static int y = 0; static int ys = 0; private static void shift(){ x+=1; y+=ys; } public static void event(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_UP){ ys = 1; } if (e.getKeyCode() == KeyEvent.VK_DOWN){ ys = -1; } } public class key{ public void Listen() { KeyListener listener = new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }; addKeyListener(listener); setFocusable(true); } } @Override public void paint(Graphics g){ super.paint(g); Graphics2D Graph = (Graphics2D) g; Graph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Graph.setColor(Color.BLUE); Graph.drawOval(x, y, 50, 50); Graph.fillOval(x, y, 50, 50); } public static void main (String[] args){ JFrame Win = new JFrame("Pong"); Game game = new Game(); Win.add(new Game()); //Win.add... add what? I need to add the key listener here //but seem to be unable to. Win.setResizable(false); Win.setSize(900, 600); Win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Win.setVisible(true); while (true) { shift(); Win.repaint(); try { Thread.sleep(10); } catch (InterruptedException e){ e.printStackTrace(); } } }

您可以通过以下方式将 KeyListener 添加到您的 JPanel 中:

addKeyListener(new KeyListener(){

    ...//all the methods.

});

或者,如果不使用所有方法,您可以使用 KeyAdpater

addKeyListener(new KeyAdapter(){

    ...//Only the methods you want.

});

我通过将 KeyListener 添加到您的 JFrame 使其工作。或者,您可以为 KeyListener 创建一个单独的 class 并以这种方式添加它(而不是使用匿名内部 class)。这是我添加的内容:

public static void main (String[] args){
        JFrame Win = new JFrame("Pong");
            Game game = new Game();
            Win.add(new Game());
            Win.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {
                }

                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_UP){
                        ys = -1;
                    }
                    if (e.getKeyCode() == KeyEvent.VK_DOWN){
                        ys = 1;
                    }
                }

                @Override
                public void keyReleased(KeyEvent e) {
                }

            });
            Win.setResizable(false);
            Win.setSize(900, 600);
            Win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Win.setVisible(true);
            while (true) {
                shift();
                Win.repaint();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }   
      }
}