JLabel.setText() 中的换行符

发布于 2024-12-08 00:58:16 字数 479 浏览 0 评论 0原文

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

最初声明 jlabel 时的方法是:

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

我的代码:

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

purchaseInfo.setText( textString);

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

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

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

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

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

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

发布评论

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

评论(7

黯然#的苍凉 2024-12-15 00:58:16

如果您的 setText() 调用更改了 JLabel 的首选尺寸,那么您需要在容器上调用 revalidate() 来获取布局重做。

查看您添加的代码片段,我在该行的最后看到一个
——无论如何它不会真正执行任何操作 ——以及许多错误引用的方法调用,这些方法调用的完成使得方法名称是 HTML 的一部分。如果你按照换行符做一些事情,

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

应该会出现。

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.

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

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

your newline ought to show up.

执手闯天涯 2024-12-15 00:58:16

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

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

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

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.

风吹雪碎 2024-12-15 00:58:16
textString+="<html> " + quantityTextField.getText()+ theInventory.findItem(UPCTextField.getText()).toString() + "<br> </html>";

有趣的。

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

funny.

不再让梦枯萎 2024-12-15 00:58:16

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

将文本括在 标记中,并且仅使用 标记一次。

You appear to be concatenating something with your html string.

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

沉溺在你眼里的海 2024-12-15 00:58:16

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

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

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

buyInfo.setText(textString);
[/code]

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

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.

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

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

purchaseInfo.setText( textString);
[/code]

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.

残月升风 2024-12-15 00:58:16

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

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

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

廻憶裏菂餘溫 2024-12-15 00:58:16

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

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

我希望这对你有帮助

Hello bro insert your new line between "pre"

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

i hope this help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文