package Übungen;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.ArrayList;

public class Stopuhr extends JFrame {

    JPanel contentPane;
    JLabel zeige_zeit;
    JButton start_zeit;
    Timer timer;
    long startzeit;

    public Stopuhr() {
        this.setTitle("Stopuhr");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 498, 630);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        zeite_elemente();
        start_programm();     
        this.setVisible(true);
    }

    public void start_programm() {
        timer = new Timer(1000, e -> update_zeit());  
        start_zeit.addActionListener(e -> start_stopuhr());
    }

    public void start_stopuhr() {
    	  timer.start();
    	  startzeit = System.currentTimeMillis(); // Milisekunden        	
    }


    public void update_zeit() {
        double rest = (System.currentTimeMillis() - startzeit) / 1000.0;
        String formatiere_zahl = String.format("Zeit: %.1f s", rest);
        zeige_zeit.setText(formatiere_zahl);
        System.out.println(formatiere_zahl);
    }



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

    public void zeite_elemente() {
        zeige_zeit = new JLabel("Zeit: 0.0 s");
        zeige_zeit.setFont(new Font("Tahoma", Font.BOLD, 16));
        zeige_zeit.setBounds(10, 11, 450, 42);
        contentPane.add(zeige_zeit);
        start_zeit = new JButton("Start/Lap");
        start_zeit.setBounds(10, 484, 190, 54);
        contentPane.add(start_zeit);
    }
}