为什么paintComponent执行了两次? [英] Why is paintComponent executed 2 times?

查看:190
本文介绍了为什么paintComponent执行了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

MembsershipFunctionComponent扩展了JComponent.为什么paintComponent方法执行两次?

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

推荐答案

paintComponent几乎可以在任何时候被Swing框架调用.何时发生这种情况的示例:

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

  • 每次调整组件大小
  • 显示组件的任何时间部分(例如在可滚动的窗口中)
  • 每次在您的组件(或可能是父组件或子组件)上调用repaint()方法
  • 每当布局发生更改时

这一切都不会让您担心-您只需要编写代码,就不必关心paintComponent被调用了多少次.

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

这篇关于为什么paintComponent执行了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆