簡體   English   中英

super.paint(g)的用途是什么?

[英]What is use of super.paint(g)?

有人可以解釋一下super.paint(g)的用法,其中g是Applets中的Graphics變量或awt或swing或Java中的。

我做過研究,發現它用於覆蓋但是這個覆蓋的用途是什么?

我是初學者。 如果可能的話,你可以用一個小example解釋paint(g)super.paint(g)之間的區別,或者請幫我解釋一下這段代碼?

/*
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);
    }
}

public void paint(Graphics g)

塗抹容器。 這會將paint轉發給任何屬於此容器子級的輕量級組件。 如果重新實現此方法,則應調用super.paint(g)以便正確呈現輕量級組件 如果子組件被g中的當前剪切設置完全剪切,則paint()將不會轉發給該子組件。

直接來自文檔

  1. 首先,我在自己的paint方法中添加super.paint(g)。

當它重新繪制時,之前繪制的東西被清除。

對不起,我無法發布超過2個鏈接的聲譽。

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

    刪除super.paint(g)

你可以看到面板上繪制的東西仍然存在。

當您添加super.paint(g)時,在您的方法中它將調用此子類的超類中的方法。我的類RussiaPanel擴展JPanel,JPanel擴展JComponent。 它將在JComponet中調用“public void paint(Graphic g)”方法,並且將清除Panel上的內容。有關更多詳細信息,請參閱API文檔。

希望能幫到你,並原諒我可憐的英語。

我猜它的用法就像你使用super.method()其他一些方法。 這用於調用超類中的方法。 基本上它取決於你的目的,你決定如何使用它。 在大多數情況下,我們覆蓋Component的子類中的paint(Graphics g)來實現我們的意圖。 如果你調用super.paint(g) ,它可能會調用這個子類的超類中的方法。

你應該調用super.paintComponent(g)如果你想讓超類先畫(或最后一個);

調用super.paint(g)會在Swing中引起令人討厭的遞歸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM