当前位置: 首页 > 知识库问答 >
问题:

如何在JFrame中添加键监听器?

阮桐
2023-03-14

我试图用我的键控制火箭动画,并尝试使用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;

    }

共有1个答案

冉昊
2023-03-14

PaintComponent(...)只能用于绘制。它不应该:

  1. 读取图像文件
  2. 设置面板的属性:setFocusable(..),setFocusKeysEnabled(...)
  3. 将侦听器添加到面板:addKeyListener(...)

以上所有代码都应该在构造函数中一次。

 类似资料:
  • 本文向大家介绍Java添加事件监听器,包括了Java添加事件监听器的使用技巧和注意事项,需要的朋友参考一下 示例 Preferences对象发出的事件有两种:PreferenceChangeEvent和NodeChangeEvent。 PreferenceChangeEvent 每当节点的键值对之一更改时,对象PreferenceChangeEvent就会发出A。可以使用来监听:Propertie

  • 问题内容: 我想将鼠标悬停在我创建的SpanElement的侦听器上: 我在Google中找到了如何使用Label-wrapper做到这一点,但我想在没有任何包装的情况下做到这一点。可能吗? 谢谢。 问题答案: 也可以不使用JSNI。 因此,与您的元素: 将事件侦听器直接添加到元素: …而且看起来确实很丑陋;)正如您所注意到的那样,对于此元素构想的所有dom事件,事件侦听器都是“公共的”。因此,为

  • 我试图添加一个imagepanel,将JPanel扩展到另一个JPanel。这对我来说不太好。图像面板的绘制功能在Jpanel中不会被调用,但在JFrame中可以正常工作。任何想法或帮助都将不胜感激。

  • 本文向大家介绍vue如何监听键盘事件?相关面试题,主要包含被问及vue如何监听键盘事件?时的应答技巧和注意事项,需要的朋友参考一下 方法 addEventListener

  • 本文向大家介绍Javascript添加监听与删除监听用法详解,包括了Javascript添加监听与删除监听用法详解的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Javascript添加监听与删除监听的用法。分享给大家供大家参考。具体分析如下: js中事件监听就是利用addEventListener来绑定一个事件,这个用法在jquery中非常常用并且简单,但在原生js中比较复杂,这里整理了

  • 我正在处理一些奇怪的遗留代码。他们有一个实现JPanel的自定义对象。这个< code>JPanel对象是主应用程序中的第二个弹出屏幕。我遇到的问题是检测辅助弹出屏幕何时关闭。 我尝试为该类实现一个,但是当我尝试添加它时,没有与此对象关联的。我假设这是因为他们正在使用一个自定义对象,它是一个嵌入式弹出屏幕。 我尝试使用以下命令检索: 这在上失败。我不知道为什么在这个页面上检测右上角的“x”关闭按钮