更改按钮的图标,但不想设置图像的位置

时间:2018-07-21 16:04:54

标签: java image swing embedded-resource imageicon

我想更改按钮的图像(我知道该怎么做)。还有什么其他方法可以添加图像而不提供图像的位置,因为如果应用程序的位置发生变化,它将不会显示图像。所以我想知道是否还有其他可能的方法?

编辑:

jButton1.setIcon(new ImageIcon("Address of the image"));

我不想添加此地址,因为它可能会更改位置。

if (status.equals("update"))
jButton1.setIcon(new ImageIcon("C:/Users/laptop/Documents/NetbeansProjects/Physics/src/update.png")); 

这是用于在状态为update时从add.png更改image update.png的代码。我不想写它的地址,因为如果jar文件的位置改变了,它就不会显示图像。该项目将离线运行,因此无需在线建议

1 个答案:

答案 0 :(得分:0)

只需将图像文件添加到项目目录中,仅提供文件的名称和格式
即在您的示例中:
spark_available_versions() spark 1 1.6.3 2 1.6.2 3 1.6.1 4 1.6.0 5 2.0.0 6 2.0.1 7 2.0.2 8 2.1.0 9 2.1.1 10 2.2.0 11 2.2.1 12 2.3.0 # Set spark connection sc <- spark_connect(master = "local", version = "2.3.1") #It does not work Error in spark_install_find(version, hadoop_version, latest = FALSE, hint = TRUE) : Spark version not installed. To install, use spark_install(version = "2.3.1") spark_install(version = "2.3.1") Error in spark_install_find(version, hadoop_version, installed_only = FALSE, : Spark version not available. Find available versions, using spark_available_versions() sc <- spark_connect(master = "local") #it works perfectly

相关问题