import java awt FlowLayout import java awt event ActionEvent import ja

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.xml.bind.Unmarshaller.Listener;
public class StopWatch {
JLabel label;
long start;
public StopWatch() {
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent a2) {
Calendar cal = Calendar.getInstance();
if (a2.getActionCommand().equals("Start")) {
start = cal.getTimeInMillis();
label.setText("Running");
} else
label.setText("Elapsed time is "
+ (double) (cal.getTimeInMillis() - start));
}
};
JFrame frame = new JFrame("StopWatch");
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(230, 90);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonStart = new JButton("Start");
buttonStart.addActionListener(listener);
frame.add(buttonStart);
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(listener);
frame.add(stopButton);
JLabel label = new JLabel("Press to start");
frame.add(label);
JTextField textField = new JTextField(10);
frame.add(textField);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}