import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class CustomEventPanel extends JPanel
implements TimerListener
{ public CustomEventPanel()
{ Timer t = new Timer(1000);
t.addTimerListener(this);
}
public void timeElapsed(TimerEvent evt)
{ Graphics g = getGraphics();
g.fillRect(0, 0, ticks, 10);
ticks++;
g.dispose(); // 为什么要调用这个方法,在paintComponent()下就不用?
}
private int ticks = 0;
}
interface TimerListener extends EventListener
{ public void timeElapsed(TimerEvent evt); // 这个方法完成什么作用啊?
}
class Timer extends Component implements Runnable
{ public Timer(int i)
{ interval = i;
Thread t = new Thread(this);
t.start();
evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
enableEvents(0);
}
public void addTimerListener(TimerListener l)
{ listener = l;
}
public void run()
{ while (true)
{ try { Thread.sleep(interval);}
catch(InterruptedException e)
{ }
TimerEvent te = new TimerEvent(this);
evtq.postEvent(te);
}
}
public void processEvent(AWTEvent evt)
{ if (evt instanceof TimerEvent)
{ if (listener != null)
listener.timeElapsed((TimerEvent)evt);
}
else super.processEvent(evt);
}
private int interval;
private TimerListener listener;
private static EventQueue evtq;
}
class TimerEvent extends AWTEvent
{ public TimerEvent(Timer t) { super(t, TIMER_EVENT);}
public static final int TIMER_EVENT =
AWTEvent.RESERVED_ID_MAX + 5555;
}
class CustomEventFrame extends JFrame
{ public CustomEventFrame()
{ setTitle("CustomEventTest");
setSize(500, 50);
Container contentPane = getContentPane();
contentPane.add(new CustomEventPanel());
}
}
public class CustomEventTest
{ public static void main(String[] args)
{ JFrame frame = new CustomEventFrame();
frame.show();// 这里用isVisible(true)也是一样吗?有区别吗?
}
}// 能不能解释一下在执行程序时,后台都做了什么事??