java 换行 \n 在 JButton.setText("fnord\nfoo") 中不起作用;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13503280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:00:21  来源:igfitidea点击:

New Line \n is not working in JButton.setText("fnord\nfoo") ;

javaswingawtjbuttonmultiline

提问by k0pernikus

On a JButton, I want to list information on multiple lines. I tried \nas a new line character but it didn't work. The following code:

在 JButton 上,我想在多行上列出信息。我尝试\n作为换行符,但没有用。以下代码:

JButton.setText("fnord\nfoo") ;

will be displayed as:

将显示为:

fnordfoo

How do I force a line break?

如何强制换行?

回答by k0pernikus

JButton accepts HTML, so for the line break to work use:

JButton 接受 HTML,因此要使换行符起作用,请使用:

 JButton.setText("<html>fnord<br />foo</html>");