问题描述
我正在制作一个简单的游戏.单击JButton时,应该更改JLabel的ImageIcon.我该怎么做?
I am making a simple game right now. When a JButton is clicked, the ImageIcon of a JLabel is supposed to change. How can I accomplish this?
推荐答案
简单,只需通过按钮的动作侦听器在JLabel上快速运行setIcon()即可.
Simple, just quickly run setIcon() on the JLabel via the action listener of the button.
一个例子:
if ("burp".equals(evt.getActionCommand())) {
charLabel.setIcon(burpIcon);
Sounds.burp();
}
正如MadProgrammer所提到的那样,您看到setIcon()的实时更改的任何问题都可能需要检查一下您实际上是如何设计该类的,而不是尝试一种变通的解决方法来强制ImageIcon像它那样起作用.应该具有正确的设置.
As mentioned by MadProgrammer, any issue you have seeing real-time changes to setIcon() will probably warrant a look at how you've actually designed the class, rather than attempting a hacky workaround to force the ImageIcon to function as it should with the proper setup.
这篇关于以编程方式自动更改ImageIcon [Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!