repaint() in Java doesn't “re-paint” immediately?

前端 未结 3 1049
余生分开走
余生分开走 2020-12-15 13:25

I have a code like that:

// In MyPanel.java
public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    // Draw something
    mypanel_count++;
         


        
3条回答
  •  不知归路
    2020-12-15 13:37

    As the other answers say: it's a problem of when AWT is calling paint().

    If you do some work that needs information from painted/layouted Components, what helps is also to put this work into a worker thread that waits until the painting is done.

    In your case that would be something like:

    panel.repaint();
    
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // do huge work
            test_count++;
            System.out.println("Test_count: " + test_count 
                 + ", MyPanel_count: " + mypanel_count);
        }
    });
    

    Although I'm not sure how it would behave in your while loop.

提交回复
热议问题