如何打印带下划线的字符串

时间:2018-04-16 19:08:54

标签: java

我正在尝试以字符串或标签值为基础。我试过这段代码:

    JLabel label = new JLabel("Underlined Label");
    Font font = label.getFont();
    Map attributes = font.getAttributes();
    attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    label.setFont(font.deriveFont(attributes));
    System.out.println(label.getText());

输出是带下划线的标签,它没有为标签加下划线。 我也试过

    JLabel label = new JLabel("<HTML><U>YOUR TEXT HERE</U></HTML>");
    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    System.out.println(label.getText());

这也行不通。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

如果您的终端支持Unicode,您可以将组合变音符号U + 0332 COMBINING LOW LINE应用于字符串中的每个字符:

<link rel="stylesheet" href="https://bootswatch.com/4/minty/bootstrap.min.css">

在我的Windows 10笔记本电脑上,public class Underline { public static void main(String[] args) { for (String s : args) { StringBuilder underlined = new StringBuilder(); for (char c : s.toCharArray()) { underlined.append(c).append('\u0332'); } System.out.println(underlined); } } } 生成:

java -Dfile.encoding=UTF-8 Underline underlined

注意:我使用mintty 2.8.4作为我的终端模拟器。如果您的终端没有良好的Unicode支持,这将不起作用。