我遵循了關於在java中創建迷宮遊戲的教程。完成後,我決定添加一些東西(主菜單,更多關卡等)。我目前卡在主菜單,因爲我無法讓鼠標輸入工作。當我在開始遊戲框中點擊左鍵時,我正嘗試切換到屏幕= 1。這是我當前的課程代碼。鼠標點擊java
package Maze;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener{
private Timer timer;
private Map m;
private Player p;
private int screen = 0;
private String Message = "";
private Font font = new Font("Serif", Font.BOLD, 48);
private Font start = new Font("Serif", Font.BOLD, 36);
private Rectangle start2 = new Rectangle(135,350,180,55);
public Board(){
m = new Map();
p = new Player();
addKeyListener(new Al());
addMouseListener(new A2());
setFocusable(true);
timer = new Timer(25,this);
timer.start();
}
public void actionPerformed(ActionEvent e){
if(m.getMap(p.getTileX(),p.getTileY()).equals("f")){
Message = "Winner";
screen = 10;
}
repaint();
}
public void paint(Graphics g){
super.paint(g);
if(screen==0){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 464, 485);
g.setColor(Color.RED);
g.setFont(font);
g.drawString("Maze Game", 100,50);
g.setFont(start);
g.setColor(Color.WHITE);
g.drawString("Created by: ", 135,90);
g.drawString("Chad Jordan", 125,130);
g.setColor(Color.PINK);
g.fillRect(start2.x, start2.y, start2.width, start2.height);
g.setColor(Color.WHITE);
g.drawString("Start Game", start2.x, start2.y+40);
}
if((screen==1)){
for(int y = 0; y<14;y++){
for(int x=0; x<14; x++){
if(m.getMap(x, y).equals("g")){
g.drawImage(m.getGrass(), x*32, y*32, null);
}
if(m.getMap(x, y).equals("w")){
g.drawImage(m.getWall(), x*32, y*32, null);
}
if(m.getMap(x,y).equals("f")){
g.drawImage(m.getFinish(), x*32, y*32, null);
}
}
}
g.drawImage(p.getPlayer(), p.getTileX()*32, p.getTileY()*32, null);
}
if(screen==10){
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(Message, 150,200);
}
}
public class A2 extends MouseAdapter{
public void MousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
System.out.println("2");
if((screen ==0) && start2.contains(mx,my)){
screen=1;
System.out.println("1");
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
public class Al extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keycode = e.getKeyCode();
if((keycode == KeyEvent.VK_W) || (keycode == KeyEvent.VK_UP)){
if(!m.getMap(p.getTileX(),p.getTileY() -1).equals("w")){
p.move(0, -1);
}
}
if((keycode == KeyEvent.VK_A) || (keycode == KeyEvent.VK_LEFT)){
if(!m.getMap(p.getTileX()-1,p.getTileY()).equals("w")){
p.move(-1, 0);
}
}
if((keycode == KeyEvent.VK_D) || (keycode == KeyEvent.VK_RIGHT)){
if(!m.getMap(p.getTileX()+1,p.getTileY()).equals("w")){
p.move(1, 0);
}
}
if((keycode == KeyEvent.VK_S) || (keycode == KeyEvent.VK_DOWN)){
if(!m.getMap(p.getTileX(),p.getTileY() +1).equals("w")){
p.move(0, 1);
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
}
我用「System.out.println(」1「);」看看它是否註冊了鼠標按鍵,但它不打印。
當你把'後的System.out.println(「1」);'之外的if語句,它仍然沒有按不打印?另外,爲什麼要打印?只是把一個斷點和調試? (即使沒有使用IDE,也可以這樣做,儘管可以幫助...) –