倒数从5倒数到0 [英] Swing Countdown from 5 to 0

查看:125
本文介绍了倒数从5倒数到0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Java Swing游戏,在每个新游戏发布之前,我都希望显示一个框架并从5到0秒倒数。

I am creating a Java Swing game and before every new game I would like to have a frame show up and countdown from 5 to 0 seconds.

这是碰巧的是,我希望后台的游戏能够等到倒计时完成。在后台等待游戏的最佳方法是什么?

While this is happening I would like the game in the background to wait until the countdown is complete. What would be the best way to make the game in the background wait?

我尝试了Thread.sleep,但这会导致Event Dispatch Thread进入睡眠状态,而GUI无法更新。但是,它在我第一次运行时有效,而第二次却没有。

I have tried Thread.sleep but this causes the The Event Dispatch Thread to sleep and the GUI not to update. However, it works the first time i run it but not the second.

感谢您的帮助。

public class CountdownPresenterPanel {
    JFrame mainFrame;
    int currentNumber = 5;
    JLabel textLabel;

    CountdownPresenterPanel() {
        mainFrame = new JFrame();
        textLabel = new JLabel(String.valueOf(currentNumber), SwingConstants.CENTER);
        textLabel.setFont(new Font("Arial", Font.BOLD, 55));
        textLabel.setVerticalAlignment(SwingConstants.CENTER);
        mainFrame.add(textLabel);
        mainFrame.setUndecorated(true); // Remove bar including close, minimize
        mainFrame.setSize(600,300);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
        Timer timer = new Timer(1000, new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) {
                if(currentNumber > 0) {
                    currentNumber--;
                    textLabel.setText(String.valueOf(currentNumber));
                } else {
                    mainFrame.setVisible(false);
                    mainFrame.dispose();
                }
            }
        });
        timer.setRepeats(true);
        timer.start();
    }
}

解决方案

import java.awt.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class CountdownPresenterPanel {
    private int currentNumber = 5;
    private JLabel textLabel;
    private final JDialog dialog;
    CountdownPresenterPanel() {
        Timer timer = new Timer(1000, new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) {
                if(currentNumber > 0) {
                    currentNumber--;
                    textLabel.setText(String.valueOf(currentNumber));
                } else {
                    dialog.dispose();
                }
            }
        });
        Frame window = new Frame();
        dialog = new JDialog(window, "Alert", true);
        textLabel = new JLabel(String.valueOf(currentNumber), SwingConstants.CENTER);
        textLabel.setFont(new Font("Arial", Font.BOLD, 55)); // Increase the font-size
        dialog.add(textLabel);
        dialog.setSize(400, 200);
        dialog.setLocationRelativeTo(null);
        timer.setRepeats(true);
        timer.start();
        dialog.setUndecorated(true);
        dialog.setVisible(true);
    }
}


推荐答案

Don '不使用 JFrame ,使用某种模式对话框

Don't use a JFrame, use some kind of modal dialog

看看如何制作对话框了解更多详情

这篇关于倒数从5倒数到0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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