日期:2014-05-20 浏览次数:21014 次
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
public class StopWatch extends JFrame
//implements Runnable,
// ActionListener
{
private JTextField text_time;
private JButton button_start,button_continue,button_stop;
private JLabel label;
private Thread thread;
private JPanel jpanel1,jpanel2;
private double time = 0.00;
private Timer timer;
public StopWatch()
{
super("秒表");
this.setBounds(240,300,240,100);
this.setBackground(java.awt.Color.lightGray);
this.setResizable(false);
this.getContentPane().setLayout(new GridLayout(2,1));
text_time = new JTextField("0.00",10);
label = new JLabel("秒");
jpanel1 = new JPanel();
jpanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
jpanel1.add(text_time);
jpanel1.add(label);
this.getContentPane().add(jpanel1);
button_start = new JButton("start");
button_continue = new JButton("continue");
button_stop = new JButton("stop");
jpanel2 = new JPanel();
jpanel2.add(button_start);
jpanel2.add(button_continue);
jpanel2.add(button_stop);
this.getContentPane().add(jpanel2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
timer = new Timer(10,new ActionListener(){
DecimalFormat formatter = new DecimalFormat("0.00");
public void actionPerformed(final ActionEvent e){
time += 0.01;
text_time.setText(formatter.format(time));
}
});
button_start.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
time = 0.00;
timer.start();
}
});
button_stop.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
timer.stop();
}
});
button_continue.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
timer.restart();
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new java.lang.Runnable(){
@Override public void run(){
new StopWatch();
}
});
}
}