如何用Java编写一个秒表计时程序?

2026-05-24 02:271阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计704个文字,预计阅读时间需要3分钟。

如何用Java编写一个秒表计时程序?

javaimport javax.swing.*;

public class TimerApp { private JFrame frame; private JLabel timeLabel; private JButton startButton; private JButton stopButton; private Timer timer; private int elapsedTime=0;

public TimerApp() { frame=new JFrame(秒表); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLayout(null);

timeLabel=new JLabel(00:00:00); timeLabel.setBounds(100, 30, 100, 30); frame.add(timeLabel);

startButton=new JButton(启动); startButton.setBounds(50, 80, 100, 30); frame.add(startButton);

stopButton=new JButton(停止); stopButton.setBounds(150, 80, 100, 30); frame.add(stopButton);

timer=new Timer(1000, e -> { elapsedTime++; int hours=elapsedTime / 3600; int minutes=(elapsedTime % 3600) / 60; int seconds=elapsedTime % 60; timeLabel.setText(String.format(%02d:%02d:%02d, hours, minutes, seconds)); });

startButton.addActionListener(e -> { if (!timer.isRunning()) { timer.start(); } });

stopButton.addActionListener(e -> { if (timer.isRunning()) { timer.stop(); } });

JButton resetButton=new JButton(重置); resetButton.setBounds(100, 130, 100, 30); frame.add(resetButton);

resetButton.addActionListener(e -> { elapsedTime=0; timeLabel.setText(00:00:00); timer.stop(); });

frame.setVisible(true); }

public static void main(String[] args) { SwingUtilities.invokeLater(() -> new TimerApp()); }}

利用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编写一个秒表计时程序?

本文共计704个文字,预计阅读时间需要3分钟。

如何用Java编写一个秒表计时程序?

javaimport javax.swing.*;

public class TimerApp { private JFrame frame; private JLabel timeLabel; private JButton startButton; private JButton stopButton; private Timer timer; private int elapsedTime=0;

public TimerApp() { frame=new JFrame(秒表); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); frame.setLayout(null);

timeLabel=new JLabel(00:00:00); timeLabel.setBounds(100, 30, 100, 30); frame.add(timeLabel);

startButton=new JButton(启动); startButton.setBounds(50, 80, 100, 30); frame.add(startButton);

stopButton=new JButton(停止); stopButton.setBounds(150, 80, 100, 30); frame.add(stopButton);

timer=new Timer(1000, e -> { elapsedTime++; int hours=elapsedTime / 3600; int minutes=(elapsedTime % 3600) / 60; int seconds=elapsedTime % 60; timeLabel.setText(String.format(%02d:%02d:%02d, hours, minutes, seconds)); });

startButton.addActionListener(e -> { if (!timer.isRunning()) { timer.start(); } });

stopButton.addActionListener(e -> { if (timer.isRunning()) { timer.stop(); } });

JButton resetButton=new JButton(重置); resetButton.setBounds(100, 130, 100, 30); frame.add(resetButton);

resetButton.addActionListener(e -> { elapsedTime=0; timeLabel.setText(00:00:00); timer.stop(); });

frame.setVisible(true); }

public static void main(String[] args) { SwingUtilities.invokeLater(() -> new TimerApp()); }}

利用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编写一个秒表计时程序?