无法在JLabel中加载图像

时间:2018-12-06 10:52:57

标签: java image swing awt

这是一个错误。

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;

1 个答案:

答案 0 :(得分:0)

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

 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;