From ArrayList – Java updates jlabel every x seconds

I have a simple java program, which reads a text file, splits it with "" (space), displays the first word, waits 2 seconds, displays the next... Etc... I want to perform this operation or some other GUI in spring

Any suggestions on how to easily update words with spring? Iterate over my list and use settext() in some way;

I have no luck I'm using this method to print my words in the console and add JFrame to it... It works well in the console, but it outputs endless jframes I found most of the content on the Internet

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

I have a window created with JFrame and jlable, but I want the static text to be dynamic instead of loading a new spring window I think it flashes a word, disappears, flashes a word, disappears

Any suggestions on how to update jlabel? What and redraw ()? I drew a blank

thank you!

Update: with the following kind help, I have made it print correctly to the console This is my printing 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 does not update the label? My constructor 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...");

}

Get there... Once I or anyone helps me, I'll release the fix and make it work Thanks again!

Solution

First, build and display the GUI After the GUI is displayed, use javax swing. Timer updates the GUI every 500 milliseconds:

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();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>