加载图像到JPanel无法正常工作

时间:2015-04-03 11:00:09

标签: java swing jpanel

我试图将文件从文件中加载到JPanel两天。我不能!

我使用JLabelIcon并且它已加载好了,但我需要直接将图片加载到JPanel,这是不可能的吗?

因为几乎我看到很多这样的相关问题,很多人都建议问题的人将图像加载到标签中!

这是代码:

public class ReadingImage extends JPanel  {
JPanel panel;
JFrame frame; 
JPanel secPanel;
private BufferedImage img;


public ReadingImage(String path){
    frame = new JFrame();
    frame.setVisible(true);
    frame.setLocation(300, 300);
    frame.setSize(300, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    secPanel  = new JPanel();
    secPanel.setVisible(true);
    //secPanel.setLayout(new FlowLayout());

    secPanel.repaint();

    frame.getContentPane().add(secPanel);

    try{
        FileImageInputStream fi = new FileImageInputStream(new File(path));
        //System.out.println(path);
        img = ImageIO.read(fi);
        this.repaint();
    }
    catch (IOException io ){ io.printStackTrace();}
}

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

它没有抛出任何异常,但它没有在JPanel中显示图像!

我多次调整代码..

任何帮助:)

谢谢,

3 个答案:

答案 0 :(得分:2)

通过扩展JPanel和使用其他JPanel导致的经典混淆。将frame.getContentPane().add(secPanel)替换为frame.add(this , BORDERLAYOUT.CENTER),一切都应该正常。

答案 1 :(得分:2)

您在super.paintComponents(g);中呼叫paintComponent,而不是最后s,这最终会导致StackOverflowException,但实际上并非如此将ReadingImage JPanel添加到任何内容中,因此它实际上从未画过

这意味着对secPane

的任何一点似乎都没有任何意义。

您还应该避免在其他组件中创建框架,尤其是在构造函数中,它为组件提供了非常有限的用例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ReadingImage extends JPanel {

    private BufferedImage img;

    public ReadingImage(String path) {
        try {
            FileImageInputStream fi = new FileImageInputStream(new File(path));
            img = ImageIO.read(fi);
            this.repaint();
        } catch (IOException io) {
            io.printStackTrace();
        }
    }

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

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame();
                frame.add(new ReadingImage("Your image"));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocation(300, 300);
                frame.setSize(300, 500);
                frame.setVisible(true);

            }
        });
    }

}

我不确定您要实现的目标,但我也建议您覆盖ReadingImage的{​​{1}}方法并返回图片大小,这使布局更容易

答案 2 :(得分:0)

首先在图片图标中加载图片,然后将对象称为' pic'。

panel1.add(new JLabel(pic));

添加并将panel1设置为可见。