0

我有一个卡片板,我必须在其中找到匹配项。我有两个变量buttonAbuttonB跟踪单击的方块。

当它们相等时,我可以通过简单地添加以下代码将它们从板上删除:

cards[buttonA].setVisible(false);
cards[buttonB].setVisible(false);

找到匹配项后,如何在所有按钮上放置相同的图像?我尝试了以下操作,但它并没有更改图像,而是在按钮上留下相同的图像

cards[buttonA].setIcon(new ImageIcon("myPic.png");
4

2 回答 2

2

您可能需要使用:

new ImageIcon(getClass().getResource("/path/to/myPic.png"));

此资源在classpath. (请记住,如果使用 IDE,您需要确保将 PNG 资源复制到输出目录。例如,在 IDEA 中,这是在编译器设置菜单中实现的)

编辑:我永远不记得路径是否以 a 开头/

于 2009-03-09T22:33:48.693 回答
0

You can have a reference to the ImageIcon if you want to share it across buttons (instead of loading it everytime). To me your code should work. Maybe you can remove the current icon (using setIcon(null)) and then set it.

于 2009-03-10T13:39:31.793 回答