添加keylistener到main()不工作

时间:2016-03-26 12:46:26

标签: java swing jframe keylistener keyevent

我在Java中制作游戏,其中一个球在没有用户控制的情况下持续向前移动,但是用户可以使用向上和向下箭头键来控制球的垂直移动,但是我是坚持通过我的main方法添加keylistener类/无论它是什么,我的JFrame。基本上,我做add(keylistener_class/method)和Java(非常正确),抱怨我的类或方法不是一个组件。那么,我的问题是AM我应该如何将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(); } } }

2 个答案:

答案 0 :(得分:1)

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

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();
                }
            }   
      }
}

答案 1 :(得分:0)

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

addKeyListener(new KeyListener(){

    ...//all the methods.

});

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

addKeyListener(new KeyAdapter(){

    ...//Only the methods you want.

});