【问题标题】:Unable to load image in JLabel无法在 JLabel 中加载图像
【发布时间】:2019-05-08 01:19:07
【问题描述】:

这里有这样的错误。

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;

【问题讨论】:

  • 这段代码的顺序是否正确?您似乎在使用 photoLabel 后实例化了它?当您调用.getWidth() 时,它应该抛出NullPointerException。还有getWidthgetHeight 返回什么,我猜它返回0,这就是你得到那个错误的原因。
  • 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到this Q&A 中看到的图像。例如。 This answer 热链接到嵌入在 this question 中的图像。 3) BTW - 如果标签没有文本或图标,其首选宽度和高度将为 0。

标签: java image swing awt


【解决方案1】:

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

 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2019-05-30
    • 2020-01-20
    • 2013-07-01
    • 2013-04-12
    相关资源
    最近更新 更多