【问题标题】:How To Reset a Timer in Java GUI and Display Message after Stop如何在 Java GUI 中重置计时器并在停止后显示消息
【发布时间】:2017-03-20 11:33:10
【问题描述】:

您好,您好,我想创建一个“重置按钮”,我的计时器将在其中重置。我创建了一个新按钮并将其命名为“reset”,我使用代码“tm2.restart();”但它在我创建的新按钮中不起作用。这是我的代码:

import javax.swing.Timer;
public class deploy extends JFrame {

 private int seconds;
 private SimpleDateFormat df;
 private boolean isRunning;
 private JLabel lblTimer1;
 private JButton btnStart1;

public deploy() {

lblTimer1 = new JLabel("New label");
lblTimer1.setForeground(Color.WHITE);
lblTimer1.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblTimer1.setBounds(100, 231, 94, 16);
contentPane.add(lblTimer1);

 Timer tm2 = new Timer(1000, new ActionListener() {

           @Override
            public void actionPerformed(ActionEvent e) {
                setTimer();
                seconds++;
            }
        });

btnStart1 = new JButton("Start");
btnStart1.setBackground(Color.LIGHT_GRAY);
btnStart1.setForeground(Color.BLUE);
btnStart1.addActionListener(new ActionListener() {
     @Override
           public void actionPerformed(ActionEvent e) {

                if(isRunning) {
                    tm2.stop();
                    btnStart1.setText("Start");
                }else {
                    tm2.start();
                    btnStart1.setText("Stop");
                }

                isRunning = !isRunning;
            }
        });

我的计时器被格式化为“SimpleDateFormat”(00:00:00),因为我正在创建一个“网吧管理应用程序”,客户将走进并记录他/她的时间,直到他/她注销并且显示他/她将被还清的金额的消息。请帮忙。谢谢

【问题讨论】:

  • 我在该代码中没有看到tm2.restart() ...请提供minimal reproducible example。
  • 顺便说一句,使用 Timer 跟踪时间跨度并不是最好的主意……您可以简单地记录 2 个时间戳(登录时、注销时)并在需要时计算差异。
  • @Fildor 我不包含格式为 tm2.restart(); 的代码我删除,因为它不工作..
  • 啊好吧。那我应该使用什么代码?我是 Java GUI 新手...
  • 您始终可以计算从现在到过去某个时间的实际时间跨度。有多种方法可以在显示经过时间的标签上实现第二次更新。

标签: java swing user-interface timer


【解决方案1】:

查看代码,并毫不犹豫地询问不清楚的地方:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Deploy extends JFrame {

    private int seconds;
    private SimpleDateFormat df;
    private JLabel lblTimer;
    private Timer timer;
    private  JButton startButton;

    public Deploy() {

        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());

        lblTimer = new JLabel();
        lblTimer.setForeground(Color.WHITE);
        lblTimer.setFont(new Font("Tahoma", Font.PLAIN, 20));
        lblTimer.setPreferredSize(new Dimension(100,30));
        contentPane.add(lblTimer,BorderLayout.NORTH);

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setTimer();
                seconds++;
            }
        });

        JPanel buttonsPanel = new JPanel();
        contentPane.add(buttonsPanel, BorderLayout.SOUTH);

        startButton = new JButton("Start");
        startButton.setBackground(Color.LIGHT_GRAY);
        startButton.setForeground(Color.BLUE);
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(timer.isRunning()) {
                    timer.stop();
                    startButton.setText("Start");
                }else {
                    timer.start();
                    startButton.setText("Stop");
                }
            }
        });

        startButton.setPreferredSize(new Dimension(100,30));
        buttonsPanel.add(startButton);

        JButton resetButton = new JButton("Reset");
        resetButton.setBackground(Color.LIGHT_GRAY);
        resetButton.setForeground(Color.RED);
        resetButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                resetTimer();
            }
        });
        resetButton.setPreferredSize(new Dimension(100,30));
        buttonsPanel.add(resetButton);

        df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23
        df.setTimeZone(TimeZone.getTimeZone("GMT"));

        resetTimer();
        pack();
        setVisible(true);
    }

    private void setTimer() {
        Date d = new Date(seconds * 1000L);
        String time = df.format(d);
        lblTimer.setText(time);
    }

    private void resetTimer() {

        if(timer.isRunning()) {
            timer.stop();
        }
        startButton.setText("Start");
        seconds = 0;
        setTimer();
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多