java - 如何从 JButton/JLabel 更新 ImageIcon

标签 java image swing jlabel imageicon

关闭模式对话框后,我在更新(或刷新)按钮中的图标时遇到问题。该图像基本上被 JDialog 的某些操作覆盖。

这是我的代码:

conf = new Configurar(this, true,control);           
conf.setVisible(true); // Open dialog
System.out.println("Cerrado"); // Check if is closed (debug)
String logo =(String)config.get("logo"); // get path from image
File newIcon =new File(logo); // Desesperate try
ImageIcon img = new ImageIcon(newIcon.getAbsolutePath()); 
btn_main_image.setIcon(img);
this.update(btn_main_image.getGraphics());
btn_main_image.updateUI(); // First Try
this.repaint(); // Second Try

第一次工作正常,但当我打开对话框并更改图像时,图像保持不变。

最佳答案

conf = new Configurar(this, true,control);           
conf.setVisible(true); // Some kind of file chooser ??
File newIcon =new File(logo);
if (newIcon.exists()) {
   ImageIcon img = new ImageIcon(newIcon.getAbsolutePath()); 
   btn_main_image.setIcon(img);
   //this.update(btn_main_image.getGraphics()); // WHAT IS THIS?!?!?!
   //btn_main_image.updateUI(); // NO NO NO, this has nothing to do with refreshing the graphics, it's L&F stuff
   btn_main_image.invalidate();
   // Use this ONLY if invalidate doesn't work...
   btn_main_image.revalidate();
   btn_main_image.repaint();
}

关于java - 如何从 JButton/JLabel 更新 ImageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12964492/

相关文章:

image - hadoop文件系统(HDFS)可以存储视频类型的文件吗?

java - 切换顶级窗口时如何使主机保持在对话框后面?

按下向上和向下箭头时,Java FX 无法识别 KeyCode.UP 或 KeyCode.Down (MacBook Pro 18 - Mojave 10.14.5)

java - 如何计算没有整数 bigdecimal 指数的幂?

java - BIT(1) HIbernate JPA 映射

java - 切换焦点后模态对话框隐藏在主框架后面

java - 在执行时将 JInternalFrame 添加到 JDesktopPane

java - 在三星设备中加载大图像时出现问题(J2me游戏)

python - 当Microsoft Edge进行操作时,如何使用python从PDF渲染图像?

html - 图片不显示问题