如何将密钥监听器添加到JFrame?

时间:2016-06-02 00:53:26

标签: java swing jframe keylistener

我正在尝试使用我的键来控制火箭动画,并尝试使用KeyListener方法,但是,我的程序不会对我的按键做出反应。我已经尝试将keylistener添加到我的JFrame,但它没有调用它,因为JFrame是一个单独的静态方法。如果你运行它,你将没有图像,但如果你运行,请替换为矩形。你能告诉我的错误吗?在此先感谢。

这是我的代码:

import java.awt.*;
import hsa.Console;
import javax.imageio.*;
import java.io.*;

import java.awt.Graphics;
import java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;

import java.util.Arrays;

public class MA_RocketLauncher extends JPanel implements ActionListener, KeyListener
{

    static Console c;

    //***********************************Main Program*********************************

    public static void main (String[] args)
    {

        c = new Console (27, 85);

        MainProgram ();

    }

    //********************************JFrame******************************************

    public static void JFrame()
    {

        JFrame jf = new JFrame();

        MA_RocketLauncher r = new MA_RocketLauncher();

        jf.setSize(692, 542);
        jf.setTitle("Rocket Launcher");
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setResizable(false);
        jf.add(r);

    }

    //********************************All Methods*************************************

    public static void MainProgram ()
    {

        //IntroMessage();
        //Password();

        JFrame ();

    }

    //***********************************Timer****************************************

    Timer t = new Timer(12, this);

    int y = 240, velY = 0, y2 = 300, velY2 = 0, x = 20, velX = 0, x2 = -5, velX2 = 0, y3 = -5, velY3 = 0,
        y4 = -5, velY4 = 0;

    //*********************************Rocket*****************************************

    public void paintComponent (Graphics c)
    {  

        super.paintComponent (c);

        Color darkGrey = new Color (32, 32, 32);

        c.setColor (darkGrey);
        c.fillRect (0, 0, 700, 550);

        c.setColor (Color.black);
        c.drawRect (18, 0, 639, 379);

        Image picture3 = loadImage ("Canada.png");
        c.drawImage (picture3, 19, 1, null);

        Font f2 = new Font ("Monaco", Font.BOLD, 20);

        c.setColor (Color.white);
        c.setFont (f2);

        c.drawString ("LIVE VIEW", 550, 25);

        Font f = new Font ("Courier New", Font.BOLD, 30);

        c.setColor (Color.red);
        c.setFont (f);

        c.drawString ("Status: ", 30, 420);

        c.setColor (Color.green);

        c.drawString ("        Launching!", 30, 420);

        Font f3 = new Font ("Courier New", Font.BOLD, 25);

        c.setFont (f3);

        c.drawString ("Clearance Level 1 Achieved", 30, 460);
        c.drawString ("ETA: 30 seconds", 30, 500);

        Image picture2 = loadImage ("circle_flag_us_america_united_states-512.png");
        c.drawImage (picture2, 627, 475, null);

        Font f1 = new Font ("Courier New", Font.BOLD, 12);

        c.setColor (Color.green);
        c.setFont (f1);

        c.drawString ("Property of the United States government", 340, 490);
        c.drawString ("Confidential Information", 450, 505);

        Image picture4 = loadImage ("RocketUp.png");

        c.drawImage (picture4, 100, y, null);
        c.drawImage (picture4, 500, y, null);

        t.start ();

        addKeyListener(this);

        setFocusable(true);

        setFocusTraversalKeysEnabled(false);

        if (y < -130)
        {

            c.setColor (darkGrey);
            c.fillRect (0, 0, 700, 550);

            c.setColor (Color.black);
            c.drawRect (18, 0, 639, 379);

            Image picture5 = loadImage ("Sky.png");

            c.drawImage (picture5, 19, 1, null);

            Image picture6 = loadImage ("RocketUpMedium.png");

            c.drawImage (picture6, 100, y2, null);
            c.drawImage (picture6, 500, y2, null);

            c.setColor (Color.white);
            c.setFont (f2);

            c.drawString ("LIVE VIEW", 550, 25);

            c.setColor (Color.red);
            c.setFont (f);

            c.drawString ("Status: ", 30, 420);

            c.setColor (Color.green);

            c.drawString ("        Launched!", 30, 420);

            c.setFont (f3);

            c.drawString ("Clearance Level 2 Achieved", 30, 460);
            c.drawString ("ETA: 20 seconds", 30, 500);

            c.drawImage (picture2, 627, 475, null);

            c.setColor (Color.green);
            c.setFont (f1);

            c.drawString ("Property of the United States government", 340, 490);
            c.drawString ("Confidential Information", 450, 505);

            if (y2 < -130)
            {

                c.setColor (darkGrey);
                c.fillRect (0, 0, 700, 550);

                c.setColor (Color.black);
                c.drawRect (18, 0, 639, 379);

                c.setFont (f2);
                c.setColor (Color.white);

                c.drawString ("LIVE VIEW", 550, 25);

                c.setColor (Color.red);
                c.setFont (f);

                c.drawString ("Status: ", 30, 420);

                c.setColor (Color.green);

                c.drawString ("        En Route!", 30, 420);

                c.setFont (f3);

                c.drawString ("Clearance Level 3 Achieved", 30, 460);
                c.drawString ("ETA: 10 seconds", 30, 500);

                c.drawImage (picture2, 627, 475, null);

                c.setColor (Color.green);
                c.setFont (f1);

                c.drawString ("Property of the United States government", 340, 490);
                c.drawString ("Confidential Information", 450, 505);

                Image picture7 = loadImage ("Space.jpg");

                c.drawImage (picture7, 19, 1, null);

                c.setFont (f2);
                c.setColor (Color.white);

                c.drawString ("LIVE VIEW", 550, 25);

                Image picture8 = loadImage ("Earth.png");

                c.drawImage (picture8, 19, 1, null);

                Image picture9 = loadImage ("RocketRightSmall.png");

                c.drawImage (picture9, x, 100, null);
                c.drawImage (picture9, x, 200, null);

                c.setColor (darkGrey);
                c.fillRect (658, 0, 40, 379);

                if (x > 658)
                {

                    c.setColor (darkGrey);
                    c.fillRect (0, 0, 700, 550);

                    c.setColor (Color.black);
                    c.drawRect (18, 0, 639, 379);

                    c.drawImage (picture7, 19, 1, null);

                    c.setFont (f2);
                    c.setColor (Color.white);

                    c.drawString ("LIVE VIEW", 550, 25);

                    c.setColor (Color.red);
                    c.setFont (f);

                    c.drawString ("Status: ", 30, 420);

                    c.setColor (Color.green);

                    c.drawString ("        Arriving!", 30, 420);

                    c.setFont (f3);

                    c.drawString ("Clearance Level 4 Achieved", 30, 460);
                    c.drawString ("ETA: 5 seconds", 30, 500);

                    c.drawImage (picture2, 627, 475, null);

                    c.setColor (Color.green);
                    c.setFont (f1);

                    c.drawString ("Property of the United States government", 340, 490);
                    c.drawString ("Confidential Information", 450, 505);

                    Image picture10 = loadImage ("Moon.png");

                    c.drawImage (picture10, 400, 100, null);

                    c.drawImage (picture9, x2, 100, null);
                    c.drawImage (picture9, x2, 200, null);

                    c.setColor (darkGrey);
                    c.fillRect (0, 0, 19, 379);

                    if (x2 > 450)
                    {

                        c.setColor (darkGrey);
                        c.fillRect (0, 0, 700, 550);

                        c.setColor (Color.black);
                        c.drawRect (18, 0, 639, 379);

                        c.drawImage (picture7, 19, 1, null);

                        c.setFont (f2);
                        c.setColor (Color.white);

                        c.drawString ("LIVE VIEW", 550, 25);

                        Image picture11 = loadImage ("MoonBig.png");

                        c.drawImage (picture11, 150, 250, null);

                        c.setColor (darkGrey);
                        c.fillRect (0, 380, 700, 170);

                        c.drawImage (picture2, 627, 475, null);

                        c.setColor (Color.green);
                        c.setFont (f1);

                        c.drawString ("Property of the United States government", 340, 490);
                        c.drawString ("Confidential Information", 450, 505);

                        c.setColor (Color.red);
                        c.setFont (f);

                        c.drawString ("Status: ", 30, 420);

                        c.setColor (Color.green);

                        c.drawString ("        Landing!", 30, 420);

                        c.setFont (f3);

                        c.drawString ("Clearance Level 5 Achieved", 30, 460);
                        c.drawString ("ETA: 2.5 seconds", 30, 500);

                        c.drawImage (picture6, 290, y3, null);
                        c.drawImage (picture6, 390, y3, null);

                        if (y3 > 290)
                        {

                            c.setColor (darkGrey);
                            c.fillRect (0, 0, 700, 550);

                            c.setColor (Color.black);
                            c.drawRect (18, 0, 639, 379);

                            Image picture12 = loadImage ("MoonSurface.jpg");
                            c.drawImage (picture12, 19, 1, null);

                            c.setFont (f2);
                            c.setColor (Color.white);

                            c.drawString ("LIVE VIEW", 550, 25);

                            c.drawImage (picture2, 627, 475, null);

                            c.setColor (Color.green);
                            c.setFont (f1);

                            c.drawString ("Property of the United States government", 340, 490);
                            c.drawString ("Confidential Information", 450, 505);

                            c.setColor (Color.red);
                            c.setFont (f);

                            c.drawString ("Status: ", 30, 420);

                            c.setColor (Color.green);

                            c.drawString ("        Taxi!", 30, 420);

                            c.setFont (f3);

                            c.drawString ("Clearance Level 6 Achieved", 30, 460);
                            c.drawString ("ETA: 1 second", 30, 500);

                            c.drawImage (picture4, 100, y4, null);
                            c.drawImage (picture4, 500, y4, null);

                            if (y4 > 260)
                            {

                                c.setColor (darkGrey);
                                c.fillRect (0, 0, 700, 550);

                                c.setColor (Color.black);
                                c.drawRect (18, 0, 639, 379);

                                c.drawImage (picture12, 19, 1, null);

                                c.setFont (f2);
                                c.setColor (Color.white);

                                c.drawString ("LIVE VIEW", 550, 25);

                                c.drawImage (picture2, 627, 475, null);

                                c.setColor (Color.green);
                                c.setFont (f1);

                                c.drawString ("Property of the United States government", 340, 490);
                                c.drawString ("Confidential Information", 450, 505);

                                c.setColor (Color.red);
                                c.setFont (f);

                                c.drawString ("Status: ", 30, 420);

                                c.setColor (Color.green);

                                c.drawString ("        Landed!", 30, 420);

                                c.setFont (f3);

                                c.drawString ("Clearance Level 7 Achieved", 30, 460);
                                c.drawString ("ETA: 0 seconds", 30, 500);

                                c.drawImage (picture4, 100, 260, null);
                                c.drawImage (picture4, 500, 260, null);

                                c.setColor(Color.green);

                                Font f4 = new Font ("Monaco", Font.BOLD, 100);
                                c.setFont(f4);

                                c.drawString("SUCCESS", 90, 200);

                            }

                        }

                    }

                }

            }

        }

    }   

    //********************************Action Listener Method**************************

    public void actionPerformed (ActionEvent e)
    {

        if (y < -130)
        {
            y2 = y2 - velY2;

            if (y2 < -130)
            {
                x = x + velX;

                if (x > 658)
                {

                    x2 = x2 + velX2;

                    if (x2 > 450)
                    {

                        y3 = y3 + velY3;

                        if (y3 > 290)
                        {

                            y4 = y4 + velY4;

                        }

                    }

                }

            }

        }

        y = y - velY;

        repaint();

    }

    //******************************Key Listener Method*******************************

    public void keyPressed (KeyEvent e)
    {

        int c = e.getKeyCode();

        if (c == KeyEvent.VK_LEFT)
        {

            velX = -1;

            velX2 = -1;

            velY = 0;

            velY2 = 0;

            velY3 = 0;

            velY4 = 0;

        }

        if (c == KeyEvent.VK_UP)
        {

            velY = -1;

            velY2 = -1;

            velY3 = -1;

            velY4 = -1;

            velX = 0;

            velX2 = 0;

        }

        if (c == KeyEvent.VK_RIGHT)
        {

            velX = +1;

            velX2 = +1;

            velY = 0;

            velY2 = 0;

            velY3 = 0;

            velY4 = 0;

        }

        if (c == KeyEvent.VK_DOWN)
        {

            velY = +1;

            velY2 = +1;

            velY3 = +1;

            velY4 = +1;

            velX = 0;

            velX2 = 0;

        }

    }

    public void keyTyped(KeyEvent e){}

    public void keyReleased(KeyEvent e){}

    //********************************Image Loader Method*****************************

    public static Image loadImage (String name)
    {

        Image img = null;

        try
        {

            img = ImageIO.read (new File (name));

        }

        catch (IOException e)
        {

        }

        return img;

    }

2 个答案:

答案 0 :(得分:3)

paintComponent(...)只能用于绘画。它不应该:

  1. 读取图像文件
  2. 设置面板的属性:setFocusable(..),setFocusKeysEnabled(...)
  3. 向面板添加侦听器:addKeyListener(...)
  4. 以上所有代码都应该在构造函数中为ONCE。

    每次重绘()时都会调用paintComponent()方法,因此您不想继续阅读图像或添加侦听器。

    你发布的代码太多了。如果您遇到问题,请发布一个显示问题的正确SSCCE

    听众无法工作的常见原因是面板没有焦点。

    常见的解决方案是使用Key Bindings,而不是KeyListener。有关详细信息,请阅读HOw to Use Key Bindings上的Swing教程中的部分。

答案 1 :(得分:0)

所以我错误的是将计时器和键命令放在pain组件中,所以我所要做的就是创建一个公共类并将命令放在那里。我还加载了主程序中的图像,并在paint组件中调用它们,如上面的答案所述。

这是我工作代码的一部分:

    import java.awt.*;
    import hsa.Console;
    import javax.imageio.*;
    import java.io.*;

    import java.awt.Graphics;
    import java.awt.Color;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;

    import javax.swing.*;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.Timer;

    import java.util.Arrays;

    public class MA_RocketLauncher extends JPanel implements ActionListener, KeyListener
    {

        static Console c;

        //***********************************Main Program*********************************

        public static void main (String[] args)
        {

            c = new Console (27, 85);

            MainProgram ();

        }

        //********************************JFrame******************************************

        public static void JFrame()
        {

            JFrame jf = new JFrame();

            MA_RocketLauncher r = new MA_RocketLauncher();

            jf.setSize(692, 542);
            jf.setTitle("Rocket Launcher");
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setResizable(false);
            jf.add(r);

        }

        //********************************All Methods*************************************

        public static void MainProgram ()
        {

            //IntroMessage();
            //Password();

            JFrame ();

        }

        //***********************************Timer****************************************

        Timer t = new Timer(10, this);

        int y = 240, velY = 0, y2 = 300, velY2 = 0, x = 20, velX = 0, x2 = -5, velX2 = 0, y3 = -5, velY3 = 0,
            y4 = -5, velY4 = 0;

        public MA_RocketLauncher()
        {

            t.start ();

            addKeyListener(this);

            setFocusable(true);

            setFocusTraversalKeysEnabled(false);

        }

        //**********************************Images****************************************

        Image picture2 = loadImage ("circle_flag_us_america_united_states-512.png");

        Image picture3 = loadImage ("Canada.png");

        Image picture4 = loadImage ("RocketUp.png");

        Image picture5 = loadImage ("Sky.png");

        Image picture6 = loadImage ("RocketUpMedium.png");

        Image picture7 = loadImage ("Space.jpg");

        Image picture8 = loadImage ("Earth.png");

        Image picture9 = loadImage ("RocketRightSmall.png");

        Image picture10 = loadImage ("Moon.png");

        Image picture11 = loadImage ("MoonBig.png");

        Image picture12 = loadImage ("MoonSurface.jpg");

        //*********************************Rocket*****************************************

        public void paintComponent (Graphics c)
        {  

            super.paintComponent (c);

            //Graphics

        }   

        //********************************Action Listener Method**************************

        public void actionPerformed (ActionEvent e)
        {

            if (y < -130)
            {
                y2 = y2 - velY2;

                if (y2 < -130)
                {
                    x = x + velX;

                    if (x > 658)
                    {

                        x2 = x2 + velX2;

                        if (x2 > 450)
                        {

                            y3 = y3 + velY3;

                            if (y3 > 290)
                            {

                                y4 = y4 + velY4;

                            }

                        }

                    }

                }

            }

            y = y - velY;

            repaint();

        }

        //******************************Key Listener Method*******************************

        public void keyPressed (KeyEvent e)
        {

            int c = e.getKeyCode();

            if (c == KeyEvent.VK_LEFT)
            {

                velX = -1;

                velX2 = -1;

            }

            if (c == KeyEvent.VK_UP)
            {

                if (y < -130)
                {

                    y = -1000;

                }

                else 
                {

                    velY = 1;

                }

                if (y2 < -130)
                {

                    y2 = -1000;

                }

                else
                {

                    velY2 = 1;

                }

                velY3 = -1;

                velY4 = -1;

            }

            if (c == KeyEvent.VK_RIGHT)
            {

                velX = +1;

                velX2 = +1;

            }

            if (c == KeyEvent.VK_DOWN)
            {

                velY = -1;

                velY2 = -1;

                velY3 = +1;

                velY4 = +1;

            }

        }

        public void keyTyped(KeyEvent e){}

        //*****************************Key Released Method********************************

        public void keyReleased(KeyEvent e)
        {

            velX = 0;

            velX2 = 0;

            velY = 0;

            velY2 = 0;

            velY3 = 0;

            velY4 = 0;

        }
}