gpt4 book ai didi

java - JButton在 Action 中执行

转载 作者:行者123 更新时间:2023-12-02 11:10:54 26 4
gpt4 key购买 nike

if("switch".equals(action))
{
changeLunar();
count++;
if (count % 2 == 0)
{
lunarButton = new JButton(new ImageIcon("assets/sun.png"));
}
else
{
lunarButton = new JButton(new ImageIcon("assets/moon.png"));
}
}

因此,我试图将其放置在单击按钮的位置时,该按钮将更改为另一图像,并且再次单击该按钮时,它将返回第一图像。但是当我编译时说:
lunarButton can't be resolved to a variable

我是什么做的?

最佳答案

错误消息:找不到符号

When a Java program is being compiled the compiler creates a list of all the identifiers in use. If it can't find what an identifier refers to (e.g., there is no declaration statement for a variable) it cannot complete the compilation. This is what the cannot find symbol error message is saying, it doesn't have enough information to piece together what the Java code wants to execute.



请验证代码中声明的 lunarButton变量,并且 也必须可以访问

可能是您已在 changeLunar()方法中将其创建为无法在方法之外访问的局部变量,而是使用 实例变量或仅 changeLunar()方法返回值

您不必每次都创建一个新的 JButton,而只需使用 JButton#setIcon()方法更改其图标即可。

关于java - JButton在 Action 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23669598/

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