简体   繁体   English

如何在没有HTML的情况下获得多行JLabel(或看上去完全相同的JTextArea)

[英]How to get a multilined JLabel (or a JTextArea looking totally the same) without HTML

I cant believe fastest solution for a multilined JLabel is the following one (text comes from a var, and so I dont want to put HTML code manually every x chars, its so ugly): 我不能相信多行JLabel的最快解决方案是以下解决方案(文本来自var,所以我不想每x个字符手动放置HTML代码,这太丑陋了):

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);
    }
} 

... sure it isnt a better way to handle this???? ...确定这不是更好的方法吗????

If you want a multilne label then you simply use HTML in its text, as they support its use. 如果要使用多重标签,则只需在其文本中使用HTML ,因为它们支持HTML的使用。 Therefore, use line brake tag </br> to break lines or put separate lines in <p></p> paragraph tags. 因此,请使用行制动标签</br>断开行或将单独的行放在<p></p>段落标签中。

Do not forget to mark that you want to use HTML for a JLabel by starting its text with the <html> tag. 不要忘记通过以<html>标记开始JLabel的文本来标记您要对JLabel使用HTML。

More available here. 更多可用在这里。

BTW I forgot to check if there were other related questions about JLabel use and there were at least a few, check this or this . 顺便说一句,我忘了检查是否还有其他有关JLabel使用的问题,并且至少有几个问题,请检查thisthis :) :)


EDIT: 编辑:

For a working sample, showing a different approach, without setting a style and with use of paragraph and label taking available space, please see below: 对于一个工作示例,显示了一种不同的方法,没有设置样式,并且使用了段落和标签占用了可用空间,请参见以下内容:

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;

public class LabelHTMLAutoResize {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel p = new JPanel(new BorderLayout());
                JLabel l = new JLabel("<html><p> Some verrrry long text  Some verrrry long  Some verrrry long text dsa ads oiosi o</p>");
                l.setVerticalAlignment(SwingConstants.TOP);
                l.setOpaque(true);
                l.setBackground(Color.green);
                p.add(l);
                f.setContentPane(p);
                /* good practice is to use f.pack(); and let the size be automatically calculated but we want to show line wrapping thus frame size is set */
                f.setSize(200, 200);
                f.setVisible(true);
            }
        });
    }
}

I need to make linewrapping automatically, and not having to parse the incoming string and put a <BR> every x chars 我需要自动进行换行,而不必解析传入的字符串并在每个x字符中放入<BR>

Set the body width (of the HTML). 设置(HTML的)正文宽度。 Swing HTML support includes basic styles. Swing HTML支持包括基本样式。

I had some examples of using CSS to limit the width of a JLabel lying around SO somewhere.. 我有一些使用CSS来限制JLabel的宽度的示例,这些JLabel围绕SO JLabel在某个地方。


Aah yes, there it is : 嗯,是的, 它是

import javax.swing.*;

class FixedWidthLabel {

    public static void main(String[] srgs) {
        String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
        String html1 = "<html><body style='width: ";
        String html2 = "px'>";

        JOptionPane.showMessageDialog(null, new JLabel(html1+"200"+html2+s));
        JOptionPane.showMessageDialog(null, new JLabel(html1+"300"+html2+s));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM