http://changer0702.javaeye.com/blog/482346
学习操作系统课程的时候提到了一个非常重要的概念thread(线程),为了加深理解老师要求课后用java写一个秒表的小程序,经过两个小时的努力终于写完了,界面截图如下所示!
实体类StopWatch
- public class StopWatch {
- private int hour;
- private int minute;
- private double second;
- //以一100毫秒为单位设置时间
- public void setTime(float t){
- hour= (int)t/36000;
- minute = ((int)t-hour*36000)/600;
- second = (t%600)/10.00;
- }
- public String toString(){
- return String.format("%02d : %02d : %04.1f", hour,minute,second);
- }
- }
GUI类StopWatchGUI
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class StopWatchGUI extends JPanel{
- private JPanel centerPanel;
- private JPanel southPanel;
- private JLabel label;
- private JButton start;
- private JButton stop;
- private JButton reset;
- private boolean flag=false;
- private float time=0;
- private StopWatch watch;
- public StopWatchGUI(){
- setSouthPanel();
- setCenterPanel();
- this.setLayout(new BorderLayout());
- this.add(centerPanel,BorderLayout.CENTER);
- this.add(southPanel,BorderLayout.SOUTH);
- watch=new StopWatch();
- new Thread(){
- public void run(){
- while(true){
- if(flag){//start the stopWatch
- watch.setTime(time);
- label.setText(watch.toString());
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- time+=1;
- }
- }
- }
- }.start();
- }
- private void setCenterPanel(){
- centerPanel=new JPanel();
- label=new JLabel("00 : 00 : 00.0",JLabel.CENTER);
- label.setFont(new Font("Serif", Font.BOLD, 30));
- centerPanel.add(label);
- }
- private void setSouthPanel(){
- southPanel=new JPanel();
- start=new JButton("Start");
- stop=new JButton("Stop");
- reset=new JButton("Reset");
- start.addActionListener(new StartListener());
- stop.addActionListener(new StopListener());
- reset.addActionListener(new ResetListener());
- southPanel.setLayout(new FlowLayout());
- southPanel.add(start);
- southPanel.add(stop);
- southPanel.add(reset);
- }
- public static void main(String[] args) {
- JFrame frame=new JFrame("StopWatch");
- frame.setContentPane(new StopWatchGUI());
- frame.setSize(300,120);
- Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
- Dimension frameSize=frame.getSize();
- frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- class StartListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- flag=true;
- start.setEnabled(false);
- stop.setEnabled(true);
- reset.setEnabled(false);
- }
- }
- class StopListener implements ActionListener{
- public void actionPerformed(ActionEvent event) {
- flag=false;
- start.setText("Start");
- start.setEnabled(true);
- reset.setEnabled(true);
- stop.setEnabled(false);
- }
- }
- class ResetListener implements ActionListener{
- public void actionPerformed(ActionEvent event) {
- flag=false;
- time=0;
- watch.setTime(time);
- label.setText(watch.toString());
- start.setEnabled(true);
- stop.setEnabled(true);
- }
- }
- }