关闭模式对话框后,我在更新(或刷新)按钮中的图标时遇到问题。该图像基本上被 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/