gpt4 book ai didi

java - 需要帮助让我的按键监听器脱离主线程

转载 作者:行者123 更新时间:2023-11-30 05:22:48 24 4
gpt4 key购买 nike

我已经开始制作一款可以自由移动的游戏。你用你的键盘移动我让它工作,但我想做的就是把它从 main 中取出,这样我就可以从不同的类多次调用。我尝试将其放入 public static void 中并从 main 调用该方法,但它不起作用。下面提供了我的代码。我已经被困在这个问题上很长一段时间了。

我在框架中有一张图片以使其可见,该图片名为 1。

import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class StartGame {
public static void main(String[] argv) throws Exception {
JFrame MainFrame = new JFrame();
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setSize(1210, 700);
MainFrame.setLocation(new java.awt.Point(150, 30));
MainFrame.setLayout(null);
MainFrame.setFocusable(true);
MainFrame.setFocusTraversalKeysEnabled(true);
MainFrame.setIconImage(new ImageIcon("images\\sword.png").getImage());
JLabel thing = new JLabel();
thing.setIcon(new ImageIcon("image\\1.gif"));
thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

MainFrame.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
if (0 < thing.getY()) {
thing.setLocation(thing.getX(), thing.getY() - 10);
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
if (thing.getY() < 570) {
thing.setLocation(thing.getX(), thing.getY() + 10);
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
if (thing.getX() < 1110) {
thing.setLocation(thing.getX()+ 10, thing.getY());
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
if (0 < thing.getX()) {
thing.setLocation(thing.getX() - 10, thing.getY());
}
}

}

public void keyTyped(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {


}
});

MainFrame.add(thing);
MainFrame.setVisible(true);
}
}

最佳答案

不要使用KeyListener,使用key bindings API 。使用按键绑定(bind) API 将允许您在“窗口”级别而不是组件级别响应按键事件(如果您愿意)

例如...

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

public class StartGame {

public static void main(String[] argv) throws Exception {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(1210, 700);
mainFrame.setLocation(new java.awt.Point(150, 30));
mainFrame.setLayout(null);
mainFrame.setFocusable(true);
mainFrame.setFocusTraversalKeysEnabled(true);
mainFrame.setIconImage(new ImageIcon("images\\sword.png").getImage());
JLabel thing = new JLabel("X");
//thing.setIcon(new ImageIcon("image\\1.gif"));
thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

InputMap inputMap = thing.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = thing.getActionMap();

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "move.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "move.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "move.down");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "move.down");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "move.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "move.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "move.right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "move.right");

actionMap.put("move.up", new ThingAction(thing, new Point(0, -1)));
actionMap.put("move.down", new ThingAction(thing, new Point(0, 1)));
actionMap.put("move.left", new ThingAction(thing, new Point(-1, 0)));
actionMap.put("move.right", new ThingAction(thing, new Point(1, 0)));

mainFrame.add(thing);
mainFrame.setVisible(true);
}

public static class ThingAction extends AbstractAction {
private JLabel thing;
private Point delta;

public ThingAction(JLabel thing, Point delta) {
this.thing = thing;
this.delta = delta;
}

@Override
public void actionPerformed(ActionEvent arg0) {
thing.setLocation(thing.getX() + delta.x, thing.getY() + delta.y);
}
}
}

这是一个非常基本的示例,但它演示了两个关键点:

  1. 您可以为单个操作分配多个键
  2. 您可以将输入(即键盘)与实现分离,这意味着您可以使用相同的基本 Action 来处理鼠标输入或操纵杆输入

关于java - 需要帮助让我的按键监听器脱离主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com