【问题标题】:why picture doesn't show up in swing in java?为什么图片没有出现在java的swing中?
【发布时间】:2012-03-09 12:58:09
【问题描述】:

我想在中心的边框布局面板中显示图像。 我如下创建图像面板,然后将其添加到边框布局的中心。 我不知道为什么图像不显示,也没有错误。 可能是什么原因,我该如何解决?

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;
        }
    }
}

}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 在下面查看我的答案,用 JPEG 文件尝试一下,看看它是否有效。
  • 你的 ImagePanel 的尺寸是多少?如果您使用的是布局管理器,您还应该将 ImagePanel 的 preferredSize 设置为您的 scaledImage 的大小,否则,ImagePanel 的大小将为 0x0,您将永远看不到您的图像。

标签: java swing png image


【解决方案1】:

只需覆盖 JPanelgetPreferredSize(),就像使用 paintComponent(...) 方法一样。让它返回一些Dimension 对象,类似于:

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

这将允许您查看您的图像。

【讨论】:

    【解决方案2】:

    你永远不会调用你的方法

    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);
        }
    }
    

    缩放功能尚无法使用,但您可以从这里开始。 (请注意,我重命名了图像)。

    【讨论】:

      【解决方案3】:

      可能没有为*.png 定义阅读器。您可以使用ImageIO.getReaderFormatNames() 检查可用的图像阅读器列表

      【讨论】:

      • 应该有:GIF、JPEG、PNG 作为 javax.imageio RI 的一部分。
      【解决方案4】:

      我认为您应该将面板设置为不透明:

      setOpaque(false)
      

      【讨论】:

        【解决方案5】:

        如果不知道您的程序何时实际调用paintComponent(),则无法判断。但是,如果您调用 loadImage 方法之后,您已经显示了 GUI (setVisible),您需要在面板上调用 repaint()invalidate() 以触发重绘组件(并以这种方式更新 GUI)。

        【讨论】:

          【解决方案6】:

          考虑使用JLabel 来显示图像。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-13
            • 2022-12-11
            • 2021-09-09
            • 2022-11-28
            • 2011-11-19
            相关资源
            最近更新 更多