欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)秒表功能

 更新時(shí)間:2022年09月11日 08:27:02   作者:Jiafu_Liu  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)秒表功能,利用javax.swing.Timer類設(shè)計(jì)實(shí)現(xiàn)秒表應(yīng)用程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

利用javax.swing.Timer類設(shè)計(jì)并實(shí)現(xiàn)一個(gè)模擬秒表功能的應(yīng)用程序。程序中顯示不斷遞增的時(shí)間,同時(shí)包含允許用戶啟動(dòng)和終止計(jì)時(shí)功能的代碼,以及一個(gè)可將時(shí)間復(fù)位為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; //顯示標(biāo)簽?
? ? private JButton startJButton; ? ?//開始按鈕
? ? private JButton stopJButton; ? ? //停止按鈕
? ? private JButton resetJButton; ? ?//復(fù)位按鈕
? ? private long countMis,countSec,countMin,countHour;//計(jì)時(shí)變量
? ? private DecimalFormat textFormat=new DecimalFormat("00");//格式化輸出
? ? Timer timer=new Timer(10,new TestActionListener());//計(jì)時(shí)單位10ms

? ? public stopWatch() { ?
? ? ? ? JPanel panel=new JPanel(new GridLayout(1,3,5,10)); //網(wǎng)格布局嵌入按鈕
? ? ? ? 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");?
? ? ? ? //設(shè)置JButton相關(guān)屬性
? ? ? ? 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);

? ? } ?
? ? //處理相關(guān)事件
? ? 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{//滿位后復(fù)位
? ? ? ? ? ? ? ? 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); ?
? ? } ?
} ?

運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java生成隨機(jī)數(shù)之Random與ThreadLocalRandom性能比較詳解

    Java生成隨機(jī)數(shù)之Random與ThreadLocalRandom性能比較詳解

    大家項(xiàng)目中如果有生成隨機(jī)數(shù)的需求,我想大多都會(huì)選擇使用Random來(lái)實(shí)現(xiàn),它內(nèi)部使用了CAS來(lái)實(shí)現(xiàn)。?實(shí)際上,JDK1.7之后,提供了另外一個(gè)生成隨機(jī)數(shù)的類ThreadLocalRandom,那么他們二者之間的性能是怎么樣的呢?本文就來(lái)詳細(xì)說(shuō)說(shuō)
    2022-12-12
  • springMvc注解之@ResponseBody和@RequestBody詳解

    springMvc注解之@ResponseBody和@RequestBody詳解

    本篇文章主要介紹了springMvc注解之@ResponseBody和@RequestBody詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • 簡(jiǎn)單介紹java中equals以及==的用法

    簡(jiǎn)單介紹java中equals以及==的用法

    這篇文章主要介紹了簡(jiǎn)單介紹java中equals以及==的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

    淺談讓@Value更方便的Spring自定義轉(zhuǎn)換類

    Spring為大家內(nèi)置了不少開箱即用的轉(zhuǎn)換類,如字符串轉(zhuǎn)數(shù)字、字符串轉(zhuǎn)時(shí)間等,但有時(shí)候需要使用自定義的屬性,則需要自定義轉(zhuǎn)換類了
    2021-06-06
  • Java線程狀態(tài)變換過(guò)程代碼解析

    Java線程狀態(tài)變換過(guò)程代碼解析

    這篇文章主要介紹了Java線程狀態(tài)變換過(guò)程代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java如何自定義List中的sort()排序,用于日期排序

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

    這篇文章主要介紹了java如何自定義List中的sort()排序,用于日期排序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • SpringBoot內(nèi)置tomcat啟動(dòng)原理詳解

    SpringBoot內(nèi)置tomcat啟動(dòng)原理詳解

    這篇文章主要介紹了SpringBoot內(nèi)置tomcat啟動(dòng)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 容器環(huán)境的JVM內(nèi)存設(shè)置實(shí)踐記錄

    容器環(huán)境的JVM內(nèi)存設(shè)置實(shí)踐記錄

    Docker和K8S的興起,很多服務(wù)已經(jīng)運(yùn)行在容器環(huán)境,對(duì)于java程序,JVM設(shè)置是一個(gè)重要的環(huán)節(jié),這里總結(jié)下我們項(xiàng)目里的最佳實(shí)踐,對(duì)容器環(huán)境的JVM內(nèi)存相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-03-03
  • redis發(fā)布訂閱Java代碼實(shí)現(xiàn)過(guò)程解析

    redis發(fā)布訂閱Java代碼實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了redis發(fā)布訂閱Java代碼實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 關(guān)于java開發(fā)的性能問(wèn)題總結(jié)(必看)

    關(guān)于java開發(fā)的性能問(wèn)題總結(jié)(必看)

    下面小編就為大家?guī)?lái)一篇關(guān)于java開發(fā)的性能問(wèn)題總結(jié)(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03

最新評(píng)論