[英]Updating textArea inside iteration
i have a fun project where i need to change the content of a text area inside a iteration. 我有一个有趣的项目,我需要在一次迭代中更改文本区域的内容。
Its a character, a "projectile", moving trought a string. 它是一个人物,一个“弹丸”,动着弦。 The string is updated and sent to the textArea inside the iteration, and the iteration stops when the character reaches a wall.
该字符串将更新并发送到迭代内的textArea中,并且当角色到达墙时迭代停止。
But my textArea only updates (visually) when i leave the iteration. 但是,我的textArea仅在离开迭代时(在视觉上)更新。 While im inside it, textArea freezes, as if its waiting for the iteration, even with Thread.sleep() inside it.
当我在其中时,textArea会冻结,就好像它在等待迭代一样,即使其中包含Thread.sleep()。
I made an MVCE exemplifing the problem bellow, notice the text only shows after the iteration, i want it to apper in every step of the while. 我制作了一个MVCE来举例说明问题,请注意该文本仅在迭代后显示,我希望它能在一段时间的每一步中都出现。
public class GUIProblem extends JFrame{
public GUIProblem() {
setSize(640, 480);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JTextArea textArea = new JTextArea();
textArea.setRows(10);
textArea.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
int i = 0;
while(i < 10){
textArea.setText("this text only appears after the iteration, i want it to appear in each step of the iteration!");
System.out.println("iterating..." + i++);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
textArea.setColumns(30);
panel.add(textArea);
}
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUIProblem gui = new GUIProblem( );
gui.setVisible(true);
}
});
JOptionPane.showMessageDialog(null, "Click the textArea!");
}
}
You've a classic Swing threading issue where you stop the Swing event thread in its tracks with your iteration and its Thread.sleep()
calls. 您有一个经典的Swing线程问题,您可以通过迭代及其
Thread.sleep()
调用在其轨道中停止Swing事件线程。 The solution is the same as for similar questions: use a Swing Timer or background thread such as a SwingWorker. 解决方案与针对类似问题的解决方案相同:使用Swing计时器或后台线程,例如SwingWorker。 In your case, use the Timer.
在您的情况下,请使用计时器。
For example, since you posted an MCVE 例如,由于您发布了MCVE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUIProblem extends JFrame {
public GUIProblem() {
// setSize(640, 480);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JTextArea textArea = new JTextArea(20, 50);
textArea.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mEvt) {
int i = 0;
int timerDelay = 200;
new Timer(timerDelay, new ActionListener() {
int count = 0;
private final int MAX_COUNT = 10;
@Override
public void actionPerformed(ActionEvent e) {
if (count >= MAX_COUNT) {
((Timer) e.getSource()).stop(); // stop the timer
return;
}
textArea.append("Count is: " + count + "\n");
count++;
}
}).start();
}
});
panel.add(new JScrollPane(textArea));
}
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUIProblem gui = new GUIProblem();
gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
gui.pack();
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
});
JOptionPane.showMessageDialog(null, "Click the textArea!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.