【发布时间】:2012-11-07 08:19:56
【问题描述】:
我有这样的代码:
// In MyPanel.java
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Draw something
mypanel_count++;
}
// In Test.java
public void testLargeData()
{
while (notDone)
{
panel.repaint();
// do huge work
test_count++;
System.out.println("Test_count: " + test_count + ", MyPanel_count: " + mypanel_count);
}
}
// Output !!!
Test_count: 752, MyPanel_count: 23
Test_count: 753, MyPanel_count: 23
Test_count: 754, MyPanel_count: 23
Test_count: 755, MyPanel_count: 24
但是当我将panel.repaint() 更改为panel.paintComponent(panel.getGraphics()) 时,结果是正确的:
Test_count: 752, MyPanel_count: 752 Test_count: 753, MyPanel_count: 753 Test_count: 754, MyPanel_count: 754 Test_count: 755, MyPanel_count: 755
为什么? paintComponent 方法有效,但有时它是盲目的,所以我不想使用它。有人可以给我一些建议吗?谢谢!
【问题讨论】:
-
@Raghunandan:谢谢你的回答,但这不是我的麻烦。
-
mKorbel 建议的 paintImmediately(...) 应该有所帮助。stackoverflow.com/questions/4120528/repaint-in-a-loop。我知道这不是答案。但我想解决方案是线程。
-
SwingUtilities.invokeLater不适合我。
标签: java swing repaint paintcomponent