Java 未在Swing中更新图像
我在更新GUI中的图像时遇到问题。除了这些方法之外的所有方法都是正确编码的,因为当我在GUI的初始化中编写代码(来自下面的“setImage”方法)时,图像会出现 我正在尝试在单击按钮时获取更新图像的方法。但是,单击按钮时,没有任何更改。方法调用正确写入MouseListener中。所有jlabel/Images都声明为private,因此我可以在方法中调用它们Java 未在Swing中更新图像,java,image,swing,repaint,imageicon,Java,Image,Swing,Repaint,Imageicon,我在更新GUI中的图像时遇到问题。除了这些方法之外的所有方法都是正确编码的,因为当我在GUI的初始化中编写代码(来自下面的“setImage”方法)时,图像会出现 我正在尝试在单击按钮时获取更新图像的方法。但是,单击按钮时,没有任何更改。方法调用正确写入MouseListener中。所有jlabel/Images都声明为private,因此我可以在方法中调用它们 public void setImage(String path) { //path = "imageName.png"
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的创建中添加了一个图像,它显示了,但是在调用方法时它从不被移除。<> P>调用<代码>验证()>代码>而不是<代码> RePrimeTo(< /代码>
< P>),而不是替换整个图像组件,考虑在适当的位置更新组件的映像,如图所示,或者更新标签的图标,如图所示。是否尝试在setImage
方法中添加一些System.out.println()
,以查看是否调用了它?