Swing中paint、paintComponent和paintComponents的区别 [英] Difference between paint, paintComponent and paintComponents in Swing

查看:44
本文介绍了Swing中paint、paintComponent和paintComponents的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java Swing 中 paint()paintComponent()paintComponents() 的实际区别是什么?

What is the actual difference between paint(), paintComponent() and paintComponents() in Java Swing?

我试图理解 Oracle 文档中的解释,但我不清楚.

I tried to understand what explained in Oracle docs but I am not clear.

推荐答案

  • AWT,覆盖paint().
  • Swing 顶级容器(例如,JFrameJWindowJDialogJApplet ..),覆盖 paint().但是有很多很好的理由在 TLC 中绘画.一个单独问题的主题,也许.
  • Swing 的其余部分(任何从 JComponent 派生的组件),覆盖 paintComponent().
  • 既不覆盖也不显式调用 paintComponents(),让 API 在需要时调用它.
    • AWT, override paint().
    • Swing top-level container (e.g.s are JFrame, JWindow, JDialog, JApplet ..), override paint(). But there are a number of good reasons not to paint in a TLC. A subject for a separate question, perhaps.
    • The rest of Swing (any component that derives from JComponent), override paintComponent().
    • Neither override nor explicitly call paintComponents(), leave it to the API to call it when needed.
    • 确保在覆盖方法时也使用 @Override 表示法.

      Be sure to also use @Override notation whenever overriding a method.

      这样做会暗示在 JFrame 中试图覆盖 paintComponent(..) 的问题(它没有这样的方法),这很常见.

      Doing so would hint at the problem of trying to override paintComponent(..) in a JFrame (it has no such method), which is quite common to see.

      这篇关于Swing中paint、paintComponent和paintComponents的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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