【发布时间】: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。还有getWidth和getHeight返回什么,我猜它返回0,这就是你得到那个错误的原因。 -
1) 为了尽快获得更好的帮助,edit 添加minimal reproducible example 或Short, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到this Q&A 中看到的图像。例如。 This answer 热链接到嵌入在 this question 中的图像。 3) BTW - 如果标签没有文本或图标,其首选宽度和高度将为 0。