簡體   English   中英

一個擴展JPanel的類,我想在具有BorderLayout的JFrame中添加CENTER屬性

[英]A class that extends JPanel, that i want to add with CENTER proprity in my JFrame that is a BorderLayout

我有一堂課叫panneau:

public class Panneau extends JPanel { 

    public void paintComponent(Graphics g){
        try {
            Image img = ImageIO.read(new File("src/disantiammo/image/image.png"));
            g.drawImage(img, 0, 0, this);
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
}
//for now it is not doing anything but it will...

在JFrame中,我有一些帶有其他元素的JPanels。

JFrame的布局是BorderLayout

問題是此鍋必須是“ java.awt.BorderLayout.CENTER”,我只能以這種方式放置它:

  this.setContentPane(new Panneau());

如何將其放置在“ CENTER”中?

我嘗試過類似的事情

this.getContentPane().add(new Panneau(), java.awt.BorderLayout.CENTER);

似乎無濟於事,我唯一能做的就是放置“ panneau”而不是JFrame內部的內容。 編輯:

這聽起來很簡單,但是我不明白為什么我不能這樣做,因為我一直堅持這樣做。

我有jDialog,它在BorderLayout中,在“南”,“北”和“中心”中,我有帶有元素的jPanel(“中心”的jPanel中沒有任何內容。“中心” jPanel稱為Map。

我是這樣的事情:主要

  Graphics t = Map.getGraphics();
    paintComponent(t);

不主要。

public void paintComponent(Graphics g){
 super.paintComponents(g);
 g.drawLine(0, 0, 50, 150);
}

我什么都畫不出來。

我發現使組件完全居中的最簡單方法是為容器提供GridBagLayout,然后將單個組件添加到該容器,但不使用任何約束。

附帶問題:您的代碼以繪畫方法讀取圖像- 絕對不要這樣做。 而是一次讀取圖像並將其存儲在變量中。 然后在繪畫方法中顯示它。 另外,請務必在您的替代中調用super的繪畫方法。

例如,

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class CenteredImage extends JPanel {
    private static final String IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/"
            + "thumb/f/f8/Portrait_d%27une_Femme_%C3%A0_sa_Toilette%2C_by_Titian%2C_"
            + "from_C2RMF_retouched.jpg/300px-Portrait_d%27une_Femme_%C3%A0_sa_"
            + "Toilette%2C_by_Titian%2C_from_C2RMF_retouched.jpg";
    private BufferedImage img;

    public CenteredImage(BufferedImage img) {
        this.img = img;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension prefSize = super.getPreferredSize();
        if (isPreferredSizeSet() || img == null) {
            return super.getPreferredSize();
        }
        return new Dimension(img.getWidth(), img.getHeight());
    }


    private static void createAndShowGui() {
        BufferedImage img = null;
        try {
            URL imgUrl = new URL(IMG_PATH);
            img = ImageIO.read(imgUrl);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        CenteredImage mainPanel = new CenteredImage(img);

        JFrame frame = new JFrame("CenteredImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(800, 650));
        frame.getContentPane().setLayout(new GridBagLayout());
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

編輯:如注釋中所述使用包裝JPanel:

您在評論中陳述:

謝謝您的幫助,但是我的意思是:在borderLayout中有一個JFrame / JDialog,我想在其中放“ panneau”到它的中心位置,也許我不知道該怎么說。在“南”,“北”等元素中...

然后將生成圖像的JPanel包裝到另一個使用GridBagLayout的JPanel中,並將包裝JPanel添加到JFrame BorderLayout.CENTER

private static void createAndShowGui() {
    BufferedImage img = null;
    try {
        URL imgUrl = new URL(IMG_PATH);
        img = ImageIO.read(imgUrl);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    CenteredImage centeredImagePanel = new CenteredImage(img);

    JPanel wrapperPanel = new JPanel(new GridBagLayout());
    wrapperPanel.add(centeredImagePanel);

    JFrame frame = new JFrame("CenteredImage");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(800, 650));
    // frame.getContentPane().setLayout(new GridBagLayout());
    frame.getContentPane().add(wrapperPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> createAndShowGui());
}

您的核心問題是(很可能是)您的面板沒有向布局管理器提供任何尺寸提示,以便允許其決定所需的尺寸。

為了實現這一點,您需要重寫面板的getPreferredSize方法以返回組件想要的大小。

這帶來了其他一些問題:

  1. 不要在paintComponent加載圖像,這很費時間,並且可能會使您的UI凍結。 另外, paintComponent被調用很多,因此您應該避免在其中花費任何時間來執行操作
  2. 您不應在代碼中的任何地方引用src 在這種情況下,您將需要使用Class#getResource獲得對圖像的引用以將其加載
  3. 當然,您應該調用super.paintComponent

例如

public class Panneau extends JPanel { 

    private BufferedImage img;

    public Panneau() throws IOException {
        img = ImageIO.read(getClass().getResource("/disantiammo/image/image.png")); 
    }

    public void getPrefferedSize() {
        return img == null ? new Dimension(0, 0) : new Dimension(img.getWidth(), img.getHeight());
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, this);
        }
    }
}

PS:我沒有嘗試將圖像放在容器的中央,但是如果布局管理器尊重組件的大小,則不會有什么不同

暫無
暫無

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

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