below in the sample code which i wrote where the user gets an interface with 2 buttons. when the user click on start button the timer starts and when the end button is clicked the timer stops and the difference in time is displayed. But the difference in time is not being output:(
一个人能够帮助。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Timer2 extends JFrame {
private JButton start;
private JButton end;
public Timer2() {
super("Test Timer");
setLayout(new FlowLayout());
start = new JButton("START");
add(start);
end = new JButton("END");
add(end);
ButtonHandler handler = new ButtonHandler();
start.addActionListener(handler);
end.addActionListener(handler);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
long s_time = 0;
long e_time = 0;
long diff = 0;
String name = ((JButton) event.getSource()).getText();
if (name.equals("start")) {
s_time = System.currentTimeMillis();
} else {
e_time = System.currentTimeMillis();
}
diff = (e_time - s_time) / 1000;
JOptionPane.showMessageDialog(null, diff);
}
}
public static void main(String[] args) {
Timer2 timer2 = new Timer2();
timer2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timer2.setSize(200, 200);
timer2.setVisible(true);
}
}