public void run() { try { pumpEvents(new Conditional() { public boolean evaluate() { return true; } }); } finally { /* * This synchronized block is to secure that the event dispatch * thread won't die in the middle of posting a new event to the * associated event queue. It is important because we notify * that the event dispatch thread is busy after posting a new event * to its queue, so the EventQueue.dispatchThread reference must * be valid at that point. */ synchronized (theQueue) { if (theQueue.getDispatchThread() == this) { theQueue.detachDispatchThread(); } /* * Event dispatch thread dies in case of an uncaught exception. * A new event dispatch thread for this queue will be started * only if a new event is posted to it. In case if no more * events are posted after this thread died all events that * currently are in the queue will never be dispatched. */ /* * Fix for 4648733. Check both the associated java event * queue and the PostEventQueue. */ if (theQueue.peekEvent() != null || !SunToolkit.isPostEventQueueEmpty()) { theQueue.initDispatchThread(); } AWTAutoShutdown.getInstance().notifyThreadFree(this); } } }
楼主可以不用考虑这么深。
你只要知道你是重写了这个方法而系统会自动调用。这个方法是重绘所有组件的方法。
JComponent 源码如下: protected void paintComponent(Graphics g) {
if (ui != null) {
Graphics scratchGraphics = (g == null) ? null : g.create();
try {
ui.update(scratchGraphics, this);
}
finally {
scratchGraphics.dispose();
}
}
}
public void update(Graphics g) {
paint(g);
}可以看出调用顺序paintComponent-->update-->paint
就像awt上的Canvas中的repaint->update->paint一样!
try {
pumpEvents(new Conditional() {
public boolean evaluate() {
return true;
}
});
} finally {
/*
* This synchronized block is to secure that the event dispatch
* thread won't die in the middle of posting a new event to the
* associated event queue. It is important because we notify
* that the event dispatch thread is busy after posting a new event
* to its queue, so the EventQueue.dispatchThread reference must
* be valid at that point.
*/
synchronized (theQueue) {
if (theQueue.getDispatchThread() == this) {
theQueue.detachDispatchThread();
}
/*
* Event dispatch thread dies in case of an uncaught exception.
* A new event dispatch thread for this queue will be started
* only if a new event is posted to it. In case if no more
* events are posted after this thread died all events that
* currently are in the queue will never be dispatched.
*/
/*
* Fix for 4648733. Check both the associated java event
* queue and the PostEventQueue.
*/
if (theQueue.peekEvent() != null ||
!SunToolkit.isPostEventQueueEmpty()) {
theQueue.initDispatchThread();
}
AWTAutoShutdown.getInstance().notifyThreadFree(this);
}
}
}