java - jlabel 更改其文本之前计时器暂停

标签 java swing timer

lblCompThoughts.setText("this is the first text");
lblCompThoughts.setText("this is the second text");
lblCompThoughts.setText("this is the third text");

我的代码中发生的情况是第三个文本是唯一的打印文本。如何让第一个文本出现然后暂停2秒然后出现第二个文本然后再暂停2秒然后显示第三个文本。

最佳答案

一种可能的方法是这样的:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.*;
import java.util.Timer;

import javax.swing.*;

public class StackOverflow {

private final List<JLabel> arrayList = Arrays.asList(new JLabel("This is the first text."), new JLabel("This is the second text."), new JLabel("And this is the third and last text."));
private JFrame frame;
private JButton button;

public static void main(final String[] arguments) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {    
    StackOverflow clazz = new StackOverflow();
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    clazz.create("Stackoverflow | Question", 350, 200);
    clazz.doLayout();
}

public void create(final String title, final int width, final int height) {
    this.frame = new JFrame(title);
    EventQueue.invokeLater(() -> {
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.getContentPane().setBackground(Color.WHITE);
        frame.setVisible(true);
    });
}

public void doLayout() {
    this.button = new JButton("Answer question");
    EventQueue.invokeLater(() -> {
        button.addActionListener(new ActionListener() {
            int index = 0;
            @Override
            public void actionPerformed(final ActionEvent action) {
                // Important: This is the java.util.Timer and not the javax.swing.Timer class.
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        if (index + 1 >= arrayList.size()) {
                            timer.cancel();
                        }
                        EventQueue.invokeLater(() -> {
                            frame.getContentPane().add(arrayList.get((index += 1) - 1));
                            frame.getContentPane().validate();
                        });
                    }
                }, 0l, 2000l);
            }
        });
        frame.add(button);
    });
}
}

希望这能回答您的问题。如果确实如此,请向我展示并投票。

关于java - jlabel 更改其文本之前计时器暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47607075/

相关文章:

java - 如何访问静态变量Java的更改值

java - 为什么 trim() 不删除此示例中的尾随空格?

java - 从ArrayList获取自定义类

java - ASTParser 类的 setEnvironment 方法未定义

java - 如何向 JTable 添加 Action 监听器?

java - AWT(扩展)修饰符何时保证有效?

java - 将事件监听器放在不同的类中会更好吗?

php - PHP和MYSQL游戏计时器比较

java - 我怎样才能停止定时器?

c - 计时器在 C 中的函数调用中查找耗时