addKeyListener() 对 JPanel 不起作用

发布于 2024-12-21 00:04:24 字数 3331 浏览 0 评论 0原文

我正在尝试制作一个游戏引擎。我已经创建了 Game 类,但错误位于 KeyBoard 类中。这里我留下一些代码。

Class:: Game

package transfer2pc.co.cc.game.tileengine;

import java.awt.Graphics;
import java.util.HashMap;

import javax.swing.JPanel;

import transfer2pc.co.cc.game.tileengine.input.KeyBoard;

public abstract class Game extends JPanel implements Runnable {

   /**
    * 
    */
    private static final long serialVersionUID = 640206679500196209L;

    HashMap<String, ?> maps = null;

    KeyBoard keyBoard = null;

    public Game(){
        super();
        keyBoard = new KeyBoard(this);
        setKeyBoard(keyBoard);
        Thread th = new Thread(this);
        th.start();
    }

    public void run(){
        while(true){
            repaint();
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g){

    }

    public void addMap(){

    }

    public void setMap(){

    }

    public abstract void keyPressed(int code);

    public abstract void keyReleased(int code);

    public abstract void keyTyped(int code);

    public void setKeyBoard(KeyBoard key){
        addKeyListener(key);
    }

}

Class:: KeyBoard

package transfer2pc.co.cc.game.tileengine.input;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import transfer2pc.co.cc.game.tileengine.Game;

public class KeyBoard extends KeyAdapter implements KeyListener {

    Game game = null;

    public KeyBoard(Game gm){
        this.game = gm;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("KeyPressed");
        game.keyPressed(e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        game.keyReleased(e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
        game.keyTyped(e.getKeyCode());
    }

    public static char getChar(int key){
        return (char)key;
    }

}

Class:: KeyTest

package transfer2pc.co.cc.game.tileengine.test;

import java.awt.Graphics;

import javax.swing.JFrame;

import transfer2pc.co.cc.game.tileengine.Game;
import transfer2pc.co.cc.game.tileengine.input.KeyBoard;

public class KeyTest extends Game {

    /**
     * 
     */
    private static final long serialVersionUID = 8557676950779023327L;

    char pressed;

    public KeyTest(){
        super();
        addKeyListener(new KeyBoard(this));
    }

    @Override
    public void keyPressed(int code) {
        pressed = KeyBoard.getChar(code);
    }


    @Override
    public void keyReleased(int code) {

    }


    @Override
    public void keyTyped(int code) {

    }

    @Override
    public void paint(Graphics g){
        g.drawString("You pressed: "+pressed, 20, 20);
    }

    public static void main(String[] args){
        JFrame frame = new JFrame("KeyTest");
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new KeyTest());
        frame.setVisible(true);
    }

}

但错误是没有抛出异常并且没有读取输入。有人能告诉我这样做的正确方法吗?

I am trying to make a game engine. I have made the Game class but the error resides in the KeyBoard class. Here I leave some code.

Class:: Game

package transfer2pc.co.cc.game.tileengine;

import java.awt.Graphics;
import java.util.HashMap;

import javax.swing.JPanel;

import transfer2pc.co.cc.game.tileengine.input.KeyBoard;

public abstract class Game extends JPanel implements Runnable {

   /**
    * 
    */
    private static final long serialVersionUID = 640206679500196209L;

    HashMap<String, ?> maps = null;

    KeyBoard keyBoard = null;

    public Game(){
        super();
        keyBoard = new KeyBoard(this);
        setKeyBoard(keyBoard);
        Thread th = new Thread(this);
        th.start();
    }

    public void run(){
        while(true){
            repaint();
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g){

    }

    public void addMap(){

    }

    public void setMap(){

    }

    public abstract void keyPressed(int code);

    public abstract void keyReleased(int code);

    public abstract void keyTyped(int code);

    public void setKeyBoard(KeyBoard key){
        addKeyListener(key);
    }

}

Class:: KeyBoard

package transfer2pc.co.cc.game.tileengine.input;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import transfer2pc.co.cc.game.tileengine.Game;

public class KeyBoard extends KeyAdapter implements KeyListener {

    Game game = null;

    public KeyBoard(Game gm){
        this.game = gm;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("KeyPressed");
        game.keyPressed(e.getKeyCode());
    }

    @Override
    public void keyReleased(KeyEvent e) {
        game.keyReleased(e.getKeyCode());
    }

    @Override
    public void keyTyped(KeyEvent e) {
        game.keyTyped(e.getKeyCode());
    }

    public static char getChar(int key){
        return (char)key;
    }

}

Class:: KeyTest

package transfer2pc.co.cc.game.tileengine.test;

import java.awt.Graphics;

import javax.swing.JFrame;

import transfer2pc.co.cc.game.tileengine.Game;
import transfer2pc.co.cc.game.tileengine.input.KeyBoard;

public class KeyTest extends Game {

    /**
     * 
     */
    private static final long serialVersionUID = 8557676950779023327L;

    char pressed;

    public KeyTest(){
        super();
        addKeyListener(new KeyBoard(this));
    }

    @Override
    public void keyPressed(int code) {
        pressed = KeyBoard.getChar(code);
    }


    @Override
    public void keyReleased(int code) {

    }


    @Override
    public void keyTyped(int code) {

    }

    @Override
    public void paint(Graphics g){
        g.drawString("You pressed: "+pressed, 20, 20);
    }

    public static void main(String[] args){
        JFrame frame = new JFrame("KeyTest");
        frame.setSize(640, 480);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(new KeyTest());
        frame.setVisible(true);
    }

}

But the error was there was no exception thrown and the input isn't being read. Could anybody say me the correct way of doing this..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-12-28 00:04:24

简而言之,您的面板需要可聚焦。在您创建面板的任意位置添加:

panel.setFocusable(true);
panel.requestFocusInWindow();

这是一个SSCCE(我建议将来使用其中一个提问):

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleKeyTest {

    public static void main(String[] args) {

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                JPanel panel = new JPanel();

                frame.getContentPane().add(panel);

                panel.addKeyListener(new KeyListener() {

                    @Override
                    public void keyTyped(KeyEvent e) {}

                    @Override
                    public void keyReleased(KeyEvent e) {}

                    @Override
                    public void keyPressed(KeyEvent e) {
                        System.out.println("Pressed " + e.getKeyChar());
                    }
                });

                panel.setFocusable(true);
                panel.requestFocusInWindow();

                frame.setSize(new Dimension(300, 300));
                frame.setVisible(true);
            }

        };

        SwingUtilities.invokeLater(r);

    }
}

另外、https://www.google.com/search?q=jpanel+keylistener

Simply, your panel needs to be focusable. Add in wherever you create the panel:

panel.setFocusable(true);
panel.requestFocusInWindow();

Here's a SSCCE (I suggest asking questions with one of these in the future):

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleKeyTest {

    public static void main(String[] args) {

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                JPanel panel = new JPanel();

                frame.getContentPane().add(panel);

                panel.addKeyListener(new KeyListener() {

                    @Override
                    public void keyTyped(KeyEvent e) {}

                    @Override
                    public void keyReleased(KeyEvent e) {}

                    @Override
                    public void keyPressed(KeyEvent e) {
                        System.out.println("Pressed " + e.getKeyChar());
                    }
                });

                panel.setFocusable(true);
                panel.requestFocusInWindow();

                frame.setSize(new Dimension(300, 300));
                frame.setVisible(true);
            }

        };

        SwingUtilities.invokeLater(r);

    }
}

Also, https://www.google.com/search?q=jpanel+keylistener

扬花落满肩 2024-12-28 00:04:24

您可以将关键侦听器添加到 JFrame,这是我过去所做的事情。
但是,如果 JFrame 中有其他组件,这可能不是一个好主意。

You can add the key listener to the JFrame, that's something I've done in the past.
It's probably not a good idea however if you have other components in the JFrame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文