Java:JLabels中的换行符? [英] Java: Linebreaks in JLabels?

查看:102
本文介绍了Java:JLabels中的换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多行文本制作Swing JLabel。它添加得很好,但换行没有通过。我该怎么做呢?或者,我可以只指定一个JLabel的最大宽度,并知道文本会换行,就像在div中一样吗?

I'm trying to make a Swing JLabel with multiple lines of text. It's added just fine, but the line breaks don't come through. How do I do this? Alternatively, can I just specify a maximum width for a JLabel and know that the text would wrap, like in a div?

    private void addLegend() {
        JPanel comparisonPanel = getComparisonPanel();

        //this all displays on one line
        JLabel legend = new JLabel("MMM FFF MMM FFFO O OOM   M MMMM.\nMMM FFF MMM FFFO O OOM   M MMMM.\nMMM FFF MMM FFFO O OOM   M MMMM.\n"); 

        comparisonPanel.add(legend);        
    }


推荐答案

在setText中使用HTML,例如

Use HTML in setText, e.g.

myLabel.setText("<html><body>with<br>linebreak</body></html>");

这篇关于Java:JLabels中的换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆