JLabel.setText() 中的换行符
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您的
setText()
调用更改了JLabel
的首选尺寸,那么您需要在容器上调用revalidate()
来获取布局重做。查看您添加的代码片段,我在该行的最后看到一个
——无论如何它不会真正执行任何操作 ——以及许多错误引用的方法调用,这些方法调用的完成使得方法名称是 HTML 的一部分。如果你按照换行符做一些事情,
应该会出现。
If your
setText()
call changes the preferred dimensions of theJLabel
, then you need to callrevalidate()
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
your newline ought to show up.
您的文本格式完全错误。这是您当前设置的字符串:
现在告诉我问题并不明显(顺便说一句,HTML 不是您唯一的问题)...无论如何,有关详细信息,请参阅 如何在 Swing 组件中使用 HTML。
The formatting of your text is all wrong. This is the string that you're currently setting:
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.
有趣的。
funny.
您似乎正在将某些内容与 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.根据个人经验,我不会在 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.
任何不测试自己答案的人都应该用“我认为”来限定它。
正如其他人所说, \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
你好兄弟在“pre”之间插入你的新行
我希望这对你有帮助
Hello bro insert your new line between "pre"
i hope this help you