java图形界面中键盘控制物体移动
我的代码如下,我希望实现,可以用上下左右键控制图形界面里的物体移动,然后用一个小汽车的图标替代我现在的这个绿色的方块.谢谢大家,感激涕零!!!!importjava.aw...
我的代码如下,我希望实现 , 可以用上下左右键控制图形界面里的物体移动,然后用一个小汽车的图标替代我现在的这个绿色的方块. 谢谢大家,感激涕零!!!!
import java.awt.*;
import java.awt.event.*;
class mCar extends Frame{
TextField txt;
Color redColor;
public mCar(){
txt = new TextField();
add("North",txt);
txt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_UP)
System.out.println("\n Go Up");
else if(e.getKeyCode()== KeyEvent.VK_DOWN)
System.out.println("\n Go Down");
else if(e.getKeyCode()== KeyEvent.VK_LEFT)
System.out.println("\n Go Left");
else if(e.getKeyCode()== KeyEvent.VK_RIGHT)
System.out.println("\n Go Right");
else if(e.getKeyCode()== KeyEvent.VK_F1)
System.out.println("\n Speed Up");
else if(e.getKeyCode()== KeyEvent.VK_F2)
System.out.println("\n Speed Down");
else
System.out.println(e.getKeyChar());
}
}
);
setSize(400,300);
setVisible(true);
setLocation(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(80, 80, 40, 40);
}
}
public class miniCar {
public static void main(String[] args){
new mCar();
}
}
麻烦把图形界面顶部的TextField部分 也去掉 我去掉以后 什么都显示不出来,是不是要用repaint()?怎么用? 展开
import java.awt.*;
import java.awt.event.*;
class mCar extends Frame{
TextField txt;
Color redColor;
public mCar(){
txt = new TextField();
add("North",txt);
txt.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_UP)
System.out.println("\n Go Up");
else if(e.getKeyCode()== KeyEvent.VK_DOWN)
System.out.println("\n Go Down");
else if(e.getKeyCode()== KeyEvent.VK_LEFT)
System.out.println("\n Go Left");
else if(e.getKeyCode()== KeyEvent.VK_RIGHT)
System.out.println("\n Go Right");
else if(e.getKeyCode()== KeyEvent.VK_F1)
System.out.println("\n Speed Up");
else if(e.getKeyCode()== KeyEvent.VK_F2)
System.out.println("\n Speed Down");
else
System.out.println(e.getKeyChar());
}
}
);
setSize(400,300);
setVisible(true);
setLocation(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(80, 80, 40, 40);
}
}
public class miniCar {
public static void main(String[] args){
new mCar();
}
}
麻烦把图形界面顶部的TextField部分 也去掉 我去掉以后 什么都显示不出来,是不是要用repaint()?怎么用? 展开
2个回答
推荐于2018-04-05
展开全部
去掉TextField后的程序,这个程序是要用到repaint()的,具体请参考程序中的注释位置:
import java.awt.*;
import java.awt.event.*;
class mCar extends Frame{
Color redColor;
int xl=80,yl=80,speed=10,step=5;/*********注意这里***********/
public mCar(){
addKeyListener(new KeyAdapter(){ /*********注意这里***********/
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_UP){
System.out.println("\n Go Up");
yl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_DOWN){
System.out.println("\n Go Down");
yl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_LEFT){
System.out.println("\n Go Left");
xl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_RIGHT){
System.out.println("\n Go Right");
xl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_F1){
speed+=step;/*********注意这里***********/
System.out.println("\n Speed Up");
}
else if(e.getKeyCode()== KeyEvent.VK_F2){
System.out.println("\n Speed Down");
speed-=step;/*********注意这里***********/
}
else
System.out.println(e.getKeyChar());
repaint();/*********注意这里***********/
}
}
);
setSize(400,300);
setVisible(true);
setLocation(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(xl, yl, 40, 40);/*********注意这里***********/
}
}
public class miniCar {
public static void main(String[] args){
new mCar();
}
}
import java.awt.*;
import java.awt.event.*;
class mCar extends Frame{
Color redColor;
int xl=80,yl=80,speed=10,step=5;/*********注意这里***********/
public mCar(){
addKeyListener(new KeyAdapter(){ /*********注意这里***********/
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_UP){
System.out.println("\n Go Up");
yl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_DOWN){
System.out.println("\n Go Down");
yl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_LEFT){
System.out.println("\n Go Left");
xl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_RIGHT){
System.out.println("\n Go Right");
xl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_F1){
speed+=step;/*********注意这里***********/
System.out.println("\n Speed Up");
}
else if(e.getKeyCode()== KeyEvent.VK_F2){
System.out.println("\n Speed Down");
speed-=step;/*********注意这里***********/
}
else
System.out.println(e.getKeyChar());
repaint();/*********注意这里***********/
}
}
);
setSize(400,300);
setVisible(true);
setLocation(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(xl, yl, 40, 40);/*********注意这里***********/
}
}
public class miniCar {
public static void main(String[] args){
new mCar();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
留个脚印,有空来看,现在上班
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询