在没有证据的情况下,听起来您正在事件调度线程的上下文中循环。
这意味着在您退出循环(和方法)之前, 的内容JTextArea
不会更新到屏幕上
Swing 是一个单线程环境。事件调度线程负责处理绘制请求等。任何阻塞该线程的东西都会阻止它处理绘画更新。
还要求您对 UI 进行的所有更新和交互都在 EDT 的上下文中完成......
你有几个选择...
您可以使用 a javax.swing.Timer
,这将允许ActionEvent
在常规基础上触发 an ,这是在 EDT 的上下文中触发的。这意味着当计时器“等待”时,它不会阻塞 EDT ......
您可以使用SwingWorker
,它允许您在后台线程中执行处理,但为您提供publish
返回 EDT 的能力结果以及 EDTprocess
内的这些更新......
查看Swing 中的并发以获取更多详细信息
更新了示例
javax.swing.Timer
充当一种循环(都是不受控制的循环)。每个时期,都会触发。您需要将其视为刚刚循环另一个迭代并根据需要更新 UI 的状态......
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 TextTimer {
public static void main(String[] args) {
new TextTimer();
}
public TextTimer() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static final String[] TEXT = new String[]{
"Why, you wanna tell me how to live my life?",
"Who, are you to tell me if it's black or white?",
"Mama, can you hear me? Try to understand.",
"Is innocence the difference between a boy and a man?",
"My daddy lived the lie, that's just the price that he paid",
"Sacrificed his life, just slavin' away.",
"",
"Ohhh, if there's one thing I hang onto,",
"That gets me through the night.",
"I ain't gonna do what I don't want to,",
"I'm gonna live my life.",
"Shining like a diamond, rolling with the dice,",
"Standing on the ledge, I show the wind how to fly.",
"When the world gets in my face,",
"I say, Have A Nice Day.",
"Have A Nice Day",
"",
"Take a look around you; nothing's what it seems",
"We're living in the broken home of hopes and dreams,",
"Let me be the first to shake a helping hand.",
"Anybody brave enough to take a stand,",
"I've knocked on every door, on every dead end street,",
"Looking for forgiveness,",
"what's left to believe?",
"",
"Ohhh, if there's one thing I hang onto,",
"That gets me through the night.",
"I ain't gonna do what I don't want to,",
"I'm gonna live my life.",
"Shining like a diamond, rolling with the dice,",
"Standing on the ledge, I show the wind how to fly.",
"When the world gets in my face,",
"I say, Have A Nice Day.",
"Have A Nice Day.",
"",
"[Guitar Solo]",
"",
"Ohhh, if there's one thing I hang onto,",
"That gets me through the night.",
"I ain't gonna do what I don't want to,",
"I'm gonna live my life.",
"Shining like a diamond, rolling with the dice,",
"Standing on the ledge, I show the wind how to fly.",
"When the world gets in my face,",
"I say, Have A Nice Day.",
"Have A Nice Day.",
"Have A Nice Day.",
"Have A Nice Day.",
"Have A Nice Day.",
"",
"When The world keeps trying, to drag me down,",
"I gotta raise my hands, I'm gonna stand my ground.",
"Well I say, Have A Nice Day.",
"Have A Nice Day",
"Have A Nice Day"
};
public class TestPane extends JPanel {
private JTextArea ta;
private int currentLine = 0;
public TestPane() {
setLayout(new BorderLayout());
ta = new JTextArea(20, 40);
add(new JScrollPane(ta));
Timer timer = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = TEXT[currentLine];
ta.append(text + "\n");
ta.setCaretPosition(ta.getText().length());
currentLine++;
if (currentLine >= TEXT.length) {
((Timer)e.getSource()).stop();
}
}
});
timer.start();
}
}
}