我在pacman游戏开始时有两个课程,我对键监听器或键绑定的工作原理感到困惑。我有向上,向下,向右和向左移动的方法。我花了好几个小时试图找出来,它不会调用常规方法,组件让我感到困惑。有人可以请帮助告诉我如何获得箭头键调用方法的最终结果?我有下面的代码,但我拿出了所有的关键测试。
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI {
JPanel myPanel = new JPanel(new GridLayout(50, 50, 1, 1));
int[][] myLabels = new int[50][50];
//Set Game Black
public int[][] setBlack(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
JLabel black = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
myLabels[i][j] = 1;
}
}
return myLabels;
}
//get panel any time changes are made to array
public JPanel getPanel(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
JLabel black = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
JLabel pacman = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
if(myLabels[i][j] == 1){
myPanel.add(black);
}
if(myLabels[i][j] == 2){
myPanel.add(pacman);
}
}
}
return myPanel;
}
//moves pacman up
public int[][] moveUp(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i-1][j] = 2;
}
}
}
return myLabels;
}
//=========
//moves pacman up
public void setLabel(int[][] arrayLabels){
myLabels = arrayLabels;
}
public int[][] moveUpKey(){
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i-1][j] = 2;
}
}
}
return myLabels;
}
//////===========================
//moves pacman down
public int[][] moveDown(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i+1][j] = 2;
}
}
}
return myLabels;
}
//moves pacman right
public int[][] moveRight(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i][j+1] = 2;
}
}
}
return myLabels;
}
//moves pacman left
public int[][] moveLeft(int[][] arrayLabels){
myLabels = arrayLabels;
for (int i = 0; i < myLabels.length; i++) {
for (int j = 0; j < myLabels[0].length; j++) {
if(myLabels[i][j] == 2){
myLabels[i][j] = 1;
myLabels[i][j-1] = 2;
}
}
}
return myLabels;
}
}
游戏类:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import java.util.*;
public class Game {
public static void main(String[] args) throws Exception {
final GUI gui = new GUI();
JFrame f = new JFrame("Frame Test");
JPanel panel = new JPanel(new GridLayout(50, 50, 1, 1));
int[][] labels = new int[50][50];
JLabel black2 = new JLabel(new ImageIcon("black-square.jpg"), JLabel.CENTER);
JLabel pacman2 = new JLabel(new ImageIcon("pacman.png"), JLabel.CENTER);
panel.setLayout(new GridLayout(50, 50, 1, 1));
labels = gui.setBlack(labels);
panel = gui.getPanel(labels);
labels[10][10] = 2;
//labels = gui.moveUp(labels);
panel.removeAll();
panel = gui.getPanel(labels);
panel.repaint();
//labels[i][j]
//.revalidate();
//do the thing
f.setContentPane(panel);
f.setSize(1000, 1000);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
答案 0 :(得分:2)
基本上,myPanel
充当游戏的核心容器。
这可能是将键绑定注册到...的最佳候选者。
为您的GUI
课程创建构造函数。在此构造函数中,您需要注册密钥绑定,例如......
public GUI() {
InputMap im = myPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = myPanel.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
am.put("up", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
moveUp(myLabels);
}
});
// Register other key strokes...
}
ps-我不知道为什么你的移动方法需要JLabel[][]
参数,因为myLables
无论如何都可以访问这些方法,但那只是我...
请确保您已阅读How to Use Key Bindings了解详情