html呈现文本的竖行间距

  • 本文关键字:文本 html java swing
  • 更新时间 :
  • 英文 :


Java只支持旧的html标准,但是在JComponent中增加行间距是否有比我这个看起来有点笨拙的解决方案更优雅的方法?

我第一次使用<span style="line-height:...">没有用,最后在改变字体大小后在每行添加了第二个换行符。

import java.awt.*;
import javax.swing.*;
public class HtmlLineSpacing extends JFrame {
public HtmlLineSpacing() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("JLabels with HTML text");
setLayout(new FlowLayout());
JLabel lb= new JLabel("""
<html><u>Regular</u><br>
Line 1<br>
Line 2<br>
Line 3<br>
Line 4</html>""");
add(lb);
add(Box.createHorizontalStrut(30));
//  Other attempts
//  <html><span style="line-height:150%">Line 1<br>
//  <html><span style="line-height:20pt">Line 1<br>
//  <html><span style="line-height:20px">Line 1<br>
//  <html><span style="line-height:5mm">Line 1<br>
lb= new JLabel("""
<html><u>Failure</u><br>
<span style="line-height:1.7">
Line 1<br>
Line 2<br>
Line 3<br>
Line 4<br></span></html>""");
add(lb);
add(Box.createHorizontalStrut(30));
lb= new JLabel("""
<html><u>Workaround</u><br>
Line 1<br>
<span style="font-size:3pt"><br></span>
Line 2<br>
<span style="font-size:3pt"><br></span>
Line 3<br>
<span style="font-size:3pt"><br></span>
Line 4<br></span></html>""");
add(lb);
add(Box.createHorizontalStrut(30));
lb= new JLabel("""
<html><u>Two full line breaks</u><br>
Line 1<br><br>
Line 2<br><br>
Line 3<br><br>
Line 4</html>""");
add(lb);
pack();
setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(HtmlLineSpacing::new);
}
}

Sergiy Medvynskyy的建议奏效了。谢谢你。

lb= new JLabel("""
<html><table cellspacing=4 cellpadding=0>
<tr><u>Table</u></tr>
<tr>Line 1</tr>
<tr>Line 2</tr>
<tr>Line 3</tr>
<tr>Line 4</tr>
</html>""");

在修改cellspacing参数时调整行间距

最新更新