JLayeredPane图像未显示

时间:2018-05-21 21:23:48

标签: java image swing jpanel jlayeredpane

我有一个图像,我想要的是将此图像打包到JLabel中并将其添加到我的JLayeredPane以稍后对多个图像进行分层。 它使用一个没有JLayeredPane的Image,但是当使用JLayeredPane时我只能看到我的边框。 我必须对多个图像进行分层,以便ypu可以配置比萨并查看您的自定义比萨饼。

package view;

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

/**
 * ImagePanel used to scale and display an image
 */

public class ImagePanel extends JPanel {
private JLayeredPane layeredPane;

/**
 * Setup an ImagePanel, get the image, scale it and set it as a label to display it
 */
public ImagePanel(){

    layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(new Dimension(300, 300));
    layeredPane.setBorder(BorderFactory.createTitledBorder("Your Pizza"));
    ImageIcon icon = new ImageIcon(getClass().getResource("/images/pizzaboden.png"));
    JLabel imgLabel = new JLabel(icon);
    layeredPane.add(imgLabel);
    add(layeredPane);

}
}

阅读图像是有效的,这是我之前阅读它的方式,它运行良好:

我的数据字段

private ImageIcon imageIcon;
private ImageIcon scaledImageIcon;
private JLabel imageLabel;
private Image image;
private Image scaledImage;

我的构造函数

imageIcon = new ImageIcon(getClass().getResource("/images/pizzaboden.png"));
    image = imageIcon.getImage();
    scaledImage = image.getScaledInstance(280, 280, Image.SCALE_SMOOTH);
    scaledImageIcon = new ImageIcon(scaledImage);
    imageLabel = new JLabel(scaledImageIcon);
    add(imageLabel);

What I get

1 个答案:

答案 0 :(得分:1)

当您向JLayeredPane添加内容时,您需要根据JLayeredPane tutorial指定在X / Y维度中添加组件的位置以及分层维度中的位置,并且您不需要&#39这样做。您还需要为JLabel提供一个大小,因为JLayeredPane不会考虑它的首选大小。像

这样的东西
imgLabel.setSize(imgLabel.getPreferredSize());
imgLabel.setPosition(0, 0);
layeredPane.add(imgLabel, JLayeredPane.DEFAULT_LAYER);

可能就足够了。

另外,您是否独立于当前程序进行了测试,以确保正确读取图像?此外,您使用JPanel添加到FlowLayout - 您确定要执行此操作吗?也许最好是给你的ImagePanel一个BorderLayout。

请注意,如果我在做这样的事情,在Swing GUI中显示多个重叠图像,我就不会使用JLayeredPane,而是在单个paintComponent(Graphics g)方法中绘制图像(BufferedImages)绘制JPanel,并在需要时从GUI添加和删除图像,然后在绘图JPanel上调用repaint()

例如,如果你给绘图JPanel一个像这样的图像的ArrayList:

private List<Image> images = new ArrayList<>();

您可以通过遍历paintComponent方法中的列表来绘制此列表所包含的任何图像:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Image image : images) {
        g.drawImage(image, 0, 0, this);
    }
}

您可以轻松添加和删除图像:

public void clearImages() {
    images.clear();
    repaint();
}

public void removeImage(Image image) {
    images.remove(image);
    repaint();
}

public void addImage(Image image) {
    images.add(image);
    repaint();
}

例如,这里是您可以运行的概念验证代码,它演示了我的意思:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MultipleImages extends JPanel {
    private static final String BULLS_EYE_PATH = "https://upload.wikimedia.org/wikipedia/"
            + "commons/thumb/3/36/Roundel_of_the_Serbian_Air_Force_1915.svg/"
            + "300px-Roundel_of_the_Serbian_Air_Force_1915.svg.png";
    private static final String CHINESE_CHAR_PATH = "https://upload.wikimedia.org/"
            + "wikipedia/commons/1/1a/%E9%9D%91-red.png";
    private static final String HOCKY_PATH = "https://upload.wikimedia.org/wikipedia/"
            + "commons/thumb/e/eb/Ice_hockey_pictogram.svg/"
            + "300px-Ice_hockey_pictogram.svg.png";
    private static final String[] LABELS = {"Bulls Eye", "Chinese Character", "Hockey"};
    private static final String[] IMAGE_PATHS = {BULLS_EYE_PATH, CHINESE_CHAR_PATH, HOCKY_PATH};
    private Map<String, Image> imageMap = new HashMap<>();
    private List<JCheckBox> checkBoxes = new ArrayList<>();
    private ImagePanel imagePanel = new ImagePanel();

    public MultipleImages() throws IOException {
        for (int i = 0; i < IMAGE_PATHS.length; i++) {
            URL imgUrl = new URL(IMAGE_PATHS[i]);
            BufferedImage img = ImageIO.read(imgUrl);
            imageMap.put(LABELS[i], img);
        }

        ActionListener checkBoxListener = e -> {
            imagePanel.clearImages();
            for (JCheckBox checkBox : checkBoxes) {
                if (checkBox.isSelected()) {
                    String label = checkBox.getActionCommand();
                    imagePanel.addImage(imageMap.get(label));
                }
            }
            imagePanel.repaint();
        };
        JPanel checkBoxPanel = new JPanel(new GridLayout(0, 1));
        for (String label : LABELS) {
            JCheckBox checkBox = new JCheckBox(label);
            checkBox.setActionCommand(label);
            checkBoxes.add(checkBox);
            checkBoxPanel.add(checkBox);
            checkBox.addActionListener(checkBoxListener);
        }
        JPanel rightPanel = new JPanel(new BorderLayout());
        rightPanel.add(checkBoxPanel, BorderLayout.PAGE_START);

        setLayout(new BorderLayout());
        add(rightPanel, BorderLayout.LINE_START);
        add(imagePanel);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Multiple Images");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            frame.getContentPane().add(new MultipleImages());
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

public class ImagePanel extends JPanel {
    private static final int PREF_W = 300;
    private static final int PREF_H = PREF_W;
    private static final Color BACKGROUND = Color.WHITE;
    private List<Image> images = new ArrayList<>();

    public ImagePanel() {
        setBackground(BACKGROUND);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Image image : images) {
            g.drawImage(image, 0, 0, this);
        }
    }

    public void clearImages() {
        images.clear();
        repaint();
    }

    public void removeImage(Image image) {
        images.remove(image);
        repaint();
    }

    public void addImage(Image image) {
        images.add(image);
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }
}