JLabel.setText()中的换行符 [英] newline character in JLabel.setText()

查看:124
本文介绍了JLabel.setText()中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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时的方法是:

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

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

我的代码:

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

purchaseInfo.setText( textString);

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

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

推荐答案

如果您的setText()调用更改了JLabel的首选尺寸,则需要在容器上调用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.

看一下您添加的代码片段,我在行的最后看到一个
-不管怎样它实际上不会做任何事情-以及很多被错误引用的方法调用,这些调用使得方法名称是HTML的一部分.如果您按照

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.

这篇关于JLabel.setText()中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆