我目前正在研究一个项目,当我尝试添加自己的类中的自己的键侦听器时,它不起作用,当您按下键时没有任何反应,而且我已经有一段时间了。我不能使用键绑定,所以请不要让我更改为那些,因为它们不支持我正在做的事情,因为它们不支持一次多次按键(相信我,我试过了)。它专注于使用panel.setFocusable(true);
and panel.requestFocusInWindow();
,我什至用frame.setFocusable(true);
and对框架做了它,frame.requestFocusInWindow();
但仍然没有。这些都是我的文件:
编辑:我添加了一个SwingUtilities.invokeLater(new Runnable() {
,但仍然没有,我目前正在做 MadProgrammer 所说的关于 KeyBindings 的内容,但截至目前,我遇到了一些我正在尝试修复的奇怪错误。
Game.Java:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
import com.PK.character.MainCharacter;
public class Game extends JPanel{
private static final long serialVersionUID = -2398443377427441196L;
public static Image gamemainmenu = Toolkit.getDefaultToolkit().createImage("src/resources/homerscared.jpg");
public static boolean menu;
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(gamemainmenu, 10, 10, null);
menu = true;
if (menu = true){
g.drawImage(MainCharacter.MainCharacterImage, 100, 100, null);
}
}
}
ButtonListener.Java:
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import com.PK.PK;
import com.PK.character.MainCharacter;
public class ButtonListener implements KeyListener{
private static JPanel gamepanel = PK.panel;
@Override
public void keyPressed(KeyEvent arg0) {
/**N=0
* NE=1
* E=2
* SE=3
* S=4
* SW=5
* W=6
* NW=7
*/
if (arg0.getKeyChar() == KeyEvent.VK_DOWN){
MainCharacter.move(4, MainCharacter.CharacterS);
System.out.println("down pressed");
gamepanel.setForeground(Color.BLUE);
}
if (arg0.getKeyChar() == KeyEvent.VK_UP){
MainCharacter.move(0, MainCharacter.CharacterN);
System.out.println("up pressed");
}
if (arg0.getKeyChar() == KeyEvent.VK_LEFT){
MainCharacter.move(6, MainCharacter.CharacterW);
System.out.println("left pressed");
}
if (arg0.getKeyChar() == KeyEvent.VK_RIGHT){
MainCharacter.move(2, MainCharacter.CharacterE);
System.out.println("right pressed");
}
if (arg0.getKeyChar() == KeyEvent.VK_RIGHT && arg0.getKeyChar() == KeyEvent.VK_UP){
MainCharacter.move(1, MainCharacter.CharacterNE);
System.out.println("right and up pressed");
}
if (arg0.getKeyChar() == KeyEvent.VK_RIGHT && arg0.getKeyChar() == KeyEvent.VK_DOWN){
MainCharacter.move(3, MainCharacter.CharacterSE);
System.out.println("up and right pressed");
}
if (arg0.getKeyChar() == KeyEvent.VK_LEFT && arg0.getKeyChar() == KeyEvent.VK_UP){
MainCharacter.move(7, MainCharacter.CharacterNW);
System.out.println("up and left pressed");
}
if (arg0.getKeyChar() == KeyEvent.VK_LEFT && arg0.getKeyChar() == KeyEvent.VK_DOWN){
MainCharacter.move(5, MainCharacter.CharacterSW);
System.out.println("left and down pressed");
}
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}
PK.Java (Main Class):
package com.PK;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.PK.movement.ButtonListener;
public class PK {
public static short CharacterX, CharacterY;
public static final int width = 800;
public static final int height = 600;
public static Date date = new Date();
public static String dString = date.toString();
public static String dFormat = "[" + dString + "]: ";
public static JFrame frame = new JFrame();
public static JPanel panel = new Game();
public static KeyListener bt = new ButtonListener();
public static Image logobasic = Toolkit.getDefaultToolkit().createImage("src/resources/logo-basic.png");
public static void main(String[] args){
frame.setContentPane(panel);
System.out.println(dFormat + "Panel added to frame");
frame.setSize(width, height);
frame.setTitle("PK");
frame.setIconImage(logobasic);
frame.setVisible(true);
frame.setJMenuBar(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(dFormat + "Frame settings set");
System.out.println(dFormat + "Launching...");
}
public PokemonUniverse(){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
panel.setFocusable(true);
panel.requestFocusInWindow();
System.out.println(dFormat + "Panel focused");
panel.addKeyListener(bt);
System.out.println(dFormat + "KeyListener added to panel");
frame.setFocusable(true);
frame.requestFocusInWindow();
System.out.println(dFormat + "Frame focused");
frame.addKeyListener(bt);
System.out.println(dFormat + "KeyListener added to frame");
}
});
}
MainCharacter.Java
package com.PK;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
import com.PK.Game;
import com.PK.PK;
@SuppressWarnings("unused")
public class MainCharacter {
static Toolkit tk = Toolkit.getDefaultToolkit();
public static Image MainCharacterImage = PK.logobasic, CharacterS, CharacterN, CharacterW, CharacterE, CharacterSE, CharacterNE, CharacterSW, CharacterNW;
private static JPanel gamepanel = PK.panel;
private static short Y = PK.CharacterY;
private static short X = PK.CharacterX;
/**N=0
* NE=1
* E=2
* SE=3
* S=4
* SW=5
* W=6
* NW=7
*/
public static void move(int direction, Image FacingDirection) {
if (direction == 0){
Y++;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 1){
Y++;
X++;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 2){
X++;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 3){
Y--;
X++;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 4){
Y--;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 5){
Y--;
X--;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 6){
X--;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else if (direction == 7){
X--;
Y++;
MainCharacterImage = FacingDirection;
gamepanel.repaint();
}
else{
MainCharacterImage = PK.logobasic;
gamepanel.repaint();
}
}
}