我编译了有两处错误 但是我不知道怎么改哦~这个程序是实现用键盘的上下左右控制button的移动
import java.awt.event.*;import java.applet.*;
import java.awt.*;
public class 1
{ button b_go=new button("go"),
int b_x=0,b_y=0;
public void init()
{ b_go.addkeylistener(this);
}
public void keypressed(keyevent e)
{ if(e.getkeycode()==leyevent.VK_UP)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_y=b_y-2;b_go.setlocation(b_x,b_y);
}
if(e.getkeycode()==leyevent.VK_DOWN)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_y=b_y+2;b_go.setlocation(b_x,b_y);
}
if(e.getkeycode()==leyevent.VK_RIGHT)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_x=b_x+2;b_go.setlocation(b_x,b_y);
}
if(e.getkeycode()==leyevent.VK_LETT)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_x=b_x-2;b_go.setlocation(b_x,b_y);
}
}
public void keyreleased(keyevent e){}
public vold keytyped(keyevent e){}
public voide actionperformed(actionevent e)
{ b_go.setbounds(22,45,20,20);
b_x=b_go.getbounds().x;b_y=b_go.getbounds().y;
b_go.requestfocus() ;
}
}
----------------解决方案--------------------------------------------------------
你这个程序何止两个错误
import java.awt.event.*;import java.applet.*;
import java.awt.*;
public class 1
{ button b_go=new button("go"),
int b_x=0,b_y=0;
public void init()
{ b_go.addkeylistener(this);
}
public void keypressed(keyevent e)
{ if(e.getkeycode()==leyevent.VK_UP)//怎么又变成了leyevent,错得太离谱了,keyevent还说得过去,也是错的,应该是大写的KeyEvent
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_y=b_y-2;b_go.setlocation(b_x,b_y);
}
if(e.getkeycode()==leyevent.VK_DOWN)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_y=b_y+2;b_go.setlocation(b_x,b_y);
}
if(e.getkeycode()==leyevent.VK_RIGHT)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_x=b_x+2;b_go.setlocation(b_x,b_y);
}
if(e.getkeycode()==leyevent.VK_LETT)
{ rectangle rect=new rectangle(b_x,b_y,20,20);
b_x=b_x-2;b_go.setlocation(b_x,b_y);
}
}
public void keyreleased(keyevent e){}
public vold keytyped(keyevent e){}
public voide actionperformed(actionevent e)
{ b_go.setbounds(22,45,20,20);
b_x=b_go.getbounds().x;b_y=b_go.getbounds().y;
b_go.requestfocus() ;
}
}
你把所有的东西都小写了,要记住在JAVA中,对大小写是很敏感的,很多方法中字母都在大写的
你还是把JAVA基本的东西学一下吧,这些事件处理的,以后可以慢慢学。不急的
----------------解决方案--------------------------------------------------------
import java.awt.event.*;import java.applet.*;
import java.awt.*;
public class 1
{ Button b_go=new Button("go"),
int b_x=0,b_y=0;
public void init()
{ b_go.addkeylistener(this);
}
public void keyPressed(KeyEvent e)
{ if(e.getKeycode()==KeyEvent.VK_UP)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_y=b_y-2;b_go.setLocation(b_x,b_y);
}
if(e.getKeycode()==KeyEvent.VK_DOWN)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_y=b_y+2;b_go.setLocation(b_x,b_y);
}
if(e.getKeycode()==KeyEvent.VK_RIGHT)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_x=b_x+2;b_go.setLocation(b_x,b_y);
}
if(e.getKeycode()==KeyEvent.VK_LETT)
{ Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_x=b_x-2;b_go.setLocation(b_x,b_y);
}
}
public void keyReleased(KeyEvent e){}
public vold keyTyped(KeyEvent e){}
public voide actionPerFormed(ActionEvent e)
{ b_go.setBounds(22,45,20,20);
b_x=b_go.getBounds().x;b_y=b_go.getBounds().y;
b_go.requestFocus() ;
}
}
改过来了哦~还有什么地方哦?
----------------解决方案--------------------------------------------------------
/*<applet code="l.class" width=200 height=200>
</applet>*/
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class l extends JApplet implements KeyListener,ActionListener
{
Container cp;
Button b_go=new Button("go");
int b_x=0,b_y=0;
public void init()
{
cp=getContentPane();
cp.add(b_go);
b_go.addKeyListener(this);
b_go.addActionListener(this);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
Rectangle rect=new Rectangle(b_x,b_y,20,20);
//g.drawRect(b_x,b_y,20,20);
b_y=b_y-2;
b_go.setLocation(b_x,b_y);
//g.drawRect(rect);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_y=b_y+2;b_go.setLocation(b_x,b_y);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_x=b_x+2;b_go.setLocation(b_x,b_y);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
Rectangle rect=new Rectangle(b_x,b_y,20,20);
b_x=b_x-2;b_go.setLocation(b_x,b_y);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{ b_go.setBounds(22,45,20,20);
b_x=b_go.getBounds().x;b_y=b_go.getBounds().y;
b_go.requestFocus() ;
}
}
[此贴子已经被作者于2006-4-27 18:34:31编辑过]
----------------解决方案--------------------------------------------------------
已经可以了 你程序错误太多了 真是受不了
[此贴子已经被作者于2006-4-27 18:35:40编辑过]
----------------解决方案--------------------------------------------------------
这会儿忙!
等我看了再说!
----------------解决方案--------------------------------------------------------
是很难啊
----------------解决方案--------------------------------------------------------