作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经开始制作一款可以自由移动的游戏。你用你的键盘移动我让它工作,但我想做的就是把它从 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);
}
}
}
这是一个非常基本的示例,但它演示了两个关键点:
Action
来处理鼠标输入或操纵杆输入关于java - 需要帮助让我的按键监听器脱离主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257956/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!