java 如何将长字符串放入 JLabel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6984099/
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-30 18:08:57  来源:igfitidea点击:

How to fit a long String into a JLabel

javaswing

提问by Tobias Moe Thorstensen

as the title says: I need to fit a JLabel into a JFrame, but the text in the JLabel are too long, so I need to add some linebreaks. The text in the JLabel are obtained from an online XML file, so i cant just change the text to contain linebreaks.

正如标题所说:我需要将 JLabel 放入 JFrame 中,但 JLabel 中的文本太长,因此我需要添加一些换行符。JLabel 中的文本是从在线 XML 文件中获取的,因此我不能将文本更改为包含换行符。

This code extracts data from the XML-file

此代码从 XML 文件中提取数据

Element element = (Element)nodes1.item(i);
            String v?r = getElementValue(element,"body");
            String v = v?r.replaceAll("<.*>", "" );  
            String forecast = "V?r: " + v;

in this case the string I want to add some linebreaks to the string v. The String v contains the parsed data from the xml file. The String forecast is returned and set as a text to the JLabel.

在这种情况下,我想向字符串 v 添加一些换行符的字符串。字符串 v 包含来自 xml 文件的解析数据。返回字符串预测并将其设置为 JLabel 的文本。

Just ask if something is uncleared, thanks in advance!

请问有什么不清楚的地方,先谢谢了!

回答by tskuzzy

I suggest using a JTextAreainstead and turning wrapping on. The only way to do it in a JLabelis to put line breaks <br />, which wouldn't work (at least not easily) in your situation if you don't know the text beforehand.

我建议使用 aJTextArea代替并打开包装。在 a 中做到这一点的唯一方法JLabel是放置换行符<br />,如果您事先不知道文本,这在您的情况下将不起作用(至少不容易)。

JTextAreais much more flexible. By default it looks different, but you can fiddle around with some of the display properties to make it look like a JLabel.

JTextArea灵活得多。默认情况下,它看起来不同,但您可以调整一些显示属性,使其看起来像JLabel.



A simple modifiedusage example taken from the How to Use Text Areastutorial -

取自How to Use Text Areas教程的简单修改使用示例-

public class JTextAreaDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {         
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame("JTextArea Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        JTextArea textArea = new JTextArea(
                "If there is anything the nonconformist hates worse " +
                "than a conformist, it's another nonconformist who " +
                "doesn't conform to the prevailing standard of nonconformity.", 
                6, 
                20);
        textArea.setFont(new Font("Serif", Font.ITALIC, 16));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setOpaque(false);
        textArea.setEditable(false);

        panel.add(textArea);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

enter image description here

在此处输入图片说明

回答by Thomas

JLabel is able to display HTML text, i.e. if you wrap your text with <html>your text<html>it might be able to wrap the text. That's not tested though, so YMMV.

JLabel 能够显示 HTML 文本,即如果您用<html>your text<html>它包裹文本,它可能能够包裹文本。不过这还没有经过测试,所以 YMMV。

回答by othman

you can dynamically tell your JLabel to resize itself to fit the text.

您可以动态地告诉您的 JLabel 调整自身大小以适应文本。

if you are not using a LayoutManager try :

如果您没有使用 LayoutManager 尝试:

        jLabel.setText ("A somewaht long message I would not want to
stop");
        jLabel.setSize(jLabel.getPreferredSize());

If you are using a Layout Manager this snippet should work:

如果您使用的是布局管理器,此代码段应该可以工作:

        jLabel.setText ("A somewaht long message I would not want to
stop");
        jLabel.validate();