0

我正在尝试创建具有混合内容的 JLabel:文本和内联图像。稍后的组件应该包含图像和文本的动态计数(具有动态顺序)。

我的第一步:创建一个 JLabel,如下所示:

JLabel label = new JLabel("<html>wow! <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAMAAABiM0N1AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAFQUExURUxpcbdSB+dzD+yFFQsEAPSHFhwMABkMAAAAAMpkC9l2EspgCoRHCUMiA+mEFPOOFcBpD/CDFPB9FOh+E08nA+99E+h2E9prDrRgDa5fDrVhC7FiD//JOv/LOf/adv/PKv/IKvuqIf/LPf/OMP/qufuxJP/aLP/GNf7CKPqkIP/UK//tw//glvy1J//bgP/fjv/ehvueHf+/M//jnP/ko//mqf28JvmSGP/or//RVf/Xa//uzPr6++fn6JRfEP/TXcN/Ftzb2v25LqhtEvuZG//WZFkgAKBmEX1OColVDMmJGf/gLbt6Ff/OScjJyciEF7JxE3Q8B/7yNGcqAv3pMf/x1quAG/7TQWQ9BUsyBIx4ZL6cJ7GLIcuqJNi0I/3hTOzcLlQ1BHRKHcSKGbSuqP/FTv7PdPjMaebMT6KWiVZBH3pdQdKzT93PK/m/Qfrx4U/NK0MAAAAcdFJOUwAWeucu/iIFEkmxK3NT3/mc9t/NQ9acZYOTkJKEXR2kAAAGSElEQVRYw+2Y61vaSBSHyz2AXBW1bQRRrlHcha1EZRsKpCYpaImwgNy1ICDq//9xz0wCJAUB20/7PPsTA5k5eZ9zzswcZnj37n/9rjRWqxlktWp+C7Kzvel2GUAu9+b2zq/BNOZtj4umWTaOxbI07fJsm9/K0mg9BpqNR6P+qaLROEsbPFrNmzBuRIGnsy/9wQNo0H/JIhiw3GujTDsemgVK8OXhOJD6R1YqcPzwEgQWS3t2TOtwrBYDwmQHnxAkNRW6+TTIIpTBYl3NMbvpuD+YfQgoGApa4CEb9Mdpt3kVR+sCd4L901TqaKIU/B1NblOp034QnHJpl6dHC2GBO0evKgD/4BSEp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KcrB+DfHlGrN3eAus6o6RIqMz1qP54e1cTm3kMivlZJ6He02zpokEJ1OYF+Pq4AxgMD+Aycmog+ePHW6DwgiB0Kdkh3dZPOxvJJZi5VLkioNoo8LXaHeDg0cdH6R084QUG6qTQkhxKzjmEXLJjlygq1GW4YrEIFRu+foAnS8CIHPQUmUoZraB9lKGtua0WsUtPXHpmcoXCl0KhUMRALPSxgFXkuG4eQChD9C4xv/PTo7mEXSq3mWLhyysqIA5a0+BQ3KJfsPdD+Y6h4EKhmzaXA9L3OSFOrluGCoAD020t3LQTG3hWAih/85wD0vfv31QCTpGryBwIzLFBLD4/GD/QQPJhUrfJQXg/1JxCjmvdTDn0B+MrpwiT3kljnwBVvnmWUEjf0AUwuUb3Jj/lOPWvnkYmJOwUoBoVGC55rHK5SquL3VnNQaSPDj9edBKq/IwKGqjZaHdvpphk0O/4qF96jDTpN3RxaflSIcyCx8voelPOYwxyBwr+hn7FcdRktFlov4yipFqdl2s3JS8wP22xGVcfa4mtXXscl3AZJWuKidt3t4h1TseE0ebUwYErKdVMhENXVMcQRue0GYk1z+uE3ua003C2jQFMVjIZg7MsbXfa9IRp7V8QEGrDYnegHxGCWOjHA4fdsvEmjIQyAuu9ZVPnoFmWdug2Le+BYnwjZsrS22wbWDab/tcoUxhBGEHw9uuQ/7r+BdVmcTguphkzAAAAAElFTkSuQmCC"></html>");

如果我将标签添加到组件,它看起来像这样:

在此处输入图像描述

-> 缺少图像!

但是如果我将标签中的 html 代码放在一个 .html 文件中并用 chrome 打开它,它看起来不错: 在此处输入图像描述

我究竟做错了什么?

问候,弗洛里安

4

1 回答 1

2

尝试使用 Jlabel 的 setIcon() 方法添加您的图像:

ImageIcon icon = new ImageIcon(YOUR_PATH); 
JLabel thumb = new JLabel();
thumb.setIcon(icon);

通过用图像的绝对誓言替换 YOUR_PATH。希望它会有所帮助!

编辑:(如果您真的想使用 html 图像标记,Java 可能会在查找图像时遇到一些困难:使用它来获得程序可用的正确路径(假设您的图像在当前目录中):

URL url = HTMLLabel.class.getResource( "/image.png" );

然后像这样访问您的图像:

"...<img src =" + url + "/> ..."
于 2016-05-20T11:45:08.983 回答