我有一個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