简体   繁体   English

使用KeyAdapter的键盘输入

[英]Keyboard input with KeyAdapter

I'm trying to make a simple game in java where you can move the player (Defender) in the 4 directions. 我正在尝试用Java开发一个简单的游戏,您可以在4个方向上移动播放器(Defender)。 I tried to make the key detecting with a key adapter, but it doesn't work. 我试图使用钥匙适配器进行钥匙检测,但是它不起作用。 What could be the problem (I tried to do a System.out.println at the key press to make sure that the problem isn't at the Defender)? 可能是什么问题(我试图在按键时执行System.out.println以确保问题不在Defender上)? Code: 码:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

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

public class DefenderComponent extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 160;
    private static final int HEIGHT = 120;
    private static final int SCALE = 4;

    Defender player = new Defender();

    public DefenderComponent() {
        Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
        setMinimumSize(size);
        setMaximumSize(size);
        setPreferredSize(size);

        addKeyListener(new TKeyListener());

        Timer timer = new Timer(5, this);
                timer.start();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test2");
        frame.add(new DefenderComponent());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        frame.setFocusable(true);

        new DefenderComponent();
    }


    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        Image i = player.getImage();
        g2d.drawImage(i, player.getX(), player.getY(), i.getWidth(this) * SCALE, i.getHeight(this) * SCALE, this);
    }

    public void actionPerformed(ActionEvent e) {
        player.move();
        repaint();  
    }
}
frame.addActionListener(this);

is what you missed. 是你错过的。

that line says. 那条线说。 this class is an ActionListener. 此类是一个ActionListener。 please call this class when you receive an action. 请在收到操作后致电该班。

if you want to add the ActionListener to the JPanel 如果要将ActionListener添加到JPanel

public DefenderComponent() {
    addActionListener(this);
    ....
}

KeyEvents are only generated for the component that has focus. 仅为具有焦点的组件生成KeyEvent。 A JPanel is not focusable by default. 默认情况下,JPanel不能聚焦。

Don't use a KeyListener. 不要使用KeyListener。 Instead you should be using Key Bindings which are more flexible. 相反,您应该使用更灵活的键绑定。

See Motion Using the Keyboard for more information and examples. 有关更多信息和示例,请参见使用键盘运动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM