如何向JLabel添加文本 [英] How to add text to JLabel

查看:412
本文介绍了如何向JLabel添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个图书阅读器,它将读取文件的内容并将其发送到Object [].我希望在我的页面上逐行显示此内容.我正在考虑某种循环,该循环会将文本添加到标签中,但是这是我的问题:如何将文本添加到JLabel的末尾,而不是设置整个内容?

I am creating a book reader, which takes the content of a file and sends it to an Object[]. I want this to be displayed, line by line on my page. I'm considering a loop of some sort that will add text to the label, but here's my question: How to I add text to the end of a JLabel, rather than setting the whole thing?

推荐答案

您可以使用getText()检索其中的内容,然后使用setText()设置新值.

You can use getText() to retrieve what's there, and then setText() to set the new value.

因此,将something添加到末尾,您会这样做

So to add something to the end, you'd do

label.setText(label.getText()+"something");

请记住,您可能想要在中间添加一个空格.如果您要添加新的String str,则可能需要

Remember you'll probably be wanting to add a space in the middle. If you've got a new String str you want to append, you will probably want

label.setText(label.getText()+" "+str);

确保添加空格,然后添加str的内容.

to make sure you add the space and then the contents of str.

这篇关于如何向JLabel添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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