簡體   English   中英

為什么圖片不會在java中出現?

[英]why picture doesn't show up in swing in java?

我想在中心的邊框布局面板中顯示圖像。 我創建如下圖像面板,然后將其添加到Border-layout的中心。 我不知道為什么圖像沒有顯示,也沒有錯誤。 可能是什么原因,我該如何解決?

public class ImagePanel extends WebPanel {
private Image image;
private Image scaledImage;
private int imageWidth = 0;
private int imageHeight = 0;
//constructor
public ImagePanel() {
    super();
}
public void loadImage(String file) throws IOException {
    File filetest= new File("C:\\tmp\\axiuser\\Pictures\\CLA0014.png");
    image = ImageIO.read(filetest);//new File(file)
    imageWidth = image.getWidth(this);
    imageHeight = image.getHeight(this);
    setScaledImage();
}
//e.g., containing frame might call this from formComponentResized
public void scaleImage() {
    setScaledImage();
}
//override paintComponent
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if ( scaledImage != null ) {
        //System.out.println("ImagePanel paintComponent " + ++paintCount);
        g.drawImage(scaledImage, 0, 0, this);
    }
}
private void setScaledImage() {
    if ( image != null ) {
        //use floats so division below won't round
        float iw = imageWidth;
        float ih = imageHeight;
        float pw = this.getWidth();   //panel width
        float ph = this.getHeight();  //panel height
        if ( pw < iw || ph < ih ) {
            if ( (pw / ph) > (iw / ih) ) {
                iw = -1;
                ih = ph;
            } else {
                iw = pw;
                ih = -1;
            }
            //prevent errors if panel is 0 wide or high
            if (iw == 0) {
                iw = -1;
            }
            if (ih == 0) {
                ih = -1;
            }
            scaledImage = image.getScaledInstance(
                        new Float(iw).intValue(), new Float(ih).intValue(), Image.SCALE_DEFAULT);
        } else {
            scaledImage = image;
        }
    }
}

}

你從不打電話給你的方法

public void loadImage (String file) 

SSCCE:

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

public class ImagePanel extends JPanel {
    private Image image;
    private Image scaledImage;

    public void loadImage (String filename) throws IOException {
        image = ImageIO.read (new File (filename)); 
        setScaledImage ();
    }

    public void paintComponent (Graphics g) {
        super.paintComponent (g);
        if ( scaledImage != null) {
            // System.out.println ("ImagePanel paintComponent ");
            g.drawImage (scaledImage, 0, 0, this);
        }
    } 

    private void setScaledImage () {
        if (image != null) {
            //use floats so division below won't round
            int imageWidth = image.getWidth (this);
            int imageHeight = image.getHeight (this);
            float iw = imageWidth;
            float ih = imageHeight;
            float pw = this.getWidth ();   //panel width
            float ph = this.getHeight ();  //panel height
            if ( pw < iw || ph < ih) {
                if ( (pw / ph) > (iw / ih)) {
                    iw = -1;
                    ih = ph;
                } else {
                    iw = pw;
                    ih = -1;
                }
                //prevent errors if panel is 0 wide or high
                if (iw == 0) {
                    iw = -1;
                }
                if (ih == 0) {
                    ih = -1;
                }
                scaledImage = image.getScaledInstance (
                new Float (iw).intValue (), new Float (ih).intValue (), Image.SCALE_DEFAULT);
            } else {
                scaledImage = image;
            }
        }
    }   

    public static void main (String [] args) throws IOException {
        ImagePanel ip = new ImagePanel ();
        ip.loadImage ("./sample.png");
        JFrame jf = new JFrame ();
        jf.setLayout (new BorderLayout ());
        jf.add (ip, BorderLayout.CENTER);
        jf.setSize (400, 400); 
        jf.setLocation (150, 150);
        jf.setVisible (true);
        jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }
}

縮放不起作用,但在這里你有一些東西要開始。 (注意我重命名了圖像)。

只需覆蓋JPanel getPreferredSize() ,就像使用paintComponent(...)方法一樣。 讓它返回一些Dimension對象,如:

public Dimension getPreferredSize()
{
    return (new Dimension(300, 300));
}

這將允許您查看圖像。

也許沒有為*.png定義的Reader。您可以使用ImageIO.getReaderFormatNames()查看可用的圖像閱讀器列表

我認為你應該將面板設置為不透明:

setOpaque(false)

在不知道程序何時實際調用paintComponent()的情況下,我們無法分辨。 但是如果已經顯示GUI( setVisible之后調用loadImage方法則需要在面板上調用repaint()invalidate()以觸​​發組件的重繪(並以這種方式更新GUI)。

考慮使用JLabel來顯示圖像。

JLabel myLabel=new JLabel();
myLabel.setIcon(new ImageIcon("C:\\tmp\\axiuser\\Pictures\\CLA0014.png"));

暫無
暫無

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

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