我编写了这个使用JCreator动画红色矩形的程序,我发现运行动画有困难,有没有人有任何建议?
这是激活矩形的程序:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Tutorial extends JPanel implements ActionListener
{
Timer tm = new Timer(5, this);
int x = 0 , velX = 2;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(x, 30, 50, 30);
tm.start();
}
public void actionPerformed(ActionEvent e){
x = x + velX;
repaint();
}
}