帮忙看下这个程序,怎么显示不出小鸟????
import java.awt.*;
import java.awt.event.*;
public class MoveBird extends Frame implements ActionListener{
private final int WIDTH=30,HEIGHT=20,INC=4;
private Button up=new Button("up");
private Button down=new Button("down");
private Button left=new Button("left");
private Button right=new Button("right");
private Button quit=new Button("Quit");
int x=50,y=50;
Image bird=Toolkit.getDefaultToolkit().getImage("bird.jpg");
public  MoveBird(){
super("moving bird");
setup();
up.addActionListener(this);
down.addActionListener(this);
left.addActionListener(this);
right.addActionListener(this);
quit.addActionListener(this);
setSize(400,400);
show();
pack();
}
public void setup(){
Panel buttons=new Panel();
 buttons.setBackground(Color.red);
 buttons.setLayout(new FlowLayout());
 buttons.add(up);
 buttons.add(down);
 buttons.add(left);
 buttons.add(right);
 buttons.add(quit);
 setLayout(new BorderLayout());
 add("South",buttons);
}
public void paint(Graphics g){
g.setColor(Color.red);
g.drawImage(bird,x,y,39,45,this);
}
public void moveUp(){
y=-INC;}
public void moveDown(){
y=+INC;}
public void moveLeft(){
x=-INC;}
public void moveRight(){
x=+INC;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==quit)
System.exit(0);
else if(e.getSource()==up)
moveUp();
else if(e.getSource()==down)
moveDown();
else if(e.getSource()==left)
moveLeft();
else if(e.getSource()==right)
moveRight();
repaint();
}
public static void main(String args[])
{MoveBird a=new MoveBird();}}

解决方案 »

  1.   

    Image bird=Toolkit.getDefaultToolkit().getImage("bird.jpg"); 用绝对路径Image bird=Toolkit.getDefaultToolkit().getImage("d:\\img\\bird.jpg"); 另外
    public void moveUp(){ 
    y=-INC;} 
    public void moveDown(){ 
    y=+INC;} 
    public void moveLeft(){ 
    x=-INC;} 
    public void moveRight(){ 
    x=+INC; 

    改为
    public void moveUp(){ 
    y-=INC;} 
    public void moveDown(){ 
    y+=INC;} 
    public void moveLeft(){ 
    x-=INC;} 
    public void moveRight(){ 
    x+=INC; 
      

  2.   

    Image bird=Toolkit.getDefaultToolkit().getImage("bird.jpg"); 
      这个图片路径不对:应是Image bird=Toolkit.getDefaultToolkit().getImage("盘符:\\路径");
        路径之间必须是"\\"隔开