作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经尝试解决这个问题大约 5 个小时了,但我就是想不通为什么我的 KeyListener 根本没有反应。它甚至似乎还没有到达寻找 keyInput 的地步?这是调用它的类:
package summonit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
public class Player extends KleinerScreen implements KeyListener {
public Player() throws IOException{
addKeyListener(this);
}
public static int playerX=20;
public static int playerY;
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_K) {
playerX += 100;
}
System.out.println(playerX);
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_K) {
playerX += 100;
}
System.out.println(playerX);
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
还有主类:
package summonit;
import java.awt.*;
import java.io.IOException;
import javax.swing.JFrame;
public class Summonit extends JFrame{
public static void main(String[] args) throws IOException {
Summonit game = new Summonit();
Screen window = new Screen();
TileMap tilemap = new TileMap();
Player player = new Player();
}
}
面板类
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package summonit;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
/**
*
* @author Boyen
*/
public class KleinerScreen extends JPanel {
String i = "457528_3569247037775_1427420686_o.jpg";
String s = "City.txt";
public static Dimension windowsize = new Dimension(1000, 1000);
private int mapWidth;
private int mapHeight;
public int map[][];
final int playerRows = 4;
final int playerCols = 4;
//images of tiles
private Image tileYellowPath;
private Image tileGround;
BufferedImage bigPlayerImg;
BufferedImage[] sprites;
//images
public KleinerScreen() throws IOException {
setPreferredSize(windowsize);
tileYellowPath = new ImageIcon(getClass().getResource("/CorrodedTechnoTiles.png")).getImage();
tileGround = new ImageIcon(getClass().getResource("/images.jpg")).getImage();
bigPlayerImg = ImageIO.read(new File("res/sprites_player_3.png"));
sprites = new BufferedImage[playerRows * playerCols];
for (int i = 0; i < playerRows; i++) {
for (int j = 0; j < playerCols; j++) {
sprites[(i * playerCols) + j] = bigPlayerImg.getSubimage(
j * 150,
i * 150,
150,
150);
}
}
}
public void readTiles() {
}
public void paint(Graphics g) {
for (int i = 0; i < TileMap.map.length; i++) {
for (int j = 0; j < TileMap.map[i].length; j++) {
switch (TileMap.map[i][j]) {
case 0:
g.drawImage(tileGround, windowsize.width / 10 * j, windowsize.height / 10 * i, windowsize.height / 10, windowsize.width / 10, null);
break;
case 1:
g.drawImage(tileYellowPath, windowsize.width / 10 * j, windowsize.height / 10 * i, windowsize.height / 10, windowsize.width / 10, null);
break;
}
}
}
g.drawImage(sprites[5], Player.playerX, 0 ,100,100,null);
}
}
最佳答案
使用Key Bindings .这里有一个简短的例子来展示如何使用它们:
public class Test
{
JFrame frame = new JFrame();
public Test()
{
ActionMap actionMap = frame.getRootPane().getActionMap();
InputMap inputMap = frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
for (Keys direction : Keys.values())
{
actionMap.put(direction.getText(), new KeyBinding(direction.getText()));
inputMap.put(direction.getKeyStroke(), direction.getText());
}
frame.getRootPane().setActionMap(actionMap);
frame.getRootPane().setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
frame.setVisible(true);
}
private class KeyBinding extends AbstractAction
{
private static final long serialVersionUID = 1L;
public KeyBinding(String text)
{
super(text);
putValue(ACTION_COMMAND_KEY, text);
}
@Override
public void actionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
System.out.println("Key Binding: " + action);
}
}
public static void main(String... args)
{
new Test();
}
}
enum Keys
{
ESCAPE("Escape", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)),
CTRLC("Control-C", KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK)),
CTRLP("Control-P", KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_DOWN_MASK)),
UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)),
DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)),
LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)),
RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
private String text;
private KeyStroke keyStroke;
Keys(String text, KeyStroke keyStroke)
{
this.text = text;
this.keyStroke = keyStroke;
}
public String getText()
{
return text;
}
public KeyStroke getKeyStroke()
{
return keyStroke;
}
@Override
public String toString()
{
return text;
}
}
关于java - KeyListener 根本没有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16388931/
我正在尝试将高斯模糊添加到场景中。我正在使用此GLSL着色器:。当您提供图像作为tDiffuse纹理时,它对图像起作用,但我正在尝试将其添加为后处理效果。我试图获取摄影机的renderTarget并将
我正在做一款天气应用程序,到目前为止还不错。现在,我想通过单击按钮来呈现一些额外的信息。我有一个5天的预报,然后想显示每一天的信息。我已经成功地过滤了数据,但无法获得要渲染的数据地图。以下是我的一些代
我正在做一款天气应用程序,到目前为止还不错。现在,我想通过单击按钮来呈现一些额外的信息。我有一个5天的预报,然后想显示每一天的信息。我已经成功地过滤了数据,但无法获得要渲染的数据地图。以下是我的一些代
我在全球安装了Create Reaction应用程序。然后我就跑了。NPX创建-反应-应用JSX。它安装了大约1460个包的所有包,但没有设置Public和src文件夹。在我的JSX文件夹中,只有1:
我是一名优秀的程序员,十分优秀!