Java 在 JLabel 中附加文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9717121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append text in JLabel
提问by Sachin Mhetre
How would I go about achieving the effect of JTextArea with JLabel?
我将如何使用 JLabel 实现 JTextArea 的效果?
I want the output to be displayed every time the button is clicked on the next line down instead of replacing what text is already there, i.e. like an append method for JLabel?
我希望每次在下一行单击按钮时都显示输出,而不是替换已经存在的文本,即像 JLabel 的附加方法一样?
I just want it to follow the same behavior as JTextArea.append.
我只是希望它遵循与 JTextArea.append 相同的行为。
Also I want to add hyperlink to each line.
另外我想为每一行添加超链接。
采纳答案by Andrew Thompson
- Use HTML formatting in the label by starting the text with prefix
<html><body>
(possibly add some in-line styles in thebody
opening element). - Add each line, ending with
<br>
or<p>
(or<li>
if adding<ul><li>
to the prefix).
- 通过以前缀开头的文本在标签中使用 HTML 格式
<html><body>
(可能在body
开始元素中添加一些内联样式)。 - 添加每一行,以
<br>
或结尾<p>
(或者<li>
如果添加<ul><li>
到前缀)。
See also How to Use HTML in Swing Components. For such gems as..
另请参阅如何在 Swing 组件中使用 HTML。对于这样的宝石..
回答by Rahul Borkar
You can do that as follows,
你可以这样做,如下所示,
label.setText(label.getText() + "text u want to append");
on each event.
在每个事件上。