本文介绍了是否有在 JFrame 中生成计时器的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作游戏.在 BorderLayout(North) 中,我想在您玩游戏时显示一个计时器.当你输了,在一个JOptionPane.showMessageDialog 显示播放的时间.当您获胜时,在 JOptionPane.showMessageDialog 中显示获胜的最佳时间.

I am making a game. In a BorderLayout(North) I want to show a timer while you are playing. When you lose, in a JOptionPane.showMessageDialog show the time playing. When you win, show in a JOptionPane.showMessageDialog the best time of the wins.

我知道如何使用JOptionPane.showMessageDialog,但我不知道java中有没有方法可以生成定时器,并获取值、设置值等.谢谢!如果有人需要更多信息或其他东西,请告诉我...

I know how to use the JOptionPane.showMessageDialog, but I don't know if there is a method in java to generate a timer, and get the values, set values, etc.Thanks! If someone needs more info or something, please let me know...

推荐答案

一些注意事项...

Swing 是一个单线程环境,也就是说,所有与 UI 的交互都应该在事件调度线程的上下文中执行.同样,您永远不应该阻止 EDT,因为这会阻止它处理新事件,包括重绘请求.

Swing is a single threaded environment, that is, all interactions with the UI are expected to be executed from within the context of the Event Dispatching Thread. Equally, you should never block the EDT as this will prevent it from processing new events, including repaint requests.

为此,最简单的方法是使用诸如 javax.swing.Timer 之类的东西,它允许您安排定期重复回调,这些回调在美国东部时间

To that end, the simplest approach would be to use something like a javax.swing.Timer which allows you to schedule a repeating call back on a regular interval which are executed within the context of the EDT

有关详细信息,请参阅如何使用计时器...

See How to use Timers for more details...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CountDownTimer {

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

    public CountDownTimer() {
        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 static class TestPane extends JPanel {

        private JLabel label;
        private long startTime = -1;
        private long timeOut = 10;

        public TestPane() {

            label = new JLabel("...");
            final Timer timer = new Timer(500, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {


                    if (startTime == -1) {
                        startTime = System.nanoTime();
                    } else {
                        long endTime = startTime + TimeUnit.SECONDS.toNanos(10);
                        long time = System.nanoTime();

                        if (time < endTime) {
                            long timeLeft = (endTime - time);
                            label.setText(Long.toString(TimeUnit.NANOSECONDS.toSeconds(timeLeft)) + " seconds");
                        } else {
                            label.setText("Time out");
                            ((Timer) e.getSource()).stop();
                        }
                        revalidate();
                        repaint();
                    }
                }
            });
            timer.setInitialDelay(0);
            timer.start();

            setLayout(new GridBagLayout());
            add(label);

        }

    }

}

这篇关于是否有在 JFrame 中生成计时器的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:25