java super.paintComponent(g) 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28724609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:04:44  来源:igfitidea点击:

What does super.paintComponent(g) do?

javaswinggraphics

提问by user3437460

What does super.paintComponent(g)do (especially when we place it inside paintComponent() method)? I am surprised I don't see anyone asking this in SO before.

有什么作用super.paintComponent(g)(尤其是当我们将它放在paintComponent() 方法中时)?我很惊讶我之前没有看到有人在 SO 中问过这个问题。

I dig out my school notes on Java Graphics, the only thing it mentioned on this line of code is "do not delete".

我翻出了我关于 Java Graphics 的学校笔记,它在这行代码中唯一提到的是"do not delete".

However I have been practicing and tying out on Java paintComponent() method these few weeks. So far I have not included that line into my codes, and everything seems to work well (so far). So..

然而,这几周我一直在练习和使用 Java 的paintComponent() 方法。到目前为止,我还没有将该行包含在我的代码中,而且一切似乎都运行良好(到目前为止)。所以..

Questions:

问题:

  1. What does it do?
  2. When do we need to use it?
  3. What advantage does it gives us by writing it in paintComponent()?
  1. 它有什么作用?
  2. 我们什么时候需要使用它?
  3. 把它写在paintComponent()中给我们带来了什么好处?

采纳答案by aioobe

  1. What does it do?
  1. 它有什么作用?

It prints the component as if you hadn't overridden the paintComponentmethod. If you have a background color set for instance, this is typically painted by the class you're extending.

它打印组件,就好像您没有覆盖该paintComponent方法一样。例如,如果您设置了背景颜色,则这通常由您要扩展的类绘制。

  1. When do we need to use it?
  1. 我们什么时候需要使用它?

You use it if you don't paint on the entire component. The parts that you don't paint will "shine through" which means that you should let the super class paint those parts. As with the example of the background color for instance: If you just paint a circle in the middle of the component, super.paintComponentwill make sure the background color is painted around the circle.

如果您不在整个组件上绘画,则可以使用它。你不涂漆的部分会“发光”,这意味着你应该让超类来涂刷这些部分。以背景色为例:如果你只是在组件的中间画一个圆圈,super.paintComponent将确保背景颜色是围绕着圆圈画的。

If you dopaint the entire area of your component, then you will paint on top of whatever super.paintComponent paints and thus there's no point in calling super.paintComponent.

如果您确实绘制了组件的整个区域,那么您将绘制在任何 super.paintComponent 绘制的顶部,因此调用 super.paintComponent 毫无意义。

  1. What advantage does it gives us by writing it in paintComponent()?
  1. 把它写在paintComponent()中给我们带来了什么好处?

That's the only logical place to put it. paintComponentis called when the component should be painted, and, as mentioned above, if you don't paint the entire component yourself, you need super.paintComponentto paint on the parts that shine through.

这是唯一合乎逻辑的地方。paintComponent在应该绘制组件时调用,并且如上所述,如果您不自己绘制整个组件,则需要super.paintComponent在发光的部分上进行绘制。

The documentationof paintComponentsays it pretty well:

文档paintComponent说,它相当不错:

[...] if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

[...] 如果你不调用 super 的实现,你必须尊重 opaque 属性,也就是说,如果这个组件是不透明的,你必须用非不透明的颜色完全填充背景。如果您不遵守不透明属性,您可能会看到视觉伪影。

回答by Kevin Workman

From the Java Painting Tutorial:

来自Java 绘画教程

Most of the standard Swing components have their look and feel implemented by separate "UI Delegate" objects. The invocation of super.paintComponent(g) passes the graphics context off to the component's UI delegate, which paints the panel's background. For a closer look at this process, see the section entitled "Painting and the UI Delegate" in the aforementioned SDN article.

大多数标准 Swing 组件的外观和感觉是由单独的“UI 委托”对象实现的。super.paintComponent(g) 的调用将图形上下文传递给组件的 UI 委托,后者绘制面板的背景。要详细了解此过程,请参阅上述 SDN 文章中标题为“绘画和 UI 委托”的部分。