我已经尝试解决这个问题大约 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);
}
}