【发布时间】:2015-06-24 22:16:39
【问题描述】:
我点击一个 JButton,我应该得到下面的最终输出,在一个 JTextField 中:
01234567
我想设置一个 Timer,这样每个数字的结果都会慢慢显示出来。
例如(在 JTextField 中),我希望的结果应该是: 0(1 秒后) 01(1 秒后) 012(1 秒后) 0123 .... 01234567 (JTextField 中的输出是 01234567)
我目前正在使用 Thread.sleep,但没有得到我想要的结果。 我首先点击 JButton: (1 秒后) 01234567
我目前正在使用代码
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
textfield.setText("");
for (int i=0; i<8; i++)
{
textfield.setText(i);
Thread.sleep(1000);
}
}
catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
有没有办法在不更改“button.addActionListener(new ActionListener()......”的情况下使用 Timer??(如果我使用 Timer,我希望不使用 Thread.sleep)
【问题讨论】:
标签: java swing timer jtextfield sleep