For the image all I get is a broken image, is it possible to nest img tags inside a JLabel
可以在 JLabel 的文本中显示图像。由于路径不正确,您会收到损坏的图像。您需要为路径添加前缀,file:
或者最好让 java 为您使用class.getResource("/your/path")
. 这是一个工作示例,只需插入有效的资源路径。
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MultipleImagesExample
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JLabel label = new JLabel(
"<html>"
+ "<img src=\""
+ MultipleImagesExample.class.getResource("/resource/path/to/image1")
+ "\">"
+ "<img src=\""
+ MultipleImagesExample.class.getResource("/resource/path/to/image2")
+ "\">"
+ "The text</html>");
frame.add(label, BorderLayout.CENTER);
frame.setBounds(100, 100, 200, 100);
frame.setVisible(true);
}
}
对于 java 中更复杂的 HTML,我推荐xhtmlrenderer。