Ich weiß nicht, wie der Timer angezeigt wird, wenn er gestoppt ist

Wie ist es bei euch? Ich habe ein kleines Problem mit meinem Code. Wenn ich mein Timerprogramm stoppe, wird "Fertig!" da ich es auf "Done" gesetzt habe, aber ich möchte die verbleibende Zeit anstelle des Wortes "Done" anzeigen. Wie zum Beispiel, wenn ich die ursprüngliche Zeit auf 4000 Sekunden eingestellt habe und bei 3000 Sekunden stehen geblieben bin, möchte ich anstelle des Wortes "Fertig" die aktuelle Zeit anzeigen, die 3000 ist, wenn ich sie stoppe. Könnt ihr mir helfen? Ich würde dies sehr begrüssen

Hier ist mein Code:

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
public class CountdownTimer extends JFrame  {

    JLabel promptLabel, timerLabel;
    int counter;
    JLabel tf;
    JButton button;
    Timer timer;


    public static void main(String args[]) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CountdownTimer();
            }

        });

    }


    public CountdownTimer() {

        JFrame lt = new JFrame();
            lt.getContentPane().setBackground(new Color(0, 102, 153));
            lt.setBounds(100, 100, 450, 300);
            lt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lt.getContentPane().setLayout(null);
            lt.setVisible(true);

        promptLabel = new JLabel("Time: ", SwingConstants.CENTER);
        promptLabel.setFont(new Font("Modern No. 20", Font.BOLD, 17));
        promptLabel.setBounds(21, 30, 210, 30);
        lt.add(promptLabel);

        tf = new JLabel("4000");
        tf.setBounds(160, 36, 40, 20);
        lt.add(tf);

        button = new JButton("Start");
        button.setFont(new Font("Modern No. 20", Font.BOLD, 11));
        button.setBounds(299, 37, 89, 23);
        lt.add(button);

        JButton btnLogout = new JButton("LOGOUT");

        btnLogout.setBackground(new Color(112, 128, 144));
        btnLogout.setFont(new Font("Trajan Pro", Font.BOLD, 17));
        btnLogout.setBounds(149, 189, 121, 36);
        btnLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 String sbutt = e.getActionCommand();

                 if(sbutt.equals("LOGOUT")) {


                     long current = counter;
                     long elapsed = current - counter;

                        timer.stop();
                        timerLabel.setText("Done!");
                        Toolkit.getDefaultToolkit().beep();
                        tf.setText(String.valueOf(elapsed));

                 }
            }
        });
        lt.getContentPane().add(btnLogout);

        timerLabel = new JLabel("Waiting...", SwingConstants.CENTER);
        timerLabel.setFont(new Font("Modern No. 20", Font.BOLD, 17));
        timerLabel.setBounds(119, 97, 176, 62);
        lt.getContentPane().add(timerLabel);

        event e =  new event();
        button.addActionListener(e);
    }
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public class event implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int count = (int)(Double.parseDouble(tf.getText()));
            timerLabel.setText("Time left: " + count);
            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            timer.start();
        }
    }
    public class TimeClass implements ActionListener {
        int counter;
        public TimeClass(int counter) {
            this.counter= counter;
   }
        public void actionPerformed(ActionEvent tc) {
            counter--;
            if(counter >= 1) {
                timerLabel.setText("Time left: " + counter);
            }
            else {
                timer.stop();
                timerLabel.setText("Done!");
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage