这是其中的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()
{
}
}
我碰见的问题有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()
{
}
}
解决方案 »
- 谁能给我解释下程序中的this是干什么的?
- 问个数学几何题可以吗?
- 求指教。。。关于Java GUI的布局问题(急)
- 我的理解对么?
- 用遗传算法写了一个程序 但不能得到预期的结果,自己认为是写的有问题,可能主要是myFitnessFunction有问题吧,高手请进
- 关于内部类的问题.
- no resultset was produced的问题
- 如果将形如(MM/dd/yyyy HH:mm:ss.SSS)的字符串日期转成Date类型
- 各位大哥,哪里有大量原版的国外计算技书下载,请告诉小弟!!!Thankyou!
- 刚学java想试编一个文本编辑器,各位能不能给推荐一些较好的参考程序或实例
- 是否有java图像编书下载
- 检查交换机状态
有人想玩玩吗?
我的email: [email protected]
2.应该先setlayout(null),再设置位置。