java JLabel.setText() 中的换行符

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

newline character in JLabel.setText()

javaswingnewlinejlabelmultiline

提问by trs

How can I insert a newline when I am using JLabel.setText()? I tried to use Html but can seem to make it work for setText, only for the initial declaration of the jLabel

使用 JLabel.setText() 时如何插入换行符?我尝试使用 Html 但似乎可以使其适用于 setText,仅适用于 jLabel 的初始声明

the way to do it when initially declaring the jlabel is:

最初声明 jlabel 时的做法是:

label = new JLabel("<html>Hello World!<br>blahblahblah</html>");

my code:

我的代码:

textString += "<html> quantityTextField.getText() +
   theInventory.findItem(UPCTextField.getText()).toString() + <br> </html>";

purchaseInfo.setText( textString);

it displays the html tags and the method names instead of the string returned by the methods

它显示 html 标签和方法名称,而不是方法返回的字符串

回答by Ernest Friedman-Hill

If your setText()call changes the preferred dimensions of the JLabel, then you need to call revalidate()on the container to get the layout redone.

如果您的setText()调用更改了 的首选尺寸JLabel,则您需要调用revalidate()容器以重新完成布局。

Looking at the code snippet you've added, I see a
at the very end of the line -- which won't really do anything anyway -- and a lot of misquoted method calls which are done such that the method names are part of the HTML. If you do something along the lines of

查看您添加的代码片段,我
在该行的最后看到了一个- 无论如何它不会真正做任何事情 - 以及许多错误引用的方法调用,这些方法调用是这样的,方法名称是其中的一部分HTML。如果你做一些类似的事情

label.setText("<html>Hello World!<br>blahblahblah</html>");
label.getParent().revalidate();

your newline ought to show up.

你的换行符应该出现。

回答by mre

The formatting of your text is all wrong. This is the string that you're currently setting:

您的文本格式完全错误。这是您当前设置的字符串:

"<html>Hello World!<br>blahblahblah</html><html> quantityTextField.getText()+ theInventory.findItem(UPCTextField.getText()).toString() + <br> </html>"

Now tell me the problem isn't painfully obvious (by the way, HTML isn't your only problem)... Anyway, for more information, please see How to Use HTML in Swing Components.

现在告诉我这个问题并不明显(顺便说一下,HTML 不是您唯一的问题)...无论如何,有关更多信息,请参阅如何在 Swing 组件中使用 HTML

回答by MarianP

textString+="<html> " + quantityTextField.getText()+ theInventory.findItem(UPCTextField.getText()).toString() + "<br> </html>";

funny.

有趣的。

回答by KKeiper1103

In personal experience, I wouldn't use HTML tags in a JLabel. If you're not doing any formatting (which it looks like you aren't, other than inserting a line break), you're better off using character codes like '\n' because of simplicity and size.

根据个人经验,我不会在 JLabel 中使用 HTML 标签。如果您没有进行任何格式化(看起来您没有进行任何格式化,除了插入换行符之外),您最好使用诸如 '\n' 之类的字符代码,因为它既简单又大小。

[code] label = new JLabel("Hello World!\nblahblahblah");

[代码] label = new JLabel("Hello World!\nblahblahblah");

textString += quantityTextField.getText() + theInventory.findItem(UPCTextField.getText()).toString() + "\n";

textString +=quantityTextField.getText() + theInventory.findItem(UPCTextField.getText()).toString() + "\n";

purchaseInfo.setText( textString); [/code]

购买信息.setText(textString); [/代码]

The reason the method names were showing up was because you had them enclosed within quotes. It was taking the methods as actual text to write to the screen, not a set of instructions to perform.

方法名称出现的原因是因为您将它们括在引号中。它将方法作为写入屏幕的实际文本,而不是一组要执行的指令。

回答by gherson

Anyone not testing their answer should qualify it with "I think".

任何不测试他们的答案的人都应该用“我认为”来限定它。

As others have said, \n won't work, .revalidate() isn't necessary, and html is. See Newline in JLabel

正如其他人所说, \n 不起作用, .revalidate() 不是必需的,而 html 是。请参阅JLabel 中的换行符

回答by 01010011 01010101 01001111

Hello bro insert your new line between "pre"

你好兄弟在“pre”之间插入你的新行

JLabel l=new JLabel("<html><pre>Hello world\nBlahblahblah</pre></html>");

i hope this help you

我希望这对你有帮助

回答by Garrett Hall

You appear to be concatenating something with your html string.

您似乎正在将某些内容与您的 html 字符串连接起来。

Surround the text in the <html>tags and only use the <html>tags once.

<html>标签中的文本括起来,并且只使用<html>一次标签。