简体   繁体   English

连续调用paintComponent

[英]paintComponent called continuously

Similar problem here but didn't find an answer: Why is paintComponent() continuously and asynchronously being called without explicit repaint() call?类似的问题在这里但没有找到答案: 为什么在没有显式 repaint() 调用的情况下连续和异步调用paintComponent()?

I have a JPanel that I am drawing onto我有一个正在绘制的 JPanel

class DrawPanel extends JPanel {

final void paintComponent(Graphics g) {
        super.paintComponent(g);
      // some graphics drawing stuff
} }

and then adding this to a JScrollPane.然后将其添加到 JScrollPane。 However if I put a system.out.println() in the paintComponent method I can see its continuously being called.但是,如果我将 system.out.println() 放入 PaintComponent 方法中,我可以看到它不断被调用。 Any way to stop this?有什么办法可以阻止这个吗? According to the link its possible due to the jpanel being covered根据链接,由于覆盖了 jpanel,因此可能

Generally paintComponent() does not paint continually .通常paintComponent() 不会continually绘制。 It get invoked occasionally when Swing determines it needs be painted.当 Swing 确定需要绘制它时,它会occasionally被调用。

If your method is being invoked continually then I can think of a couple of possible problems.如果您的方法不断被调用,那么我可以想到几个可能的问题。 You are:你是:

  1. manually invoking repaint()手动调用 repaint()
  2. changing a property of the component in the paintComponent() method which then automatically invokes repaint()在paintComponent() 方法中更改组件的属性,然后该方法会自动调用repaint()

The paintComponent calls come from Swing's Event Dispatch Thread. PaintComponent 调用来自 Swing 的事件调度线程。 It gets called everytime the component needs to be repainted.每次需要重新绘制组件时都会调用它。 If you resize the component or bring it back from minimized state, then it's repainted.如果您调整组件的大小或将其从最小化状态恢复,则它会被重新绘制。 Of course if you cover it with another component then the repainting will be called less.当然,如果你用另一个组件覆盖它,那么重绘的调用就会减少。 The other component will have a paintComponent method too though.不过,另一个组件也会有一个paintComponent 方法。 Nothing to worry about.没什么可担心的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM