gpt4 book ai didi

Java - 代码编译但无法识别操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:34 28 4
gpt4 key购买 nike

由于焦点问题,我在使用 Java 中的键监听器时遇到了一些麻烦,所以我阅读了相关内容,许多网站建议改用键绑定(bind)。我的代码在下面,我正在尝试将空格键映射到一个 Action TestAction。面板对象只是 JPanel 的扩展。

public class Template_Main {

static Boolean quit = false;

public static void main(String[] args) {
Window window = new Window();
while(!quit) {
window.update();
}
System.exit(0);
}

}

.

import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

public class Window {

public Frame frame;
public Panel panel;

public Window() {
frame = new Frame();
panel = new Panel();

Action pressedAction = new TestAction();
Action releasedAction = new TestAction();

pressedAction.setEnabled(true);
releasedAction.setEnabled(true);

panel.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"pressed");
panel.getInputMap().put(KeyStroke.getKeyStroke("released SPACE"),"released");

panel.getActionMap().put("pressed",pressedAction);
panel.getActionMap().put("released",releasedAction);

frame.add(panel);
frame.pack();
}

public void update() {
Logic.update();
updateImages();
sleep();
}

public void updateImages() {
panel.removeAll();
for (int i = 0; i < Imgs.array.size(); ++i) {
Img img = Imgs.get(i);
addImage(img.name, img.x, img.y, img.width, img.height);
}
panel.repaint();
}

public void addImage(String name, int x, int y, int width, int height) {
panel.add(newImageLabel(name, x, y, width, height));
}

public static JLabel newImageLabel(String imageName, int x, int y, int width, int height) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
java.net.URL iconUrl = ClassLoader.getSystemResource("resources/"+imageName);
Image image = toolkit.createImage(iconUrl);
JLabel label = new JLabel(new ImageIcon(image));
label.setBounds(x, y, width, height);
return label;
}

static void sleep() {
try {
Thread.sleep(Math.round(10));
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

.

import javax.swing.JFrame;

public class Frame extends JFrame {

public Frame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle(Config.TITLE);
setResizable(Config.RESIZABLE);
setVisible(true);
}

}

.

import java.awt.Dimension;
import javax.swing.JPanel;

public class Panel extends JPanel {

public Panel() {
setPreferredSize(new Dimension(Config.WIDTH,Config.HEIGHT));
setLayout(null);
setBackground(Config.BG_COLOR);
setFocusable(true);
}

}

.

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;

public class TestAction extends AbstractAction {

public TestAction() {

}

@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("ACTION PERFORMED");
}

}

问题是虽然它编译没有错误,但当我按下空格键时没有任何反应。

最佳答案

我认为您的问题正如 William Morrison 在 JPanel 未集中注意力时所说的那样。

尝试调用电话 requestFocus()添加面板时。如果您可以发布一个包含的小代码示例,我们可以为您提供更多帮助。

关于Java - 代码编译但无法识别操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134026/

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