大家帮找找这个空直引用的错误!!谢谢!

hpym365 2006-03-22 02:18:30
错误是在第84行! 说
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MoveBall
{
public static void main(String [] args)
{
BallFrame b=new BallFrame();
b.setVisible(true);
}
}

class BallFrame extends JFrame
{
JPanel jp=new JPanel();
public BallFrame()
{
this.setBounds(100,150,400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel con=(JPanel)this.getContentPane();

jp.setBackground(Color.black);
con.add(jp);
new Ball(jp).start();
}
}



class Ball extends Thread
{
int r=0;
int moveX=2;
int moveY=2;
int ballX=50;
int ballY=50;
int WIDTH=10;
JPanel jp=new JPanel();
public Ball(JPanel jp)
{
this.jp=jp;
System.out.println("jpjp");
jp.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT:r=0;System.out.println("VK_LEFT");break;
case KeyEvent.VK_RIGHT:r=1;System.out.println("VK_RIGHT");break;
case KeyEvent.VK_UP:r=2;System.out.println("VK_UP");break;
case KeyEvent.VK_DOWN:r=3;System.out.println("VK_DOWN");break;
default:System.out.println("others");
}
}
});
jp.setFocusable(true);

}

public void move()
{
Graphics g=jp.getGraphics();
g.setXORMode(jp.getBackground());//错误在这里!!!

g.fillOval(ballX,ballY,WIDTH,WIDTH);

switch(r)
{
case 0:ballX+=-moveX;
case 1:ballY+=moveX;
case 2:ballY+=-moveX;
case 3:ballY+=moveX;
}
g.fillOval(ballX,ballY,WIDTH,WIDTH);
g.dispose();
}

public void run()
{
Graphics g=jp.getGraphics();
if(jp==null)
System.out.println("null");
g.setXORMode(jp.getBackground());
g.dispose();
while(true)
{
this.move();
try
{
Thread.sleep(10);
}
catch(Exception ex){}
}
}
}
...全文
234 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mark_Chen 2006-03-23
  • 打赏
  • 举报
回复
接分!!
sundeveloper 2006-03-23
  • 打赏
  • 举报
回复
up
cleansunshing 2006-03-23
  • 打赏
  • 举报
回复
jf
jinancf 2006-03-23
  • 打赏
  • 举报
回复
JF
  • 打赏
  • 举报
回复
jf
yuzl32 2006-03-22
  • 打赏
  • 举报
回复
gx
leekooqi 2006-03-22
  • 打赏
  • 举报
回复
jf
iambic 2006-03-22
  • 打赏
  • 举报
回复
接两分。
WiseDragon 2006-03-22
  • 打赏
  • 举报
回复
jf
treeroot 2006-03-22
  • 打赏
  • 举报
回复
jf]
interpb 2006-03-22
  • 打赏
  • 举报
回复
jf
ys_wangtao 2006-03-22
  • 打赏
  • 举报
回复
跟在后面接分了
图门的世界 2006-03-22
  • 打赏
  • 举报
回复
修改一下你的BallFrame定义

class BallFrame extends JFrame
{
JPanel jp=new JPanel();
public BallFrame()
{
this.setBounds(100,150,400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel con=(JPanel)this.getContentPane();

jp.setBackground(Color.black);
con.add(jp);
new Ball(jp).start();
}
}

改为:

class BallFrame extends JFrame {
JPanel jp = new JPanel();

public BallFrame() {
this.setBounds(100, 150, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel con = (JPanel) this.getContentPane();

jp.setBackground(Color.black);
con.add(jp);
this.setVisible(true);//增加这行代码,显示之后才能取的到graphics
new Ball(jp).start();
}
}


hpym365 2006-03-22
  • 打赏
  • 举报
回复
自己解决了。
hpym365 2006-03-22
  • 打赏
  • 举报
回复
主要是得不到图形对象 大家帮忙看看

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧