逆に証拠がなければ、イベントディスパッチスレッドのコンテキスト内でループしているように聞こえます。
これは、ループ (およびメソッド) を終了するまで、の内容がJTextArea
画面に更新されないことを意味します。
Swing はシングルスレッド環境です。イベント ディスパッチ スレッドは、ペイント リクエストの処理などを担当します。このスレッドをブロックすると、ペイントの更新を処理できなくなります。
また、UI に対して行うすべての更新と操作は、EDT のコンテキスト内で行う必要があります...
いくつかのオプションがあります...
を使用すると、EDT のコンテキスト内でjavax.swing.Timer
トリガーされる定期的に をトリガーできます。ActionEvent
これは、タイマーが「待機」している間、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();
}
}
}