newline character in JLabel.setText()

淺唱寂寞╮ 提交于 2019-12-10 17:47:05

问题


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


回答1:


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.




回答2:


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.




回答3:


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

funny.




回答4:


You appear to be concatenating something with your html string.

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




回答5:


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.




回答6:


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




回答7:


Hello bro insert your new line between "pre"

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

i hope this help you



来源:https://stackoverflow.com/questions/7666496/newline-character-in-jlabel-settext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!