Java Swing:多行标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2152742/
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
Java swing: Multiline labels?
提问by bguiz
Possible Duplicate:
Multiline text in JLabel
可能的重复:
JLabel 中的多行文本
I want to do this:
我想做这个:
JLabel myLabel = new JLabel();
myLabel.setText("This is\na multi-line string");
Currently this results in a label that displays
目前,这会导致一个标签显示
This isa multi-line string
I want it to do this instead:
我希望它这样做:
This is
a multi-line string
Any suggestions?
有什么建议?
Thank you
谢谢
EDIT: Implemented solution
编辑:实施的解决方案
In body of method:
在方法体中:
myLabel.setText(convertToMultiline("This is\na multi-line string"));
Helper method:
辅助方法:
public static String convertToMultiline(String orig)
{
return "<html>" + orig.replaceAll("\n", "<br>");
}
采纳答案by Peter Lang
You can use HTML
in JLabels
. To use it, your text has to start with <html>
.
您可以HTML
在JLabels
. 要使用它,您的文本必须以<html>
.
Set your text to "<html>This is<br>a multi-line string"
and it should work.
将您的文本设置为"<html>This is<br>a multi-line string"
,它应该可以工作。
See Swing Tutorial: JLabeland Multiline label (HTML)for more information.
有关更多信息,请参阅Swing 教程:JLabel和多行标签 (HTML)。
回答by KWAN TOH CHOONG
JLabel can accept html code. Maybe you can try to use the <br>
tag.
JLabel 可以接受 html 代码。也许你可以尝试使用<br>
标签。
Example:
例子:
JLabel myLabel = new JLabel();
myLabel.setText("<html> This is a <br> multi-line string </html>");
回答by Viachaslau Kamkou
回答by Marco Schmid
The direct procedure of writing a multi-line text in a jlabel is:
在 jlabel 中写入多行文本的直接过程是:
JLabel label = new JLabel("<html>First Line<br>Second Line</html>");
回答by Austyn Mahoney
Another easy way (but changes the text style a bit) is to use a <pre></pre>
html block.
另一种简单的方法(但稍微改变了文本样式)是使用<pre></pre>
html 块。
This will persist any formatting the user entered if the string you are using came from a user input box.
如果您使用的字符串来自用户输入框,这将保留用户输入的任何格式。
Example:
例子:
JLabel label = new JLabel("<html><pre>First Line\nSecond Line</pre></html>");
回答by Geoffrey Zheng
The problem with using html in JLabel
or any Swing component is that you then have to style it as html, not with the usual setFont
, setForeground
, etc. If you're ok with that, fine.
在使用HTML的问题JLabel
或任何Swing组件是,你再要的风格为HTML,不跟平时setFont
,setForeground
等等。如果你真行与精细。
Otherwise you can use something like MultilineLabelfrom JIDE, which extends JTextArea
. It's part of their open source Commom Layer.
否则,您可以使用JIDE 中的MultilineLabel 之类的东西,它扩展了JTextArea
. 这是他们开源Commom Layer的一部分。
回答by Whimusical
public class JMultilineLabel extends JTextArea{
private static final long serialVersionUID = 1L;
public JMultilineLabel(String text){
super(text);
setEditable(false);
setCursor(null);
setOpaque(false);
setFocusable(false);
setFont(UIManager.getFont("Label.font"));
setWrapStyleWord(true);
setLineWrap(true);
//According to Mariana this might improve it
setBorder(new EmptyBorder(5, 5, 5, 5));
setAlignmentY(JLabel.CENTER_ALIGNMENT);
}
}
It totally looks the same for me, but its ugly
对我来说完全一样,但它丑陋