不要使用循环或
Thread.sleep。只需使用即可
javax.swing.Timer。以下将导致每1000毫秒发生30 次迭代
。您可以相应地调整代码,使其适应
actionPerformed每隔毫秒发生一次的情况。
int count = 0;...Timer timer = new Timer(1000, new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ if (count == 30) { ((Timer)e.getSource()).stop(); } else { int rand = (int) (Math.random()* RandomNames.size); stars.setText(RandomNames.list.get(rand)); count++; } }});timer.start();如果需要,您可以
Timer在构造函数中设置,并
start()在
actionPerformed另一个按钮的侦听器的中设置它。
在如何使用Swing计时器中查看更多信息



