问题描述
我正在使用JFreeChart,我通过实现 XYToolTipGenerator
界面来自定义图表的工具提示。
由于 generateToolTip()
方法应该返回一个String,这就是我在工具提示中显示图像的方法:
I'm using JFreeChart where I customized the chart's tooltip by implementing the XYToolTipGenerator
interface.As the generateToolTip()
method is supposed to return a String, here is what I did to show images in the tooltip:
@Override
public String generateToolTip(XYDataset dataset, int series, int item) {
(...)
return "<html><body><img src=\"file:resources/img/image.png\"></body></html>";
}
虽然直接从我的IDE(Eclipse)执行时这很有效,但是从打包的JAR文件执行时它显然会失败(image.png也在JAR文件中)。
While this works perfectly when executing directly from my IDE (Eclipse), it obviously fails when being executed from a packaged JAR file (the image.png is also in the JAR file).
任何关于如何解决这个问题的提示都会非常感激(理想情况下无需从JAR文件中提取image.png)。
Any hint on how to solve this would be greatly appreciated (ideally without having to extract the image.png from the JAR file).
非常感谢,
Thomas
Many thanks,Thomas
推荐答案
尝试使用资源网址:
URL url = getClass().getResource("/img/image.png");
String tt = "<html><body><img src='" + url + "'></body></html>";
编辑: 从可执行文件运行的简单示例显示工具提示的jar:
simple example run from executable jar that shows tooltip:
@Override
public String generateToolTip(XYDataset arg0, int arg1, int arg2) {
return String.format(
"<html><body><img src='%s'> some data </body></html>",
getClass().getResource("/images/duke.gif"));
}
这篇关于带有JAR文件中图像的Java工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!