这是其中的2个类,作用只是在jFrame中显示自定义的画布,并且添加KeyLisetner。
我碰见的问题有2:
1.按键盘没有反映,我想要的是图片的切换。
2.被添加的画布,位置是不能自己设置的,总是在左上角比如myPictrue[0].setLocation(100,100);
  没用。  请问大家 可以帮我看看吗?
import java.awt.*;
import java.awt.event.*;
public class MyPictrue extends Canvas implements KeyListener
{
private Image image_true,image_false;
private char key;
volatile boolean direction=true; //函数返回
private Rectangle rect;  //函数返回
private int x,y;
MyPictrue(Image image_true,Image image_false,char key)
{
this.image_true=image_true;
this.image_false=image_false;
this.key=key;
rect=this.getBounds();
this.setSize(image_true.getWidth(this),image_true.getHeight(this));
repaint();
}
public Rectangle getRect()   /////////对象返回位置
{
return rect;
}
public void paint(Graphics g)
{
if(direction == true)
{
     g.drawImage(image_true,0,0,image_true.getWidth(this),image_true.getHeight(this),this);
}else
if(direction == false)
{
                       g.drawImage(image_false,0,0,image_false.getWidth(this),image_false.getHeight(this),this);
}
this.validate();
}
public boolean getDirection()/////返回运动方向,就是显示的图片
{
return direction;
}
public void keyTyped(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_7);
{
int i=0;
if( (i++)%2 == 0 )
{
direction=true;
System.out.println("direction="+direction);
repaint();
}
else
{
direction=false;
repaint();
}
}
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
}
//////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GameFrame extends JFrame implements Runnable
{
static volatile int number = 0;
private MyPictrue[] myPictrue;
private MyCar[] myCar;
Toolkit tool = this.getToolkit();
Image image_true,image_false;
Container con=this.getContentPane();
GameFrame()
{
image_true = tool.getImage("transfers_true.jpg");
image_false = tool.getImage("transfers_false.jpg");
myPictrue = new MyPictrue[1] ;
myPictrue[0]=new MyPictrue(image_true,image_false,'7');
con.add(myPictrue[0]);
myPictrue[0].setLocation(100,100);
this.validate();
}
public void run()
{

}
}