繁体   English   中英

无法在 JLabel 中加载图像

[英]Unable to load image in JLabel

这里有这样一个错误。

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
    Width (0) and height (0) must be non-zero

如何解决?

ResultSet resultSet = connection.query("select url_image from "+name+" where id = "+List.get(i));
java.sql.Blob blob = null;

try {
    while (resultSet.next()) {
        blob = resultSet.getBlob("url_image");
    }
} catch (SQLException e4) {
    e4.printStackTrace();
}
BufferedImage destImage = null;
try {
    destImage = ImageIO.read(blob.getBinaryStream());
} catch (IOException e1) {
    e1.printStackTrace();
} catch (SQLException e1) {
    e1.printStackTrace();
}
Image scaledImage = destImage.getScaledInstance(photoLabel.getWidth(),photoLabel.getHeight(), Image.SCALE_DEFAULT); // error
ImageIcon imgIc = new ImageIcon(scaledImage);
photoLabel.setIcon(imgIc);

 photoLabel = new javax.swing.JLabel();
 .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(photoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
private javax.swing.JLabel photoLabel;

此代码可能会为您提供正确的输出。

 ResultSet resultSet = connection.query("select url_image from "+name+" where id = "+List.get(i));
                    java.sql.Blob blob = null;

                    try {
                        while (resultSet.next()) {
                            blob = resultSet.getBlob("url_image");
                        }
                    } catch (SQLException e4) {
                        e4.printStackTrace();
                    }
                    BufferedImage destImage = null;
                    try {
                        destImage = ImageIO.read(blob.getBinaryStream());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                    //Image scaledImage = destImage.getScaledInstance(photoLabel.getWidth(),photoLabel.getHeight(), Image.SCALE_DEFAULT); // error
                    //ImageIcon imgIc = new ImageIcon(scaledImage);
                    //photoLabel.setIcon(imgIc);
                    photoLabel.setIcon(new ImageIcon(destImage ));


     photoLabel = new javax.swing.JLabel();
     .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(photoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    private javax.swing.JLabel photoLabel;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM