从 ArrayList<List> 每 X 秒更新一次 JLabel- 爪哇 [英] Update JLabel every X seconds from ArrayList<List> - Java
问题描述
我有一个简单的 Java 程序,它读入一个文本文件,用"(空格)分割它,显示第一个单词,等待 2 秒,显示下一个……等等……我想这样做在 Spring 或其他一些 GUI 中.
I have a simple Java program that reads in a text file, splits it by " " (spaces), displays the first word, waits 2 seconds, displays the next... etc... I would like to do this in Spring or some other GUI.
关于如何使用 spring 轻松更新单词的任何建议?遍历我的列表并以某种方式使用 setText();
Any suggestions on how I can easily update the words with spring? Iterate through my list and somehow use setText();
我没有任何运气.我正在使用这种方法在控制台中打印出我的话,并将 JFrame 添加到其中......在控制台中效果很好,但会产生无穷无尽的 jframe.我在网上找到了大部分.
I am not having any luck. I am using this method to print my words out in the consol and added the JFrame to it... Works great in the consol, but puts out endless jframe. I found most of it online.
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 创建的窗口,但是,我希望静态文本是动态的,而不是加载一个新的 spring 窗口.我希望它闪一个字,消失,闪一个字消失.
I have a window that get's created with JFrame and JLable, however, I would like to have the static text be dynamic instead of loading a new spring window. I would like it to flash a word, disappear, flash a word disappear.
有关如何更新 JLabel 的任何建议?有 repaint() 的东西吗?我正在画一个空白.
Any suggestions on how to update the JLabel? Something with repaint()? I am drawing a blank.
谢谢!
更新:在以下好心人的帮助下,我已将其正确打印到控制台.这是我的打印方法:
UPDATE: With the help from the kind folks below, I have gotten it to print correctly to the console. Here is my Print Method:
private void printWords() {
final Timer timer = new Timer(500, null);
ActionListener listener = new ActionListener() {
private Iterator<Word> w = words.iterator();
@Override
public void actionPerformed(ActionEvent e) {
if (w.hasNext()) {
_textField.setText(w.next().getName());
//Prints to Console just Fine...
//System.out.println(w.next().getName());
}
else {
timer.stop();
}
}
};
timer.addActionListener(listener);
timer.start();
}
但是,它没有更新标签?我的构造函数看起来像:
However, it isn't updating the lable? My contructor looks like:
public TimeThis() {
_textField = new JTextField(5);
_textField.setEditable(false);
_textField.setFont(new Font("sansserif", Font.PLAIN, 30));
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(_textField);
this.setContentPane(content);
this.setTitle("Swing Timer");
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
//_textField.setText("loading...");
}
到达那里...一旦我或任何帮助我的人开始工作,我就会发布修复程序.再次感谢!
Getting there... I'll post the fix once I, or whomever assists me, get's it working. Thanks again!
推荐答案
首先,构建并显示您的 GUI.显示 GUI 后,使用 javax.swing.Timer 每 500 毫秒更新一次 GUI:
First, build and display your GUI. Once the GUI is displayed, use a javax.swing.Timer to update the GUI every 500 millis:
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<List> 每 X 秒更新一次 JLabel- 爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!