我有一个JLabel,其中包含一些html格式的文本。最后尝试在JTable中包装行。但是,我的问题可以通过JLabel简化。在HTML格式中是这样的:JLabel中的HTML不会在浏览器中显示相同的结果
<html>
<body style='width: 250px;'>
<p style='word-wrap: break-word;'>some string</p>
</body>
</html>
在浏览器中运行的一切这正常工作,甚至有很长的一个字串,它包装,因为我希望它。如果我把这个在JLabel像这样:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class HtmlInLabelExample extends JFrame {
private static final long serialVersionUID = 2194776387062775454L;
public HtmlInLabelExample() {
this.setTitle("HTML within a JLabel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.setSize(new Dimension(300, 100));
this.setLocationRelativeTo(null);
String HTML_1 = "<html><body style='width: ";
String HTML_2 = "px;'><p style='word-wrap: break-word;'>";
String HTML_3 = "</p></body></html>";
String strWithSpaces = "This is a long String that should be wrapped and displayed on multiple lines. However, if this was only one really long word, that doesnt work.";
String strWithoutSpaces = "ThisisalongStringthatshouldbewrappedanddisplayedonmultiplelines.However,ifthiswasonlyonereallylongword,thatdoesntwork.";
JLabel lblHtml = new JLabel();
lblHtml.setText(HTML_1 + String.valueOf(250) + HTML_2 + strWithoutSpaces + HTML_3);
this.add(lblHtml);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new HtmlInLabelExample();
}
});
}
}
包装作品,但它没有打破其间的话,就好像我wouldnt使用样式=“自动换行:打破字;.
有人可以向我解释为什么html格式在浏览器中工作不同(我尝试了最常见的),并且如果有解决方法吗?
'WebView'可以嵌入到Swing中,例如[https://stackoverflow.com/a/31576647/230513]。 – trashgod