Why is paintComponent executed 2 times?

£可爱£侵袭症+ 提交于 2020-01-04 05:15:07

问题


I have following code:

        canvas=new MembershipFunctionComponent(functions);
        canvas.setPreferredSize(new Dimension((int)this.getWidth(), (int)this.getHeight()));
        canvas.addMouseListener(canvas);
        pane.add(canvas);

MembsershipFunctionComponent extends JComponent. Why is paintComponent method executed 2 times?


回答1:


paintComponent can get called at pretty much any time by the Swing framework. Examples of when this might happen:

  • Any time a component is resized
  • Any time part of the component is revealed (for example in a scrollable window)
  • Any time the repaint() method is called on your component (or possibly a parent or sub-component)
  • Any time there is a layout change

None of this should worry you - you should just write your code so that it doesn't care how many times paintComponent is called.



来源:https://stackoverflow.com/questions/9355395/why-is-paintcomponent-executed-2-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!