Java super.paint(g) 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23434158/
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-08-13 22:59:11  来源:igfitidea点击:

What is use of super.paint(g)?

javaappletawtpaintsuper

提问by unknownerror

Can someone explain me what is the use of super.paint(g)where, gis a Graphicsvariable in Applets or awt or swings or in Java.

有人可以解释一下super.paint(g)where, gis a Graphicsvariable in Applets or awt or swings or in Java.

I have done research and found that it is used to override but what is the use of this override?

我做了研究,发现它是用来覆盖的,但是这个覆盖有什么用?

I am a beginner. If possible can you explain the difference between paint(g)and super.paint(g)with a small exampleor please help me with this code?

我是初学者。如果可能的话,你可以解释之间的差别paint(g),并super.paint(g)用小example或请帮助我这个代码?

/*
Let us consider this code 
This has only one paint declaration i.e; subclass's paint method declaration, no     declaration for superclass's paint function... when we explicitly call superclass's paint function 
what is the use of super.paint(g) and is it going to use superclass's paint declaration??
*/

import java.awt.*;
import java.applet.*;
/*
<applet code="superpaintDemo" height=768 width=1366>
</applet>
*/
class superpaintDemo extends Applet
{

    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawString("This is Demo",200,200);
    }
}

采纳答案by mztkenan

  1. First, I add super.paint(g) in my own paint method.
  1. 首先,我在自己的绘制方法中添加了 super.paint(g)。

When it repaints, things drawn before are cleared.

当它重新绘制时,之前绘制的东西会被清除。

Sorry, I can't have the reputation to post more than 2 links.

抱歉,我不能拥有发布 2 个以上链接的声誉。

  1. Then, I remove super.paint(g) in my own paint method.

    remove super.paint(g)

  1. 然后,我在我自己的绘制方法中删除 super.paint(g)。

    删除 super.paint(g)

You can see things drawn on the Panel before are still there.

你可以看到之前画在面板上的东西还在那里。

When you add super.paint(g), in your method it will call the method in the superclass of this subclass.My class RussiaPanel extends JPanel, and JPanel extends JComponent. It will invoke the "public void paint(Graphic g)" method in JComponet, and the things on the Panel will be cleared.For more details, you can refer to the API docs.

当你添加super.paint(g)时,在你的方法中它会调用这个子类的超类中的方法。我的类RussiaPanel扩展了JPanel,JPanel扩展了JComponent。它会调用JComponet中的“public void paint(Graphic g)”方法,Panel上的东西就会被清除。更多细节可以参考API文档。

Hope to help you, and forgive my poor English.

希望能帮到你,并原谅我可怜的英语。

回答by Jeroen Vannevel

public void paint(Graphics g)

Paints the container. This forwards the paint to any lightweight components that are children of this container. If this method is reimplemented, super.paint(g)should be called so that lightweight components are properly rendered. If a child component is entirely clipped by the current clipping setting in g, paint()will not be forwarded to that child.

public void paint(Graphics g)

油漆容器。这会将绘制转发到作为此容器子项的任何轻量级组件。如果重新实现此方法,super.paint(g)则应调用该方法以便正确呈现轻量级组件。如果子组件被 g 中的当前裁剪设置完全裁剪,paint()则不会转发给该子组件。

Straight from the docs.

直接来自文档

回答by Rakesh KR

I guess its usage is like some other methods that you use super.method(). This is used to invoke the method in the super class. Basically it depends on your purpose and you decide how to use use it. In most case, we override the paint(Graphics g)in our subclass of the Component to fulfill our intention. If you call super.paint(g), it might call the method in the super class of this subclass.

我猜它的用法就像你使用的其他一些方法super.method()。这用于调用超类中的方法。基本上这取决于你的目的,你决定如何使用它。在大多数情况下,我们paint(Graphics g)在 Component 的子类中覆盖以实现我们的意图。如果你调用super.paint(g),它可能会调用这个子类的超类中的方法。

You should call super.paintComponent(g)if you want the super class to paint first (or last);

super.paintComponent(g)如果您希望超类首先(或最后)绘制,则应该调用;

calling super.paint(g)would cause nasty recursion in Swing.

调用super.paint(g)会导致 Swing 中令人讨厌的递归。