从ArrayList每隔X秒更新JLabel<List>-Java
我有一个简单的Java程序,该程序读取一个文本文件,将其分隔为“”(空格),显示第一个单词,等待2秒,显示下一个…等等…我想在Spring或其他一些GUI。
关于如何使用spring轻松更新单词的任何建议?遍历我的列表并以某种方式使用setText();
我没有运气。我正在使用此方法在consol中打印我的单词,并向其中添加JFrame …在consol中效果很好,但是却发出了无尽的jframe。我在网上找到了大部分。
private void printWords() { for (int i = 0; i < words.size(); i++) {
//How many words?
//System.out.print(words.size());
//print each word on a new line...
Word w = words.get(i);
System.out.println(w.name);
//pause between each word.
try{
Thread.sleep(500);
}
catch(InterruptedException e){
e.printStackTrace();
}
JFrame frame = new JFrame("Run Text File");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel(w.name,SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
//Display the window. frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
我有一个使用JFrame和JLable创建的窗口,但是,我希望静态文本是动态的,而不是加载新的弹簧窗口。我希望它闪烁一个字,消失,闪烁一个字消失。
关于如何更新JLabel的任何建议?有repaint()吗?我在画空白。
回答:
首先,构建并显示你的GUI。显示GUI后,使用javax.swing.Timer每500毫秒更新一次GUI:
final Timer timer = new Timer(500, null);ActionListener listener = new ActionListsner() {
private Iterator<Word> it = words.iterator();
@Override
public void actionPerformed(ActionEvent e) {
if (it.hasNext()) {
label.setText(it.next().getName());
}
else {
timer.stop();
}
}
};
timer.addActionListener(listener);
timer.start();
以上是 从ArrayList每隔X秒更新JLabel<List>-Java 的全部内容, 来源链接: utcz.com/qa/413839.html