KeyListener不起作用(requestFocus无法修复它) [英] KeyListener not working (requestFocus not fixing it)

查看:57
本文介绍了KeyListener不起作用(requestFocus无法修复它)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在没有帮助的情况下将我的第一个游戏制作为youtube视频.我看过许多有关制作Java游戏的youtube系列影片,但它们从未出现在我想做的事情上,因此在学习了足够多的知识后,我开始制作自己的游戏.我知道有足够的Java可以解决这个问题,到目前为止,一切都没有问题.

I am trying to make my first game without the help a youtube video. I have watched many youtube series about making java games and they never where what I wanted to do, so after learning enough I started making my own game. I know enough Java to get around and so far everything has worked with no issues.

我尝试寻求帮助,但我只能找到人们所说的尝试之处,如下:

I have tried searching for some help but all I could find where people saying the try something along the lines of:

setFocusable(true);
requestFocus();

但是,这对我没有用.我知道大多数人都不想只浏览我所有的代码,但我认为它是可管理的(还没有那么多代码).

This however has not worked for me. I know that most people would not like to just look through all my code but I think it will be manageable (Its not that much code yet).

我计划将其制作成类似于蛇的游戏,在这种游戏中,您变大而不是变长,并且您是豚鼠(目前以橙色矩形表示)而不是蛇.

I plan on making it a sort of snake like game where you get bigger instead of longer and you are a guinea pig (represented by the orange rectangle for now) instead of a snake.

Game.java:

Game.java:

package com.kaperly.game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame {
    private static final long serialVersionUID = 1L;

    public static JFrame frame;
    public static JPanel panel;

    public static int width = 1000;
    public static int height = 600;

    public Game() {
        frame = new JFrame();
        panel = new Panel();

        frame.setPreferredSize(new Dimension(width, height));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Guinea Pig Eat");
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
        panel.setFocusable(true);
        panel.requestFocus(true);

        frame.add(panel, BorderLayout.CENTER);
        frame.pack();
    }

    public static void init() {

    }

    public static void loop() throws InterruptedException { while(true) {
        Character.move();

        Thread.sleep(10);
    }}

    public static void main(String[] args) throws InterruptedException, IOException {
        Game game = new Game();

        init();
        loop();
    }
}

Panel.java:

Panel.java:

package com.kaperly.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Panel extends JPanel implements KeyListener {
    private static final long serialVersionUID = 1L;

    public static Image startButton;

    public static boolean goingRight = false;
    public static boolean goingLeft = false;
    public static boolean goingUp = false;
    public static boolean goingDown = false;

    private static int changeDirection = 0;

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W) {
            goingUp = true;
            changeDirection = 1;
            System.out.println("Moved Up!");

        }else if(e.getKeyCode() == KeyEvent.VK_A) {
            goingLeft = true;
            changeDirection = 2;

        }else if(e.getKeyCode() == KeyEvent.VK_S) {
            goingDown = true;
            changeDirection = 3;

        }else if(e.getKeyCode() == KeyEvent.VK_D) {
            goingRight = true;
            changeDirection = 4;

        }else if(changeDirection == (1 | 2 | 3 | 4)) {
            changeDirection = 0;
            if(changeDirection == 1) {
                goingLeft = false;
                goingDown = false;
                goingRight = false;

            }else if(changeDirection == 2) {
                goingUp = false;
                goingDown = false;
                goingRight = false;

            }else if(changeDirection == 3) {
                goingUp = false;
                goingLeft = false;
                goingRight = false;

            }else if(changeDirection == 4) {
                goingUp = false;
                goingLeft = false;
                goingDown = false;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        //Set background color
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, Game.width, Game.height);


        //Make Walls
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, Game.width, 25);
        g.fillRect(0, 550, Game.width, 25);
        g.fillRect(0, 0, 25, Game.height);
        g.fillRect(970, 0, 25, Game.height);

        //Show Character
        g.setColor(Color.ORANGE);
        g.fillRect(Character.x, Character.y, Character.width, Character.height);

        repaint();
    }
}

Character.java:

Character.java:

package com.kaperly.game;

public class Character {

    public static int x = 475;
    public static int y = 250;

    public static int width = 50;
    public static int height = 75;

    public static void move() {
        if(Panel.goingUp == true) {
            y = y ++;
        }
        if(Panel.goingLeft == true) {
            x = x --;
        }
        if(Panel.goingDown == true) {
            y = y --;
        }
        if(Panel.goingRight == true) {
            x = x ++;
        }
    }
}

应当避免

推荐答案

KeyListener,因为它与焦点有关.您永远都不知道会导致键盘焦点从您的组件移开.

KeyListener should be avoid BECAUSE of it's inherent focus related issues. You never know what might take keyboard focus away from your component.

相反,请利用键绑定 API,这样您就可以控制触发关键事件所需的焦点级别.

Instead, take advantage of the Key Bindings API, which allows you to control the focus level required to trigger your key events.

它还将允许您在其他地方重用与键绑定关联的基础Action ...

It will also allow you to reuse underlying Action associated with the key binding in other places...

明智的做法是将类Panel重命名为更有意义的名称. AWT已经有一个Panel类,将两者混淆很容易

You'd be wise to rename you class Panel to something more meaningful. AWT already has a Panel class and it would be very easy to confuse the two

这篇关于KeyListener不起作用(requestFocus无法修复它)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆