Java标签计时器和保存 [英] Java Label Timer and Saving

查看:147
本文介绍了Java标签计时器和保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我解释一下我想要做什么。

Let me explain what I am trying to do.

我有两个类扩展 JFrame StartJFrame TestingJFrame 。在main方法中,我启动了 StartJFrame 。它有一个按钮,开始。当按下它时,我将其设置为隐藏该帧并启动 TestingJFrame 。现在我在T estingJFrame 中没有任何东西。

I have two classes extending JFrame, the StartJFrame and TestingJFrame. In the main method, I start up a StartJFrame. It has a single button, start. When that is pressed, I have it set up to hide that frame and start up the TestingJFrame. Right now I don't have anything in the TestingJFrame.

在那个屏幕上,我希望有一个右下角的标签是一个计时器,从45秒开始计数到0。我还需要每隔10秒运行一些代码,并收集一些数据。 TestingJFrame 中将有两个按钮,是和否。当按下其中一个按钮时,它应该停止计时器并保存信息。

In that screen, I want to have a label in the bottom right corner that is a timer, starting on 45 seconds and counting down to 0. I also need to have some code run every 10th of a second, and collect some data. There will be two buttons in the TestingJFrame, Yes and No. When one of them is pressed, it should stop the timer and save the information.

数据基本上只是双倍数。每次运行程序时,我只会收集一次数据。我有一个 UserData 类,它包含有关测试主题的一些信息,以及一个双精度列表,它每10秒添加一次。我有一个大概的想法如何在java中保存数据。

The data is basically just doubles. I am only going to be collecting data once per run of the program. I have a UserData class that holds some information about the test subject, and a list of doubles, it is added to every 10th of a second. I have a general idea how to save the data in java.

我的问题是,我应该如何设置计时器,以便从45秒倒计时,当它达到0或用户按下是或否按钮时,它会调用一个函数来保存数据?我想我可以处理保存数据部分。

My question is, how should I set up the timer, so that it will count down from 45 seconds, and when it reaches 0 or the user presses the yes or no button it will call a function to save the data? I think I can handle the saving data part.

很抱歉,如果这很简单,我是Java的新手(来自c#),Swing让我有点困惑。

Sorry if this is really easy, I'm new to Java (from c#) and Swing has been confusing me a bit.

推荐答案

第一部分(显示倒计时和停止计时器)相对容易......

The first part (show the count down and stopping the timer) is relatively easy...

public class TimerTest01 {

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

    public TimerTest01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

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

            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private Timer timer;
        private long startTime;

        public TestPane() {
            setLayout(new GridLayout(0, 1));
            label = new JLabel("...");
            label.setHorizontalAlignment(JLabel.CENTER);
            final JButton btn = new JButton("Start");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (timer.isRunning()) {
                        timer.stop();
                        btn.setText("Start");
                    } else {
                        startTime = System.currentTimeMillis();
                        timer.restart();
                        btn.setText("Stop");
                    }
                    repaint();
                }
            });
            add(label);
            add(btn);

            timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    long endTime = (startTime + 45000);
                    long timeLeft = endTime - System.currentTimeMillis();
                    if (timeLeft < 0) {
                        timer.stop();
                        label.setText("Expired");
                        btn.setText("Start");
                    } else {
                        label.setText(Long.toString(timeLeft / 1000));
                    }
                    repaint();
                }
            });
        }
    }
}

看看 Swing Timer 获取更多信息

你问题的第二部分是含糊不清,可靠地为你提供答案。数据来自哪里?如何收集??

The second part of you question is to vague to reliably provide you with an answer. Where is the data coming from? How is collected??

这篇关于Java标签计时器和保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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