一个关于Timer的问题!
这是我写的一个关于Timer的程序,要求0.5秒变换一次直线的颜色,调试通过了,但在运行中总是出错,各位大大帮忙看看!!谢谢了!//===============E1103.java===============
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class E1103 extends JFrame
{
private int count=1;
private Color color ;
private Graphics g;
private Font font;
public E1103()
{
super("E1003");
setSize(400,180);
setVisible(true);
}
public void paint(Graphics s)
{
ActionListener task = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
font = new Font("Serif",Font.BOLD,20);
switch (count%3)
{
case 1: color= Color.red; break;
case 2: color= Color.green; break;
default: color= Color.blue;
}
g.setFont(new Font("Serif",Font.BOLD,20));
g.setColor(color);
g.drawString("按顺序变换水平线为红绿蓝色",60,100);
g.drawLine(60,150,320,150);
g.drawLine(70,154,330,154);
g.drawLine(80,158,340,158);
count++;
}
};
Timer time = new Timer(500, task);
time.start();
}
public static void main(String[] args)
{
E1103 app = new E1103();
app.addWindowListener(new MyWindowListener());
}
}