Mechanisms of setText() in JTextArea?(JTextArea 中 setText() 的机制?)
问题描述
我尝试在运行时在我的 JTextArea
中显示一些文本.但是当我使用 setText
循环按顺序显示文本时,它只显示最后一个循环的文本这是我的代码:
I try to show some text in my JTextArea
in runtime. But when I use a loop of setText
to show text in order, it only show the text of the last loop
Here is my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for (int i=0;i<10;i++)
jTextArea1.setText("Example "+i);
}
我希望它显示 "Example 1", "Example 2",..,"Example 9"
.但它只显示一次Example 9
"
I want it to show "Example 1", "Example 2",..,"Example 9"
. But it only show one time "Example 9
"
谁能给我解释一下??
推荐答案
setText
就是这样做的,它将字段的文本设置"为您提供的值,删除所有以前的内容.
setText
does just that, it "sets the text" of field to the value your provide, removing all previous content.
你想要的是 JTextArea#append
如果您使用的是 Java 8,另一个选项可能是 StringJoiner
If you're using Java 8, another option might be StringJoiner
StringJoiner joiner = new StringJoiner(", ");
for (int i = 0; i < 10; i++) {
joiner.add("QUang " + i);
}
jTextArea1.setTexy(joiner.toString());
(假设每次调用actionPerformed
方法时都想替换文本,但仍然可以使用append
)
(assuming you want to replace the text each time the actionPerformed
method is called, but you can still use append
)
根据评论的假设进行更新
我假设"您的意思是您希望每个 String
显示一小段时间,然后替换为下一个 String
.
I "assume" you mean you want each String
to be displayed for a short period of time and then replaced with the next String
.
Swing 是一个单线程环境,所以任何阻塞事件调度线程的东西,比如循环,都会阻止 UI 更新.相反,您需要使用 Swing Timer
来安排定期回调,并在每次滴答时更改 UI.
Swing is a single threaded environment, so anything that blocks the Event Dispatching Thread, like loops, will prevent the UI from been updated. Instead, you need to use a Swing Timer
to schedule a callback at regular intervals and make change the UI on each tick, for example.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private String[] messages = {
"Example 1",
"Example 2",
"Example 3",
"Example 4",
"Example 5",
"Example 6",
"Example 7",
"Example 8",
"Example 9",
};
private JTextArea ta;
private int index;
private Timer timer;
public TestPane() {
setLayout(new BorderLayout());
ta = new JTextArea(1, 20);
add(new JScrollPane(ta));
JButton btn = new JButton("Start");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (timer.isRunning()) {
timer.stop();
}
index = 0;
timer.start();
}
});
add(btn, BorderLayout.SOUTH);
timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index < messages.length) {
ta.setText(messages[index]);
} else {
timer.stop();
}
index++;
}
});
}
}
}
看看 Swing 中的并发 和 如何使用 Swing 计时器 了解更多详情
Have a look at Concurrency in Swing and How to use Swing Timers for more details
这篇关于JTextArea 中 setText() 的机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JTextArea 中 setText() 的机制?
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01