在过去的几个月里,我尝试过用 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);
}