java实现秒表功能

 更新时间:2022年09月11日 08:27:02   作者:Jiafu_Liu  
这篇文章主要为大家详细介绍了java实现秒表功能,利用javax.swing.Timer类设计实现秒表应用程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

利用javax.swing.Timer类设计并实现一个模拟秒表功能的应用程序。程序中显示不断递增的时间,同时包含允许用户启动和终止计时功能的代码,以及一个可将时间复位为0的按钮。

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout; 
import java.util.TimerTask;  
import java.text.DecimalFormat;
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Font;

public class stopWatch extends JPanel{  
    private JLabel currentTimeLabel; //显示标签 
    private JButton startJButton;    //开始按钮
    private JButton stopJButton;     //停止按钮
    private JButton resetJButton;    //复位按钮
    private long countMis,countSec,countMin,countHour;//计时变量
    private DecimalFormat textFormat=new DecimalFormat("00");//格式化输出
    Timer timer=new Timer(10,new TestActionListener());//计时单位10ms

    public stopWatch() {  
        JPanel panel=new JPanel(new GridLayout(1,3,5,10)); //网格布局嵌入按钮
        JPanel panel2=new JPanel(); 
        currentTimeLabel=new JLabel(" "); 
        TestActionListener actionListener=new TestActionListener();
        currentTimeLabel.setForeground(Color.blue);
        currentTimeLabel.setFont(new Font("SAN_SERIF",Font.BOLD,50));  

        startJButton=new JButton("Start"); 
        stopJButton=new JButton("Stop"); 
        resetJButton=new JButton("Reset"); 
        //设置JButton相关属性
        startJButton.setBorder(BorderFactory.createRaisedBevelBorder());
        stopJButton.setBorder(BorderFactory.createRaisedBevelBorder());
        resetJButton.setBorder(BorderFactory.createRaisedBevelBorder());

        startJButton.setFont(new java.awt.Font("Times New Roman", 1, 30));
        stopJButton.setFont(new java.awt.Font("Times New Roman", 1, 30));
        resetJButton.setFont(new java.awt.Font("Times New Roman", 1, 30));

        stopJButton.setBackground(Color.cyan); 
        startJButton.setBackground(Color.red);
        resetJButton.setBackground(Color.orange);

        stopJButton.addActionListener(actionListener);  
        startJButton.addActionListener(actionListener);  
        resetJButton.addActionListener(actionListener);  

        this.setLayout(new BorderLayout());  

        panel2.setBackground(Color.gray);
        panel2.setBorder(BorderFactory.createLoweredBevelBorder());  
        panel2.add(currentTimeLabel); 
        panel.add(stopJButton);  
        panel.add(startJButton);  
        panel.add(resetJButton); 

        this.add(panel2,BorderLayout.NORTH); 
        this.add(panel,BorderLayout.CENTER);

    }  
    //处理相关事件
    class TestActionListener implements ActionListener{   
        public void actionPerformed(ActionEvent e){ 
            if(e.getSource()==startJButton){
                timer.start();
                startJButton.setEnabled(false);
            }  

            else if(e.getSource()==stopJButton){
                timer.stop();
                startJButton.setEnabled(true);
            }

            else if(e.getSource()==resetJButton){ 
                countHour=0;
                countMin=0;
                countSec=0;
                countMis=0;
            }

            else{//满位后复位
                countMis++;
                if(countMis>=99){
                    countSec++;
                    countMis=0;
                    if(countSec>=59){
                        countMin++;
                        countSec=0;
                        if(countMin>=59){
                            countHour++;
                            countMin=0;
                        }
                    }
                }

            }

        }
    }


    public void paintComponent(Graphics g){
        super.paintComponent(g); 
        currentTimeLabel.setText(textFormat.format(countHour)+":"+textFormat.format(countMin)+
        ":"+textFormat.format(countSec)+":"+textFormat.format(countMis));
        repaint();  
    }


    public static void main(String args[]){  
        JFrame frame=new JFrame("秒表演示");  
        stopWatch stopwatch=new stopWatch();  
        frame.setSize(480,280);
        frame.getContentPane().add(stopwatch);  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setVisible(true);  
    }  
}  

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java生成随机数之Random与ThreadLocalRandom性能比较详解

    Java生成随机数之Random与ThreadLocalRandom性能比较详解

    大家项目中如果有生成随机数的需求,我想大多都会选择使用Random来实现,它内部使用了CAS来实现。 实际上,JDK1.7之后,提供了另外一个生成随机数的类ThreadLocalRandom,那么他们二者之间的性能是怎么样的呢?本文就来详细说说
    2022-12-12
  • springMvc注解之@ResponseBody和@RequestBody详解

    springMvc注解之@ResponseBody和@RequestBody详解

    本篇文章主要介绍了springMvc注解之@ResponseBody和@RequestBody详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 简单介绍java中equals以及==的用法

    简单介绍java中equals以及==的用法

    这篇文章主要介绍了简单介绍java中equals以及==的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 浅谈让@Value更方便的Spring自定义转换类

    浅谈让@Value更方便的Spring自定义转换类

    Spring为大家内置了不少开箱即用的转换类,如字符串转数字、字符串转时间等,但有时候需要使用自定义的属性,则需要自定义转换类了
    2021-06-06
  • Java线程状态变换过程代码解析

    Java线程状态变换过程代码解析

    这篇文章主要介绍了Java线程状态变换过程代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • java如何自定义List中的sort()排序,用于日期排序

    java如何自定义List中的sort()排序,用于日期排序

    这篇文章主要介绍了java如何自定义List中的sort()排序,用于日期排序,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot内置tomcat启动原理详解

    SpringBoot内置tomcat启动原理详解

    这篇文章主要介绍了SpringBoot内置tomcat启动原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 容器环境的JVM内存设置实践记录

    容器环境的JVM内存设置实践记录

    Docker和K8S的兴起,很多服务已经运行在容器环境,对于java程序,JVM设置是一个重要的环节,这里总结下我们项目里的最佳实践,对容器环境的JVM内存相关知识感兴趣的朋友一起看看吧
    2022-03-03
  • redis发布订阅Java代码实现过程解析

    redis发布订阅Java代码实现过程解析

    这篇文章主要介绍了redis发布订阅Java代码实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • 关于java开发的性能问题总结(必看)

    关于java开发的性能问题总结(必看)

    下面小编就为大家带来一篇关于java开发的性能问题总结(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03

最新评论