[英]Why is the sleep method affecting/not showing my updated JTextArea on the panel?
I have a window that has various panels. 我有一个带有各种面板的窗口。 On one panel, I have a JTextArea called dogTalk where I update its text. 在一个面板上,我有一个名为dogTalk的JTextArea,用于更新其文本。 Upon user's click of a button, I want the text to add what I have mentioned below in setText. 在用户单击按钮后,我希望文本在setText中添加我在下面提到的内容。
I used the sleep method so that the user can read my updated text and the window can close automatically within 4 seconds. 我使用了sleep方法,以便用户可以阅读我的更新文本,并且窗口可以在4秒钟内自动关闭。 (I don't want the user to have the ability to close the window on close, hence I didn't use Jframe.EXIT_ON_CLOSE but used JFrame.DO_NOTHING_ON_CLOSE and used my automatic closing with the help of sleep and system.ext(0)) (我不希望用户能够在关闭时关闭窗口,因此我没有使用Jframe.EXIT_ON_CLOSE,而是使用了JFrame.DO_NOTHING_ON_CLOSE,并借助sleep和system.ext(0)进行了自动关闭。 )
However, I noticed that the sleep method does not allow the dogTalk to get updated. 但是,我注意到sleep方法不允许dogTalk进行更新。 It prints out "we're working", though, so I am guessing it's a problem with the window? 不过,它会打印出“我们正在工作”的信息,所以我想这是窗户的问题吗? I know that the sleep is causing the issue and not something else in my code because when I commented out the sleep and system.exit(0) and tested if my if statement is executing, I noticed the JTextArea did update with my statement just fine! 我知道睡眠是导致问题的原因,而不是代码中的其他问题,因为当我注释掉sleep和system.exit(0)并测试我的if语句是否正在执行时,我注意到JTextArea确实用我的语句进行了更新! Could you please help me? 请你帮助我好吗?
if (e.getActionCommand().equals("buybone")) { 如果(e.getActionCommand()。equals(“ buybone”)){
System.out.println("We're working");
dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much thanks bye.");
try
{
TimeUnit.SECONDS.sleep(4);
}
catch ( InterruptedException e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
} }
Rather than calling System.exit
, let the application gracefully die out. 与其调用System.exit
,不如让应用程序正常退出。 An application terminates when there are no non-daemon threads still alive. 当没有非守护进程线程仍然存在时,应用程序终止。 Daemon is just a flag used to determine whether the JVM should terminate if that thread is still running; 守护程序只是一个标志,用于确定如果该线程仍在运行,则JVM是否应终止。 the JVM will still terminate if non-daemon threads are running. 如果非守护程序线程正在运行,JVM仍将终止。
With that said, the problem is that you're calling sleep
on the Event Dispatch Thread. 话虽如此,问题在于您正在事件调度线程上调用sleep
。
The EDT handles all updating and rendering of Swing and AWT components, as well as execute the behavior specified in event listeners (like ActionListener#actionPerformed(ActionEvent)
). EDT处理Swing和AWT组件的所有更新和渲染,并执行事件侦听器中指定的行为(例如ActionListener#actionPerformed(ActionEvent)
)。 If you cause it to block (through sleeping or other forms of blocking), it won't be able to process updating and rendering. 如果您导致它阻塞(通过休眠或其他形式的阻塞),它将无法处理更新和渲染。 When you call setText
, the EDT needs to be able to adjust the text. 调用setText
,EDT需要能够调整文本。 You're preventing this by forcing it to sleep. 您可以通过强迫其进入睡眠状态来防止这种情况发生。
How to fix 怎么修
Spawn a new thread, have it wait 4 seconds, then have it dispose of your frame: 产生一个新线程,让它等待4秒钟,然后处置它:
Java 8+ Java 8+
public void actionPerformed(ActionEvent e) {
dogTalk.setText(...);
new Thread(() -> {
TimeUnit.SECONDS.sleep(4);
frame.dispose();
}).start();
}
Before Java 8 在Java 8之前
public void actionPerformed(ActionEvent e) {
dogTalk.setText(...);
new Thread(new Runnable() {
public void run() {
TimeUnit.SECONDS.sleep(4);
frame.dispose();
}
}).start();
}
Just right after dogTalk.setText(dogTalk.getText() + "\\nWow bone very wow much thanks bye.");
就在dogTalk.setText(dogTalk.getText() + "\\nWow bone very wow much thanks bye.");
put the following code : 输入以下代码:
dogTalk.revalidate();
dogTalk.repaint();
the following code works well i used Frame.update function it worked. 以下代码运行良好,我使用了工作正常的Frame.update函数。 in your case you have to update panel i guess 在您的情况下,您必须更新面板
dogTalk.setText(dogTalk.getText() + "\nWow bone very wow much thanks bye.");
frame.update(getGraphics());
try
{
TimeUnit.SECONDS.sleep(4);
}
catch ( InterruptedException e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
this is my full program 这是我的完整程序
public class TestSleep extends JFrame implements ActionListener{
JTextArea are=new JTextArea("asdjkfh");
JButton button=new JButton("Submit done");
public TestSleep()
{
are.setBounds(20, 20, 30, 10);
button.setBounds(10, 50, 20, 20);
this.add(are);
this.add(button);
button.addActionListener(this);
}
public static void main(String[] args)
{
TestSleep sleep=new TestSleep();
sleep.setLayout(new GridLayout());
sleep.setVisible(true);
sleep.setBounds(10, 10, 500, 280);
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Working");
are.setText(are.getText() + "\nWow bone very wow much thanks bye.");
this.update(getGraphics());
try
{
TimeUnit.SECONDS.sleep(4);
}
catch ( InterruptedException e1 )
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.