簡體   English   中英

在JPanel上繪制新事物

[英]Paint new things on JPanel

好的,所以我有一個JPanel重寫了paintComponent方法。

很簡單,看起來像這樣:

public class Panel1 extends JPanel {

   public void paintComponent (Graphics g) {
      super.paintComponent (g);
      g.fillOval (0, 0, getWidth (), getHeight ());
   }
}

現在,我將此JPanel作為屬性添加到另一個JPanel類,例如:

public class Panel2 extends JPanel {

   Panel1 panel;

   public Panel2 (Panel1 panel) {
      this.panel = panel;
   }

   protected void paintComponent (Graphics g) {
      super.paintComponent (g);
      panel.paint (g); //This isn't working.
//          panel.paintComponent (g); //Tried this too

      g.drawOval (100, 100, getWidth () - 200, getHeight () - 200);
   }
}

我想要的是將Panel2繪畫成與Panel1完全一樣(無需對其進行硬編碼),並可能添加其他內容(例如三角形或某物,我不知道)。

這有可能嗎? 我調查了一下,但沒有找到任何方法。 在此先感謝您的幫助!!

MAIN,以幫助:

public class Main {

   public static void main (String[] args) {
      JFrame frame = new JFrame ();
      frame.setSize (500, 500);

      frame.add (new Panel2 (new Panel1 ()));

      frame.setVisible (true);
   }
}

編輯:以防萬一,我不想繼承。 這就是為什么我將其添加為屬性的原因,但是如果還有其他方法,請讓我現在進行。

您可以嘗試將Panel1 paintComponent公開,然后在Panel2 paintComponent中調用它:

protected void paintComponent (Graphics g) {
    panel1.paintComponent(g);
}

您還可以在Panel1類中創建一個方法來為您處理繪畫

public void yourPainting(Graphics g){
    //whatever you want to paint
}

然后在Panel1Panel2paintComponent方法中調用此方法

它不按問題中的方式工作的原因是Panel1的大小為0x0。 要獲得合理的尺寸,請從getPreferredSize()返回尺寸,然后將面板尺寸設置為首選尺寸。

在此處輸入圖片說明

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

public class PaintUnrealized {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                JFrame f = new JFrame("Paint Unrealized Component");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(new Panel2(new Panel1()));
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class Panel1 extends JPanel {

    public Panel1() {
        setBackground(Color.RED);
        setSize(getPreferredSize());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillOval(0, 0, getWidth(), getHeight());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }
}

class Panel2 extends JPanel {

    Panel1 panel;

    public Panel2(Panel1 panel) {
        this.panel = panel;
        setBackground(Color.YELLOW);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        panel.paintComponent(g); // This works

        int pad = 25;
        g.drawOval(pad, pad, getWidth()-(2*pad), getHeight()-(2*pad));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 300);
    }
}
public class Panel2 extends JPanel {
    private Panel1 panel1;

    public Panel2 (Panel1 panel) {
        this.panel1 = panel;
    }

    protected void paintComponent (Graphics g) {
         panel1.paint(g);
    }  
}

我相信應該可以

暫無
暫無

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

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