【发布时间】:2013-05-17 12:44:22
【问题描述】:
我在我的 GUI 中更新图像时遇到问题。除了这些方法之外的所有内容都被正确编码,因为当我在 GUI 的初始化中编写代码(来自下面的“setImage”方法)时会出现图像。
我正在尝试获取单击按钮时更新图像的方法。但是,当单击按钮时,没有任何变化。方法调用正确地写入了 MouseListener。所有的 JLabels / Images 都被声明为私有的,所以我可以在方法中调用它们。
public void setImage(String path) {
//path = "imageName.png"
removeImageLabel();
try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
imageLabel = new JLabel(new ImageIcon(img));
imagePanel.add(imageLabel);
imagePanel.repaint();
}
public void removeImageLabel() { //removes the current image from the GUI
imagePanel.remove(imageLabel);
}
我还有其他方法可以根据按钮点击来设置元素,但这似乎不起作用。没有错误被抛出,但没有任何更新。怎么了?
注意:我在方法中添加了一个 println() 并调用它。
新功能: 我在创建 GUI 时添加了一个图像,它会显示出来,但在调用方法时它永远不会被删除。
【问题讨论】:
-
您是否尝试在您的
setImage方法中添加一些System.out.println()以查看它是否被调用?
标签: java image swing repaint imageicon