从ArrayList中每X秒更新一次JLabel< List> - Java [英] Update JLabel every X seconds from ArrayList<List> - Java

查看:171
本文介绍了从ArrayList中每X秒更新一次JLabel< 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.

有关如何轻松更新弹簧字词的建议吗?迭代通过我的列表,不知何故使用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创建的窗口,但是,我想让静态文本是动态的,而不是加载一个新的弹簧窗口。我想它闪一个字,消失,闪一下字消失。

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的任何建议?有什么重绘()?我画一个空白。

Any suggestions on how to update the JLabel? Something with repaint()? I am drawing a blank.

感谢!

UPDATE:
下面的亲切的人,我得到它正确打印到控制台。这是我的打印方法:

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();

    }

但是,它不更新lable吗?我的结构看起来像:

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中每X秒更新一次JLabel&lt; List&gt; - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆