我有一個項目,當它存在時,它似乎不斷重新繪製,導致CPU隨着它在我的任何窗口中都會尖峯化。它直接從JLabel繼承,與屏幕上的其他JLabel不同,它具有紅色背景和邊框。我不知道爲什麼它會不同,不斷重新繪畫。這個callstack看起來像這樣:爲什麼JLabel不斷重新粉刷?
Thread [AWT-EventQueue-1] (Suspended (breakpoint at line 260 in sItem))
sItem.paint(Graphics) line: 260
sItem(JComponent).paintToOffscreen(Graphics, int, int, int, int, int, int) line: 5124
RepaintManager$PaintManager.paintDoubleBuffered(JComponent, Image, Graphics, int, int, int, int) line: 1475
RepaintManager$PaintManager.paint(JComponent, JComponent, Graphics, int, int, int, int) line: 1406
RepaintManager.paint(JComponent, JComponent, Graphics, int, int, int, int) line: 1220
sItem(JComponent)._paintImmediately(int, int, int, int) line: 5072
sItem(JComponent).paintImmediately(int, int, int, int) line: 4882
RepaintManager.paintDirtyRegions(Map<Component,Rectangle>) line: 803
RepaintManager.paintDirtyRegions() line: 714
RepaintManager.seqPaintDirtyRegions() line: 694 [local variables unavailable]
SystemEventQueueUtilities$ComponentWorkRequest.run() line: 128
InvocationEvent.dispatch() line: 209
summitEventQueue(EventQueue).dispatchEvent(AWTEvent) line: 597
summitEventQueue(SummitHackableEventQueue).dispatchEvent(AWTEvent) line: 26
summitEventQueue.dispatchEvent(AWTEvent) line: 62
EventDispatchThread.pumpOneEventForFilters(int) line: 269
EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: 184
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 174
EventDispatchThread.pumpEvents(int, Conditional) line: 169
EventDispatchThread.pumpEvents(Conditional) line: 161
EventDispatchThread.run() line: 122 [local variables unavailable]
它基本上只是不斷地擊中一遍又一遍,只要我可以按繼續。這是「獨一無二」的,以該特定標籤的代碼看起來大約是這樣的:
bgColor = OurColors.clrWindowTextAlert;
textColor = Color.white;
setBackground(bgColor);
setOpaque(true);
setSize(150, getHeight());
Border border_warning = BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(1, 1, 1, 1, OurColors.clrXBoxBorder),
Global.border_left_margin);
setBorder(border_warning);
顯然,它做更多,但特定塊只存在這些標籤所造成的秒殺/連續重繪。
任何想法爲什麼它會繼續重新繪製這個特定的標籤?
偉大的想法,但涉及的邏輯要求他們不幸在渲染循環中設置。有各種各樣的邏輯可以使事物的表現略有不同。儘管在渲染循環中創建邊框似乎是問題,但您確實處於正確的軌道上(請參閱我的答案)。不確定爲什麼。 – Morinar 2010-05-07 20:42:33
設置這些項目的邏輯不需要在paint()方法中,將它們設置在paint方法之外將導致調用paint方法。考慮一下,當繪製東西時,你不會改變自己想要繪製的東西,因爲那樣你就需要重繪它。如果必須更改此標籤,則應在繪製標籤之前完成該標籤,然後該更改應該(正確)觸發重新繪製的需要。 – 2010-05-07 20:53:20
我相信你是對的,但是這個代碼已經在生產了10年了,而且我不打算在事情上施加壓力。感謝您的邊界信息。 – Morinar 2010-05-07 21:48:11