Java swing:多行标签?

发布于 2024-08-19 03:04:46 字数 757 浏览 6 评论 0原文

可能的重复:
JLabel 中的多行文本

我想这样做:

JLabel myLabel = new JLabel();
myLabel.setText("This is\na multi-line string");

标签

This isa multi-line string

目前这会导致显示我想要的 而是这样做:

This is
a multi-line string

有什么建议吗?

谢谢


编辑:已实现的解决方案

在方法正文中:

myLabel.setText(convertToMultiline("This is\na multi-line string"));

辅助方法:

public static String convertToMultiline(String orig)
{
    return "<html>" + orig.replaceAll("\n", "<br>");
}

Possible Duplicate:
Multiline text in 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>");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

潇烟暮雨 2024-08-26 03:04:46

您可以在 JLabels 中使用 HTML。要使用它,您的文本必须以 开头。

将文本设置为“这是
多行字符串”
,它应该可以工作。

请参阅 Swing 教程:JLabel多行标签 (HTML) 了解更多信息。

You can use HTML in JLabels. To use it, your text has to start with <html>.

Set your text to "<html>This is<br>a multi-line string" and it should work.

See Swing Tutorial: JLabel and Multiline label (HTML) for more information.

春风十里 2024-08-26 03:04:46
public class JMultilineLabel extends JTextArea{
    private static final long serialVersionUID = 1L;
    public JMultilineLabel(String text){
        super(text); // According to Mr. Polywhirl, this might improve it -> text + System.lineSeparator()
        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);
    }
} 

对我来说看起来完全一样,但很丑

public class JMultilineLabel extends JTextArea{
    private static final long serialVersionUID = 1L;
    public JMultilineLabel(String text){
        super(text); // According to Mr. Polywhirl, this might improve it -> text + System.lineSeparator()
        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

尛丟丟 2024-08-26 03:04:46

另一种简单的方法(但稍微改变文本样式)是使用

html 块。

如果您使用的字符串来自用户输入框,这将保留用户输入的任何格式。

例子:

JLabel label = new JLabel("<html><pre>First Line\nSecond Line</pre></html>"); 

Another easy way (but changes the text style a bit) is to use a <pre></pre> html block.

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>"); 
白龙吟 2024-08-26 03:04:46

在 jlabel 中写入多行文本的直接过程是:

JLabel label = new JLabel("<html>First Line<br>Second Line</html>"); 

The direct procedure of writing a multi-line text in a jlabel is:

JLabel label = new JLabel("<html>First Line<br>Second Line</html>"); 
烟花易冷人易散 2024-08-26 03:04:46

JLabel 或任何 Swing 组件中使用 html 的问题是,您必须将其样式设置为 html,而不是使用通常的 setFontsetForeground等等。如果你同意的话,那就好。

否则,您可以使用 JIDE 中的 MultilineLabel 之类的内容,它扩展了 <代码>JTextArea。它是他们的开源公共层的一部分。

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.

Otherwise you can use something like MultilineLabel from JIDE, which extends JTextArea. It's part of their open source Commom Layer.

灯下孤影 2024-08-26 03:04:46

JLabel可以接受html代码。也许您可以尝试使用
标签。

例子:

JLabel myLabel = new JLabel();
myLabel.setText("<html> This is a <br> multi-line string </html>");

JLabel can accept html code. Maybe you can try to use the <br> tag.

Example:

JLabel myLabel = new JLabel();
myLabel.setText("<html> This is a <br> multi-line string </html>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文