package org.stopwatch;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.*;
import javax.swing.JFrame;
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;
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_start.addActionListener(this);
button_continue = new JButton("continue");
button_continue.addActionListener(this);
button_stop = new JButton("stop");
button_stop.addActionListener(this);
jpanel2 = new JPanel();
jpanel2.add(button_start);
jpanel2.add(button_continue);
jpanel2.add(button_stop);
this.getContentPane().add(jpanel2);
this.setVisible(true);
}
public void run() {
time = 0.00;
DecimalFormat df = new DecimalFormat("0.00");
//String num = df.format(time);
while(true)
try
{
Thread.sleep(10);
time = time+0.01;
text_time.setText(""+ df.format(time));
}
catch(InterruptedException e)
{
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button_start)
{
thread = new Thread(this);
thread.start();
button_start.setEnabled(false);
button_stop.setEnabled(true);
}
if(e.getSource() ==button_continue)
{
thread.resume();//这个说已经过时
button_continue.setEnabled(false);
button_stop.setEnabled(true);
}
if(e.getSource() == button_stop)
{
thread.suspend();
button_stop.setEnabled(false);
button_continue.setEnabled(true);
button_start.setEnabled(true);
//catch(InterruptedException ie)//
{//
}//
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new StopWatch();
}
}
------解决方案--------------------
额。。。刚刚的代码写错了,应该是
boolean falg = true;
stop()那里改成false才是。。。
------解决方案--------------------
使用 javax.swing.Timer
- Java code
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(); } }); }}