Java 使用JCreator运行动画

Java 使用JCreator运行动画,java,swing,jpanel,paintcomponent,jcreator,Java,Swing,Jpanel,Paintcomponent,Jcreator,我用JCreator编写了一个红色矩形的动画程序,我发现运行annimation有困难,有人有什么建议吗 这是为矩形设置动画的程序: 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

我用JCreator编写了一个红色矩形的动画程序,我发现运行annimation有困难,有人有什么建议吗

这是为矩形设置动画的程序:

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

你能详细说明你遇到了什么问题吗?我不知道我将如何运行这个程序,并看到矩形移动,我已经尝试在主方法中运行它,但我没有成功你提供的代码中实际上没有主方法。。。