java實現(xiàn)秒表功能
更新時間:2022年09月11日 08:27:02 作者:Jiafu_Liu
這篇文章主要為大家詳細介紹了java實現(xiàn)秒表功能,利用javax.swing.Timer類設計實現(xiàn)秒表應用程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
利用javax.swing.Timer類設計并實現(xiàn)一個模擬秒表功能的應用程序。程序中顯示不斷遞增的時間,同時包含允許用戶啟動和終止計時功能的代碼,以及一個可將時間復位為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); ?
? ? } ?
} ?運行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
SpringBoot配置文件中數(shù)據庫密碼加密兩種方案(推薦)
SpringBoot項目經常將連接數(shù)據庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對安全性要求很高,因此我們就考慮如何對密碼進行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧2019-10-10
java 在Jetty9中使用HttpSessionListener和Filter
這篇文章主要介紹了java 在Jetty9中使用HttpSessionListener和Filter的相關資料,需要的朋友可以參考下2017-06-06
MybatisPlus實現(xiàn)數(shù)據攔截的使用示例
在MyBatis-Plus中,可以通過自定義攔截器來實現(xiàn)對SQL語句的攔截和修改,本文就來介紹一下如何使用,具有一定的參考價值,感興趣的可以了解一下2023-10-10

