如何在没有 HTML 的情况下将文本移动到 JLabel 中的下一行?

How to move the text to the next line in the JLabel without HTML?

在 JLabel 中将文本移动到下一行我使用 HTML:

JLabel label = new JLabel("<html><b>Line<br/Next line</b></html>");

有没有办法在不使用 HTML 的情况下将文本移动到下一行 ?谢谢!

Is there a way to move the text to the next line without using HTML?

不是真的。相反,您必须使用其他技术,例如:

  • 使用包含 1 列的 GridLayout 中的多个 JLabel
  • 使用 JTextArea,但将其配置为看起来像 JLabel(我的最爱之一)
  • 或者通过 paintComponent(...) 方法将文本直接写入组件。

JLabel 没有其他方法。不过您可以使用自定义 Swing 组件。

HTML 方式有什么问题?你可以这样简化它:

String line1 = "...";
String line2 = "...";
String text = String.format("<html>%s<br/>%s</html>", line1, line1);
JLabel lbl = new JLabel(text);