问题
I am trying to display a JLabel which has a few lines of text and an image as follows:
String html = "<html> hello </br> <img src = \"/absolute/path/here\" height = \"30\" width =\"40\"/> </html>";
JLabel l = new JLabel(html);
For the image all I get is a broken image, is it possible to nest img tags inside a JLabel?
EDIT: I want to add multiple images to the JLabel so I don't think the use of an ImageIcon will do here.
Thanks
回答1:
Rather then try to have multiple images on a single JLabel why not simply have many JLabels, each with one image (as uthark described) and then group all the labels together on a single JPanel. That should give you the effect you are looking for with only minimal additional complexity.
回答2:
For the image all I get is a broken image, is it possible to nest img tags inside a JLabel
It is possible to display image(s) in a JLabel's text. You are getting broken images because the path is not correct. You need to either prefix your path with file:
or preferably get java to do it for you with class.getResource("/your/path")
. Here is a working example, just insert valid resource paths.
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);
}
}
For more complex HTML in java, I recommend xhtmlrenderer.
回答3:
File f = new File("C:\image.jpg");
jLabel1.setText("<html><img src=\"file:"+f.toString()+"\">");
This works for me. It's simple and gives possibility to put any number of images you want, not just one image icon. It's not working without quotation marks.
回答4:
Unless you're happy with JEditorPane, you're basically looking at a full webbrowser inside of Swing.
Ideally, you would use JWebPane which would be a WebKit view as a Swing component, but it isn't out yet. The most recent information I could find was this blog post.
The DJ project allows embedding the platform's native browser in Swing. It uses Internet Explorer on Windows and XULRunner on Linux. It does not have any support for Mac.
回答5:
Use a JEditorPane to display the HTML. You can change the background, forground, font etc so it looks like a label.
回答6:
The above approaches don't seem to work anymore.
It seems that you now have to use an actual URI in the img
tag.
Things work for me with "<img src=\"" + new File(...).toURI() + "\">"
.
回答7:
Embedded images are not supported in the HTML. Thus, you have to use setIcon or supply an ImageIcon to the JLabel constructor; the HTML cannot have an IMG tag.
JLabel imageLabel =
new JLabel(labelText,
new ImageIcon("path/to/image.gif"),
JLabel.CENTER);
In your case you need to use JTextPane to display HTML. See tutorial here
来源:https://stackoverflow.com/questions/2484488/can-jlabel-have-img-tags