最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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é)果:

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

相關(guān)文章

  • SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot項(xiàng)目經(jīng)常將連接數(shù)據(jù)庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對(duì)安全性要求很高,因此我們就考慮如何對(duì)密碼進(jìn)行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧
    2019-10-10
  • Java線程中Thread方法下的Join方法詳解

    Java線程中Thread方法下的Join方法詳解

    這篇文章主要介紹了Java線程中Thread方法下的Join方法詳解,在項(xiàng)目中往往會(huì)遇到這樣一個(gè)場景,就是需要等待幾件事情都給做完后才能走下面的事情,這個(gè)時(shí)候就需要用到Thread方法下的Join方法,join方法是無參且沒有返回值的,需要的朋友可以參考下
    2024-01-01
  • maven依賴版本沒有按照最短路徑原則生效的解決方案

    maven依賴版本沒有按照最短路徑原則生效的解決方案

    這篇文章主要介紹了maven依賴版本沒有生效的解決方案,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2021-01-01
  • java 在Jetty9中使用HttpSessionListener和Filter

    java 在Jetty9中使用HttpSessionListener和Filter

    這篇文章主要介紹了java 在Jetty9中使用HttpSessionListener和Filter的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Java實(shí)現(xiàn)多任務(wù)執(zhí)行助手

    Java實(shí)現(xiàn)多任務(wù)執(zhí)行助手

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多任務(wù)執(zhí)行助手,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • MybatisPlus實(shí)現(xiàn)數(shù)據(jù)攔截的使用示例

    MybatisPlus實(shí)現(xiàn)數(shù)據(jù)攔截的使用示例

    在MyBatis-Plus中,可以通過自定義攔截器來實(shí)現(xiàn)對(duì)SQL語句的攔截和修改,本文就來介紹一下如何使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Java 隨機(jī)生成任意組電話號(hào)碼過程解析

    Java 隨機(jī)生成任意組電話號(hào)碼過程解析

    這篇文章主要介紹了Java 隨機(jī)生成任意組電話號(hào)碼過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Java 設(shè)計(jì)模式之適配器模式詳解

    Java 設(shè)計(jì)模式之適配器模式詳解

    設(shè)計(jì)模式(Design pattern)是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過分類編目的、代碼設(shè)計(jì)經(jīng)驗(yàn)的總結(jié)。使用設(shè)計(jì)模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性
    2021-11-11
  • Java中的命名與目錄接口JNDI基本操作方法概覽

    Java中的命名與目錄接口JNDI基本操作方法概覽

    這篇文章主要介紹了Java中的命名與目錄接口JNDI基本操作方法概覽,JNDI提供統(tǒng)一的客戶端API使得Java應(yīng)用程序可以和這些命名服務(wù)和目錄服務(wù)之間進(jìn)行交互,需要的朋友可以參考下
    2016-03-03
  • Lucene實(shí)現(xiàn)索引和查詢的實(shí)例講解

    Lucene實(shí)現(xiàn)索引和查詢的實(shí)例講解

    下面小編就為大家分享一篇Lucene實(shí)現(xiàn)索引和查詢的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12

最新評(píng)論

扶沟县| 嫩江县| 辽阳县| 嵩明县| 固原市| 揭阳市| 方城县| 长阳| 东方市| 苏州市| 广德县| 开远市| 泰宁县| 汪清县| 凌海市| 营口市| 梁平县| 龙里县| 会东县| 洪江市| 山东| 河曲县| 淮滨县| 锡林浩特市| 武宣县| 新邵县| 牡丹江市| 清原| 英吉沙县| 教育| 东兰县| 浦东新区| 化州市| 大丰市| 陵川县| 武定县| 介休市| 芒康县| 登封市| 铜山县| 靖西县|