【发布时间】:2012-12-17 22:00:45
【问题描述】:
在过去的几个月里,我尝试过使用 Java 进行编程,并且大部分都没有遇到任何问题,但现在我在处理 void 时遇到了一些麻烦。在我的程序中,用户按下一个按钮,目标是在 JLabel 上显示多条消息,这些消息使用 Thread.sleep() 方法展开。由于某种原因,只有最后一个最终被发送。这是我的代码。这不是全部,但我很确定问题出在此处。那里的错误输出是为了尝试查看代码中发生了什么,但是,显然它们最终没有工作。
private class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try {
if (e.getSource() == exitButton)
System.exit(0);
else if (e.getSource() == button1)
alValue = "This is the new message text.";
System.err.println(alValue);
createNewArrayList();
Thread.sleep(3000);
alValue = "Back to invisible...";
System.err.println(alValue);
createNewArrayList();
Thread.sleep(2000);
alValue = "";
System.err.println(alValue);
createNewArrayList();
} catch (InterruptedException ex) {
Logger.getLogger(EmptySpace.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
和
private void createNewArrayList() {
ArrayList al = new ArrayList();
al.add(alValue);
label1.setText("" + al);
}
【问题讨论】:
-
您所说的“处理空隙”是什么意思?
-
您是否得到“返回隐形...”值显示?
-
@Amar,不,我没有在 JLabel 上显示该值。只显示最后一个。
标签: java swing concurrency jlabel event-dispatch-thread