java实现秒表

    利用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);  
    }  
}  

运行结果:
这里写图片描述

  • 9
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
以下Java实现秒表计时器的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Stopwatch implements ActionListener { private JFrame frame; private JLabel timeLabel; private JButton startButton, stopButton, resetButton; private Timer timer; private int elapsedTime; private int seconds; private int minutes; private int hours; private boolean started; public Stopwatch() { frame = new JFrame("Stopwatch"); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); timeLabel = new JLabel("00:00:00", JLabel.CENTER); startButton = new JButton("Start"); stopButton = new JButton("Stop"); resetButton = new JButton("Reset"); startButton.addActionListener(this); stopButton.addActionListener(this); resetButton.addActionListener(this); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { elapsedTime += 1000; updateTimer(); } }); JPanel panel = new JPanel(new GridLayout(2, 1)); JPanel topPanel = new JPanel(new GridLayout(1, 1)); JPanel bottomPanel = new JPanel(new GridLayout(1, 3)); topPanel.add(timeLabel); bottomPanel.add(startButton); bottomPanel.add(stopButton); bottomPanel.add(resetButton); panel.add(topPanel); panel.add(bottomPanel); frame.add(panel); frame.setVisible(true); } public static void main(String[] args) { new Stopwatch(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { start(); } else if (e.getSource() == stopButton) { stop(); } else if (e.getSource() == resetButton) { reset(); } } private void start() { if (!started) { started = true; timer.start(); } } private void stop() { if (started) { started = false; timer.stop(); } } private void reset() { started = false; timer.stop(); elapsedTime = 0; seconds = 0; minutes = 0; hours = 0; updateTimer(); } private void updateTimer() { seconds = (elapsedTime / 1000) % 60; minutes = (elapsedTime / 60000) % 60; hours = (elapsedTime / 3600000) % 24; String secondsStr = String.format("%02d", seconds); String minutesStr = String.format("%02d", minutes); String hoursStr = String.format("%02d", hours); timeLabel.setText(hoursStr + ":" + minutesStr + ":" + secondsStr); } } ``` 这个示例代码实现了一个基本的秒表计时器功能,包括开始、停止和重置功能。它使用了Java Swing中的JFrame、JLabel、JButton和JPanel组件,以及Timer类来实现计时器的功能。当用户点击开始按钮时,计时器会开始计时;当用户点击停止按钮时,计时器会停止计时;当用户点击重置按钮时,计时器会重置为0。计时器的显示格式为:小时:分钟:秒钟。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值