import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
class MapFrame extends JFrame implements ActionListener,KeyListener{
JLabel play[] = new JLabel[100];
JLabel image;
static int xp=100;
static int yp=295;
//窗格
Container con;
JButton anew;
JButton exit;
JToolBar jmain;
JLabel text;
MapFrame(){new MapFrame("sd");}
MapFrame(String title){
//获行客格引用
con = this.getContentPane();
con.setLayout(null);
//
jmain = new JToolBar();
text = new JLabel("版权作者、");
//当鼠标放上显示信息
anew = new JButton(" 新 游 戏 ");
anew.setToolTipText("重新开始新的一局");
exit = new JButton(" 退 出 ");
exit.setToolTipText("退出程序");
text.setToolTipText("信息提示");
jmain.setLayout(new GridLayout(0,4));
jmain.add(anew);
//jmain.add(repent);
jmain.add(exit);
jmain.add(text);
jmain.setBounds(0,0,1000,60);
con.add(jmain);
anew.addActionListener(this);
exit.addActionListener(this);
con.addKeyListener(this);
con.setFocusable(true);
//requestFocus();
drawMouse(xp,yp);
drawBg();
//drawMouse(200,295);
drawCat(800,295);
//con.add(play[51]);
con.add(image = new JLabel(new ImageIcon("tomandjerry.jpg")));
image.setBounds(0,0,1000,680);
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350);
//设置
//con.remove(play[19]);
this.setIconImage(new ImageIcon("tom_jerry.jpg").getImage());
this.setResizable(false);
this.setTitle(title);
this.setSize(1000,680);
setVisible(true);
this.toFront();
}
public void actionPerformed(ActionEvent ae) {}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP){
//this.removeMouse();
if(this.isStopped()!=true)
{this.moveMouse(xp,yp-=70);}
}
//this.drawMouse(xp,yp-70);}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
//this.removeMouse();
if(this.isStopped()!=true)
{this.moveMouse(xp,yp+=70);}
}
//this.drawMouse(xp,yp+70);}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
//this.removeMouse();
if(this.isStopped()!=true)
{this.moveMouse(xp-=100,yp);}
}
//this.drawMouse(xp-=100,yp);}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
//this.removeMouse();}
if(this.isStopped()!=true)
{this.moveMouse(xp+=100,yp);} }
// repaint();
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
//重新开始按钮
public void drawBg(){
int i,k;
//图标
Icon in;
in = new ImageIcon("外墙板.gif");
for (i=0,k=0;i<10;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,85,100,70);
play[i].setName("09");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=10,k=0;i<20;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,570,100,70);
play[i].setName("029");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=20,k=150;i<26;i++,k+=70){
play[i] = new JLabel(in);
play[i].setBounds(0,k,100,70);
play[i].setName("0229");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=27,k=150;i<33;i++,k+=70){
play[i] = new JLabel(in);
play[i].setBounds(900,k,100,70);
play[i].setName("0229");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=34,k=100;i<38;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,225,100,70);
play[i].setName("0229s");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=39,k=600;i<41;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,225,100,70);
play[i].setName("0229s");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=42,k=100;i<45;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,365,100,70);
play[i].setName("0229s");
con.add(play[i]);
}
in=new ImageIcon("外墙板.gif");
for (i=46,k=500;i<48;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,365,100,70);
play[i].setName("02s29s");
con.add(play[i]);
}
in=new ImageIcon("D.jpg");
//for (i=0,k=100;i<1;i++,k+=100){
play[49] = new JLabel(in);
play[49].setBounds(400,365,100,70);
play[49].setName("022s9s");
con.add(play[49]);
//}
in=new ImageIcon("N.jpg");
//for (i=0,k=100;i<1;i++,k+=100){
play[50] = new JLabel(in);
play[50].setBounds(100,500,100,70);
play[50].setName("0ss22s9s");
con.add(play[50]);
}
public void drawMouse(int x,int y){
Icon in;
in=new ImageIcon("JJ.jpg");
play[51] = new JLabel(in);
play[51].setBounds(x,y,100,70);
play[51].setName("100");
con.add(play[51]);
}
public void removeMouse(){
con.remove(play[51]);
//this.repaint();
}
public void drawCat(int x,int y){
Icon in;
in=new ImageIcon("TOM.jpg");
play[52] = new JLabel(in);
play[52].setBounds(x,y,100,70);
play[52].setName("101");
con.add(play[52]);
}
public void moveMouse(int x, int y){
play[51].setVisible(false);
play[51].setBounds(x,y,100,70);
play[51].setVisible(true);
}
public boolean isStopped(){
int lang=0;
for(int t=0;t<=48;t++){
if(xp==play[t].getX()&&yp==play[t].getY()){
lang=1; break;}
}
if(lang==1)
{return true;}
else{
return false;}}
public boolean isGetCheese(){
int q=0;
if(xp==play[50].getX()&&yp==play[50].getY())
{return true;}
else
{return false;}
}
}
为什么 我一按方向键 就会出现Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
求解决方法 THX
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
class MapFrame extends JFrame implements ActionListener,KeyListener{
JLabel play[] = new JLabel[100];
JLabel image;
static int xp=100;
static int yp=295;
//窗格
Container con;
JButton anew;
JButton exit;
JToolBar jmain;
JLabel text;
MapFrame(){new MapFrame("sd");}
MapFrame(String title){
//获行客格引用
con = this.getContentPane();
con.setLayout(null);
//
jmain = new JToolBar();
text = new JLabel("版权作者、");
//当鼠标放上显示信息
anew = new JButton(" 新 游 戏 ");
anew.setToolTipText("重新开始新的一局");
exit = new JButton(" 退 出 ");
exit.setToolTipText("退出程序");
text.setToolTipText("信息提示");
jmain.setLayout(new GridLayout(0,4));
jmain.add(anew);
//jmain.add(repent);
jmain.add(exit);
jmain.add(text);
jmain.setBounds(0,0,1000,60);
con.add(jmain);
anew.addActionListener(this);
exit.addActionListener(this);
con.addKeyListener(this);
con.setFocusable(true);
//requestFocus();
drawMouse(xp,yp);
drawBg();
//drawMouse(200,295);
drawCat(800,295);
//con.add(play[51]);
con.add(image = new JLabel(new ImageIcon("tomandjerry.jpg")));
image.setBounds(0,0,1000,680);
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350);
//设置
//con.remove(play[19]);
this.setIconImage(new ImageIcon("tom_jerry.jpg").getImage());
this.setResizable(false);
this.setTitle(title);
this.setSize(1000,680);
setVisible(true);
this.toFront();
}
public void actionPerformed(ActionEvent ae) {}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP){
//this.removeMouse();
if(this.isStopped()!=true)
{this.moveMouse(xp,yp-=70);}
}
//this.drawMouse(xp,yp-70);}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
//this.removeMouse();
if(this.isStopped()!=true)
{this.moveMouse(xp,yp+=70);}
}
//this.drawMouse(xp,yp+70);}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
//this.removeMouse();
if(this.isStopped()!=true)
{this.moveMouse(xp-=100,yp);}
}
//this.drawMouse(xp-=100,yp);}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
//this.removeMouse();}
if(this.isStopped()!=true)
{this.moveMouse(xp+=100,yp);} }
// repaint();
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
//重新开始按钮
public void drawBg(){
int i,k;
//图标
Icon in;
in = new ImageIcon("外墙板.gif");
for (i=0,k=0;i<10;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,85,100,70);
play[i].setName("09");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=10,k=0;i<20;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,570,100,70);
play[i].setName("029");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=20,k=150;i<26;i++,k+=70){
play[i] = new JLabel(in);
play[i].setBounds(0,k,100,70);
play[i].setName("0229");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=27,k=150;i<33;i++,k+=70){
play[i] = new JLabel(in);
play[i].setBounds(900,k,100,70);
play[i].setName("0229");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=34,k=100;i<38;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,225,100,70);
play[i].setName("0229s");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=39,k=600;i<41;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,225,100,70);
play[i].setName("0229s");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i=42,k=100;i<45;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,365,100,70);
play[i].setName("0229s");
con.add(play[i]);
}
in=new ImageIcon("外墙板.gif");
for (i=46,k=500;i<48;i++,k+=100){
play[i] = new JLabel(in);
play[i].setBounds(k,365,100,70);
play[i].setName("02s29s");
con.add(play[i]);
}
in=new ImageIcon("D.jpg");
//for (i=0,k=100;i<1;i++,k+=100){
play[49] = new JLabel(in);
play[49].setBounds(400,365,100,70);
play[49].setName("022s9s");
con.add(play[49]);
//}
in=new ImageIcon("N.jpg");
//for (i=0,k=100;i<1;i++,k+=100){
play[50] = new JLabel(in);
play[50].setBounds(100,500,100,70);
play[50].setName("0ss22s9s");
con.add(play[50]);
}
public void drawMouse(int x,int y){
Icon in;
in=new ImageIcon("JJ.jpg");
play[51] = new JLabel(in);
play[51].setBounds(x,y,100,70);
play[51].setName("100");
con.add(play[51]);
}
public void removeMouse(){
con.remove(play[51]);
//this.repaint();
}
public void drawCat(int x,int y){
Icon in;
in=new ImageIcon("TOM.jpg");
play[52] = new JLabel(in);
play[52].setBounds(x,y,100,70);
play[52].setName("101");
con.add(play[52]);
}
public void moveMouse(int x, int y){
play[51].setVisible(false);
play[51].setBounds(x,y,100,70);
play[51].setVisible(true);
}
public boolean isStopped(){
int lang=0;
for(int t=0;t<=48;t++){
if(xp==play[t].getX()&&yp==play[t].getY()){
lang=1; break;}
}
if(lang==1)
{return true;}
else{
return false;}}
public boolean isGetCheese(){
int q=0;
if(xp==play[50].getX()&&yp==play[50].getY())
{return true;}
else
{return false;}
}
}
为什么 我一按方向键 就会出现Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
求解决方法 THX
play[i] = new JLabel(in);
play[i].setBounds(k, 85, 100, 70);
play[i].setName("09");
con.add(play[i]);
} in = new ImageIcon("外墙板.gif");
for (i = 10, k = 0; i < 20; i++, k += 100) {
play[i] = new JLabel(in);
play[i].setBounds(k, 570, 100, 70);
play[i].setName("029");
con.add(play[i]); }
in = new ImageIcon("外墙板.gif");
for (i = 20, k = 150; i < 26; i++, k += 70) {
play[i] = new JLabel(in);
play[i].setBounds(0, k, 100, 70);
play[i].setName("0229");
con.add(play[i]); }
in = new ImageIcon("外墙板.gif");
for (i = 26, k = 150; i < 33; i++, k += 70) {
play[i] = new JLabel(in);
play[i].setBounds(900, k, 100, 70);
play[i].setName("0229");
con.add(play[i]); }
in = new ImageIcon("外墙板.gif");
for (i = 33, k = 100; i < 38; i++, k += 100) {
play[i] = new JLabel(in);
play[i].setBounds(k, 225, 100, 70);
play[i].setName("0229s");
con.add(play[i]); }
in = new ImageIcon("外墙板.gif");
for (i = 38, k = 600; i < 41; i++, k += 100) {
play[i] = new JLabel(in);
play[i].setBounds(k, 225, 100, 70);
play[i].setName("0229s");
con.add(play[i]);
}
in = new ImageIcon("外墙板.gif");
for (i = 41, k = 100; i < 45; i++, k += 100) {
play[i] = new JLabel(in);
play[i].setBounds(k, 365, 100, 70);
play[i].setName("0229s");
con.add(play[i]); }
in = new ImageIcon("外墙板.gif");
for (i = 45, k = 500; i <= 48; i++, k += 100) {
play[i] = new JLabel(in);
play[i].setBounds(k, 365, 100, 70);
play[i].setName("02s29s");
con.add(play[i]);
}
in = new ImageIcon("D.jpg");
// for (i=0,k=100;i <1;i++,k+=100){
play[49] = new JLabel(in);
play[49].setBounds(400, 365, 100, 70);
play[49].setName("022s9s");
con.add(play[49]);
出在这一段??你里面的play的赋值不连续,导致有的play[t]为空。