这是一个可以控制移动的圆!键盘按左就往左移动一下,右就右移。。~~~importjava.awt.*;importjava.awt.event.*;publicclasswork{publicstaticvoidmain(String[]args){TFramet=newTFr...
这是一个可以控制移动的圆!键盘按左就往左移动一下,右就右移。。~~~
import java.awt.*;
import java.awt.event.*;
public class work
{
public static void main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame
{
static int x=50;
static int y=50;
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
{
y=y-5;
}
}
else if(keycode == KeyEvent.VK_DOWN)
{
y=y+5;
}
else if(keycode == KeyEvent.VK_LEFT)
{
x=x-5;
}
else if(keycode == KeyEvent.VK_RIGHT)
{
x=x+5;
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
}
现在想改进一下,要求改成按左就一直往左移动,而且是能看到在移动,其他方向也是,希望各位高手指点一下啊,我想了很久,结果做出来是一下就移开了~~~~
想问下那位给出程序的朋友,你那个程序要导入text包是吧?
展开