import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class Stoppuhr extends JFrame {
private DateFormat formatter;
private JLabel label;
private JButton button;
private Date start, stopp;
private final String START = "Start";
private final String STOPP = "Stopp";
public Stoppuhr(String title) {
super(title);
setSize(300, 150);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
label = new JLabel();
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label, BorderLayout.CENTER);
button = new JButton(START);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(START)) {
start = new Date();
label.setText("Zeit läuft...");
button.setText(STOPP);
}
else if(e.getActionCommand().equals(STOPP)) {
stopp = new Date();
button.setText(START);
long l = stopp.getTime() - start.getTime();
label.setText(formatter.format(new Date(l)));
}
}
});
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(button);
add(panel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Stoppuhr("Stoppuhr").setVisible(true);
}
}