簡體   English   中英

JFrame或Jpanel中的repaint()方法?

[英]repaint() method in JFrame or Jpanel?

我是Java新手,剛剛開始學習GUI組件。 因此,我正在閱讀Headfirst Java,並且有這段代碼解釋了JPanel paintcomponent方法。 只要用戶單擊“更改顏色”按鈕,就可以更改橢圓的顏色。 --

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleGui3C implements ActionListener {

    JFrame frame;

    public static void main(String[] args) {
        SimpleGui3C gui = new SimpleGui3C();
        gui.go();
    }

    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Change colors");
        button.addActionListener(this);

        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, button);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.setSize(500,500);
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        frame.repaint();
    }
}

MyDrawPanel類 -

import javax.swing.*;
import java.awt.*;

class MyDrawPanel extends JPanel {

    public void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

//      GradientPaint gradient = new GradientPaint(70,70,Color.blue,150,150,Color.orange);

        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue = (int) (Math.random() * 255);
        Color startColor = new Color(red, green, blue);

        red = (int) (Math.random() * 255);
        green = (int) (Math.random() * 255);
        blue = (int) (Math.random() * 255);
        Color endColor = new Color(red, green, blue);

        GradientPaint gradient = new GradientPaint(70,70,startColor,150,150,endColor);

        g2d.setPaint(gradient);
        g2d.fillOval(70,70,100,100);
    }
}

除了調用repaint()方法的那一部分之外,我從本示例中獲得了所有內容。

在書中,提到了僅重寫JPanelpaintcomponent方法,並且當您調用repaint() ,它將調用paintcomponent來呈現內容(因為我們不直接調用paintcomponent )。

現在,我通過擴展JPanel創建了MyDrawPanel類,並重寫了它的paintComponent方法。 現在,當我們將drawPanel小部件放在框架上,然后單擊鼠標時,將repint()方法。 但是,使用frame - JFrame的對象調用了此repaint()方法。 根據我對Java的基本理解,如果我要使用對象調用方法,則該方法必須在該類或某些Super類中。 我看不到在frame上調用repaint()將如何觸發MyDrawPanel paint組件。 我做了一些挖掘,看看JFrameJPanel共享相同的層次結構,這就是我發現的內容:

object -> component (具有重新繪制方法)-> container (具有paintcomponent方法)->

        `jcomponent` --> `jpanel`

        `window` --> `frame` --> `jframe`

因此,我認為既然JFrameJPanel都具有paintComponentrepaint方法,也許我可以擴展Jframe來創建MyDrawPanel而不是Jpanel ,它應該可以正常工作。 但事實並非如此。 它可以正常編譯,但會拋出運行時異常。

真令人沮喪。 可能是我沒有獲得GUI概念,或者我的基礎知識仍然很薄弱。 請讓我知道我是否在這里錯過了一些非常愚蠢的事情。

讓我們開始在執行任何自定義繪畫之前, paintComponent應該真正調用super.paintComponent

然后繼續前進,您真的應該閱讀AWT和Swing中的 繪畫執行自定義繪畫 ,以更好地了解Swing中的繪畫工作原理。 repaint不調用paintComponent直接,它要求RepaintManager來安排paint事件隊列上的,這一點,在未來的某個時刻的事件,通過事件調度線程處理,然后處理paint基礎上的需求事件容器

所以我認為,既然JFrame和JComponent都獲得paintComponent

不,他們沒有。 JFrame (及其父類)沒有paintComponent方法

可能是我可以擴展Jframe以創建MyDrawPanel而不是Jpanel,並且應該可以正常工作”

不,除了JFrame沒有paintComponent之外,您不應該這樣做,這可能會干擾您可能嘗試在JFrame上執行的任何自定義繪制,因此您應該繼續按JFrame構造代碼

暫無
暫無

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

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