import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
//import java.util.TimerTask;
import javax.swing.Timer;
import javax.swing.*;
public class StopwatchFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = -1947098583246834192L;
JButton startButton; //启动按钮
JButton stopButton; //暂停按钮
JButton countButton; //完成按钮
JButton resetButton; //复位按钮
JLabel timeLabel;
JTextArea resultArea;
Timer myTimer;
int minute; //分
int second; //秒
int donknow; //秒后的单位
int numberFinish; //记录的次数
StopwatchFrame(String s)
{
super(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new CardLayout(4,4));
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel resultPanel = new JPanel(new CardLayout(4,4));
JPanel timePanel = new JPanel(new CardLayout(45,4));
//JPanel timeFlowPanel = new JPanel(new FlowLayout());
JPanel buttonCardPanel = new JPanel(new CardLayout(4,4));
JPanel buttonPanel = new JPanel(new GridLayout(4,1,0,3));
JPanel westPanel = new JPanel(new BorderLayout());
startButton = new JButton("开始/继续");
stopButton = new JButton("暂停");
countButton = new JButton("计数");
resetButton = new JButton("复位");
timeLabel = new JLabel("00:00:00");
timeLabel.setFont(new Font("微软雅黑",Font.PLAIN,35));
timeLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
timeLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
resultArea = new JTextArea();
resultArea.setEditable(true);
resultArea.setLineWrap(false);
resultArea.setColumns(10);
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
buttonPanel.add(resetButton);
buttonPanel.add(countButton);
buttonCardPanel.add(buttonPanel);
timePanel.add(timeLabel);
resultPanel.add(new JScrollPane(resultArea));
westPanel.add(buttonCardPanel,BorderLayout.CENTER);
mainPanel.add(timePanel,BorderLayout.NORTH);
mainPanel.add(westPanel,BorderLayout.CENTER);
mainPanel.add(resultPanel,BorderLayout.WEST);
contentPane.add(mainPanel);
myTimer = new Timer(10,this);
startButton.addActionListener(this);
stopButton.addActionListener(this);
countButton.addActionListener(this);
resetButton.addActionListener(this);
this.minute = 0;
this.second = 0;
this.donknow = 0;
this.numberFinish=0;
setSize(250,300);
setResizable(false); //禁止改变窗口大小
setLocationRelativeTo(null); // 在屏幕中心弹出窗口
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==startButton){
myTimer.start();
}
else if(e.getSource()==stopButton){
myTimer.stop();
}
else if(e.getSource()==countButton){
this.numberFinish++;
DecimalFormat myFormat = new DecimalFormat("00");
this.resultArea.append(" 第"+this.numberFinish+": "+myFormat.format(this.minute)+":"+
myFormat.format(this.second)+":"+myFormat.format(this.donknow)+"\r\n");
}
else if(e.getSource()==resetButton){
myTimer.stop();
this.minute = 0;
this.second = 0;
this.donknow = 0;
this.numberFinish=0;
timeLabel.setText("00:00:00");
//myTimer.start();
//resultArea.setText("");
}
else if(e.getSource()==myTimer)
{
this.donknow++;
this.second += this.donknow/100;
this.minute += this.second/60;
this.donknow = this.donknow%100;
this.second = this.second%60;
this.minute =this.minute%60;
DecimalFormat myFormat = new DecimalFormat("00");
this.timeLabel.setText(myFormat.format(this.minute)+":"+
myFormat.format(this.second)+":"+myFormat.format(this.donknow));
}
}
public static void main(String args[])
{
@SuppressWarnings("unused")
StopwatchFrame myFrame = new StopwatchFrame("秒表");
}
}