gpt4 book ai didi

java - 获取 jbutton 上 imageIcon 的描述

转载 作者:行者123 更新时间:2023-12-01 18:58:22 25 4
gpt4 key购买 nike

我有一个 JButton,可以根据情况选择设置 2 个不同的 imageIcons。

image1Url = getClass().getResource(filename1);
image2Url = getClass().getResource(filename2);
ImageIcon image1 = new ImageIcon( image1Url , "image1")
ImageIcon image2 = new ImageIcon( image2Url , "image2")

JButton button = new JButton;

//Either image1 or image2 is set to this button depending
//on whether the user clicks this button or whether the
//computer sets an image there. The point is that the programme
//then needs to respond depending upon what the image on the button is.
//I tried using...

if(button.getIcon() ==image1)
{
//respond appropriately.
}

然而这并没有奏效。有人可以向我解释为什么这不起作用以及是否有其他方法可以解决这个问题?我想我也许可以使用图像描述,但不确定如何实现这一点。谢谢

最佳答案

我不建议您使用==,因为您正在比较引用文献。当然,除非这就是您想要做的。

我想说最简单的方法是设置一个值(例如 boolean 值)来确定当前使用的ImageIcon,然后与之进行比较。

编辑抱歉,我实际上误解了这个问题。如果您想使用描述进行比较,只需执行以下操作:

String desc = ((ImageIcon)button.getIcon()).getDescription();
if(desc.equals(image1.getDescription())
{
//descriptions are the same
}

关于java - 获取 jbutton 上 imageIcon 的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13220271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com