java - 在 Java GUI 中延迟操作的任何方法

标签 java swing jtextarea

<分区>

我正在为一个大学作业制作一个扑克游戏,我想知道是否有任何方法可以实现如下的方法(注意:非常粗略地编写代码)

JTextArea textArea = new JTextArea();

public void printer(String s){
    //I want to delay here, for 2 seconds each time i print to the jtextarea
    textArea.append(s);
}

public void runGame(){
    printer("Dealing cards...");
    //I want to delay to add to an effect of the time it takes to actually deal the cards
    pokerHand.setVisibility(true);
    printer("What you like to do?");

    //
    //Code here containing running the game
    //

    printer("Daniel Negreanu folds");
    //I want to have a delay here for the time it takes to make a decision.
    printer("Phil Hellmuth folds");

我想在整个程序中使用它的实例还有很多,只是想知道是否有任何方法可以做到这一点。

提前致谢

编辑:不打算使用 Thread.sleep() 因为它不能很好地与 gui 一起工作。 EDIT2:我希望代码中的 pokerHand.setVisibility(true) 和其他方法在延迟后执行(使用计时器不会这样做)。

最佳答案

Not looking to use Thread.sleep() as it doesn't work well with gui.

很好,改用 Swing Timer

I want the pokerHand.setVisibility(true), and other methods in my code to execute AFTER the delay, ( using a timer doesn't do this ).

是的,你只是没有正确使用它,但由于你没有提供任何实际代码,我不能说“如何”你没有正确使用它,只能从它的声音,你是。

先看看 How to use Swing Timers了解更多详情

下面是一个非常简单的例子,它使用了一个Timer来计算和打印你点击按钮之间的时间量。

示例在 Timer 启动后更新 UI,但在 Timer 完成之前,不会生成计算和结果

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea ta;       
        private JButton btn;

        private Timer timer;

        private LocalTime startTime, endTime;

        public TestPane() {
            setLayout(new BorderLayout());
            ta = new JTextArea(10, 20);
            add(new JScrollPane(ta));
            btn = new JButton("Click");
            add(btn, BorderLayout.SOUTH);

            timer = new Timer(2000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    endTime = LocalTime.now();
                    Duration duration = Duration.between(startTime, endTime);
                    ta.append("Ended @ " + endTime + "\n");
                    ta.append("Took " + (duration.toMillis() / 1000) + " seconds\n");
                }
            });
            timer.setRepeats(false);

            ta.setEditable(false);

            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.start();
                    startTime = LocalTime.now();
                    btn.setEnabled(false);
                    ta.append("Start @ " + startTime + "\n");
                }
            });
        }

    }

}

关于java - 在 Java GUI 中延迟操作的任何方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37521419/

相关文章:

java - 如何将 blob 图像添加到 pdf 文件?

java - mvn 发布 :prepare doesn't catch that I have modified files?

java - 如何将两个以 .docx 格式保存的 word 文档合并到第三个文件中?

java - 是否可以将 HTML 加载到 jTable 中?

java - 我们是否可以在框架提供的 ActionMap 中找到 Actions 的实际代码?

java - 在 jcombobox 中添加年份列表

java - JTextArea 无滚动条

java - 我可以在抽象类中创建一个为实例化类构造实例的方法吗?

Java 日志 ==> JTextArea

java - 如何在 JTextArea 中打印循环的结果?