我正在开发一个程序,它将图像文件夹读取到 JList 中,并且在 JList 中选择的图片将被绘制到 JPanel 中。我有两个类:ImageViewerPanel
,它创建面板来显示所选图片。然后我ImageViewerUI
将绘制 JList,并将ImageViewerPanel
其添加到ImageViewerUI
类中。这是该ImageViewerPanel
课程的相关代码。
public ImageViewerPanel() {
initComponents();
}
public void setImage(BufferedImage image) {
this.image = image;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (scaled == false) {
g.drawImage(image, 0, 0, null);
}else if(scaled == true) {
g.drawImage(image, 0, 0, 80, 80, null);
}
ImageViewerUI
这是使用图像刷新面板的类中的代码:
ImageViewerPanel imagePanel = new ImageViewerPanel();
BufferedImage displayedImage;
BufferedImage originalImage;
public ImageViewerUI() {
initComponents();
loadListWithImageFilenames();
updateImagePanel();
updateThumbnailImagePanel();
}
public final void updateImagePanel() {
try {
String currFile = (String) ("Images/" + imageList.getSelectedValue());
displayedImage = ImageIO.read(new File(currFile));
imagePanel.setImage(displayedImage);
} catch (IOException ex) {
Logger.getLogger(ImageViewerUI.class.getName()).log(Level.SEVERE, null, ex);
}
public final void updateThumbnailImagePanel() {
try {
String currFile = (String) ("Images/" + imageList.getSelectedValue());
originalImage = ImageIO.read(new File(currFile));
imagePanel.setScaled(true);
imagePanel.setImage(originalImage);
imageViewerPanel1.add(imagePanel);
imagePanel.repaint();
} catch (IOException ex) {
Logger.getLogger(ImageViewerUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
我遇到的问题是图像没有显示在面板中。有谁知道为什么?