我使用 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();
}
}