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

java實(shí)現(xiàn)自定義時(shí)鐘并實(shí)現(xiàn)走時(shí)功能

 更新時(shí)間:2022年06月21日 09:53:28   作者:Leslie小碼農(nóng)  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)自定義時(shí)鐘并實(shí)現(xiàn)走時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java自定義時(shí)鐘并實(shí)現(xiàn)走時(shí)功能的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

很多人想要自己用代碼實(shí)現(xiàn)一個(gè)簡單的時(shí)鐘,顯示時(shí)間,下面是我用JAVA寫的一個(gè)時(shí)鐘測試類

首先,我們先定義一個(gè)Time類,里面存放我們需要的setTime方法,可以自定義設(shè)定我們想要的時(shí)分秒,toUniversal()和toStandard()兩個(gè)成員方法分別對應(yīng)兩種時(shí)間制,24小時(shí)和12小時(shí)。

public class Time {
? ? public int hour;
? ? public int minute;
? ? public int second;
? ? public Time(){
? ? ? ? setTime(0,0,0);
? ? };
? ? public void setTime(int h,int m,int s){
? ? ? ? this.hour=((h>=0&&h<24)?h:0);
? ? ? ? this.minute=((m>=0&&m<60)?m:0);
? ? ? ? this.second=((s>=0&&s<60)?s:0);
? ? }
? ? public String toUniversal(){
? ? ? ? return hour+":"+minute+":"+second;
? ? }
? ? public ?String toStandard(){
? ? ? ? return((hour==12||hour==0)?12:hour%12)+":"+minute+":"+second
? ? ? ? ? ? ? ? +(hour<12?"AM":"PM");
? ? }
}

我們之后再寫一個(gè)類,在里面實(shí)現(xiàn)我們的功能:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.*;
?
public class TimeTest{
? ? //全員靜態(tài)變量
? ? static Integer i1=0,i2=0,i3=0;//時(shí)分秒
? ? static Time tt=new Time();//創(chuàng)建一個(gè)對象
? ? static TextField ta = new TextField(30);//兩個(gè)文本框存放兩個(gè)時(shí)間
? ? static TextField ka=new TextField(30);
? ? static Timer timer=new Timer();//設(shè)置一個(gè)Timer
? ? //設(shè)置靜態(tài)函數(shù)進(jìn)行時(shí)間轉(zhuǎn)換和timer時(shí)間調(diào)度器
? ? public static void fun(){
? ? ? ? TimerTask task=new TimerTask() {
? ? ? ? ? ? @Override
? ? ? ? ? ? //run走針操作
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? if(i3==60)
? ? ? ? ? ? ? ? {i3=0;
? ? ? ? ? ? ? ? ? ? i2++;
? ? ? ? ? ? ? ? ? ? if(i2==60)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? i1++;
? ? ? ? ? ? ? ? ? ? ? ? i2=0;
? ? ? ? ? ? ? ? ? ? ? ? if(i1==24){
? ? ? ? ? ? ? ? ? ? ? ? ? ? i1=0;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? tt.setTime(i1,i2,i3);
? ? ? ? ? ? ? ? String s1= tt.toUniversal();
? ? ? ? ? ? ? ? String s2=tt.toStandard();
? ? ? ? ? ? ? ? ta.setText(s1);//將獲取的兩個(gè)時(shí)間存放到文本域里面
? ? ? ? ? ? ? ? ka.setText(s2);
? ? ? ? ? ? ? ? i3++;//秒針自加
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? timer.schedule(task,0,1000);//timer調(diào)度器從0開始,間隔一秒調(diào)度一次
? ? }
? ? public static void main(String[] args) {
? ? ? ? Frame frame = new Frame("鐘表的測試類");
? ? ? ? TextField text1=new TextField("時(shí)");
? ? ? ? TextField text2=new TextField("分");
? ? ? ? TextField text3=new TextField("秒");
? ? ? ? JButton button=new JButton("確認(rèn)");
? ? ? ? Font f=new Font("仿宋",Font.BOLD+Font.CENTER_BASELINE,30);
? ? ? ? ta.setFont(f);
? ? ? ? ka.setFont(f);
? ? ? ? ta.setBackground(Color.GRAY);
? ? ? ? ka.setBackground(Color.GRAY);
? ? ? ? text1.setFont(f);
? ? ? ? text2.setFont(f);
? ? ? ? text3.setFont(f);
? ? ? ? //button事件響應(yīng)
? ? ? ? button.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? String d0=text1.getText().trim();
?
? ? ? ? ? ? ? ? String d2=text2.getText().trim();
?
? ? ? ? ? ? ? ? String d3=text3.getText().trim();
? ? ? ? ? ? ? ? i1=Integer.valueOf(d0);
? ? ? ? ? ? ? ? i2=Integer.valueOf(d2);
? ? ? ? ? ? ? ? i3=Integer.valueOf(d3);
? ? ? ? ? ? ? ? tt.setTime(i1,i2,i3);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //調(diào)用fun函數(shù)
? ? ? ? fun();
? ? ? ? Box aBox=Box.createVerticalBox();
? ? ? ? aBox.add(text1);
? ? ? ? aBox.add(text2);
? ? ? ? aBox.add(text3);
? ? ? ? aBox.add(button);
? ? ? ? Box bBox=Box.createVerticalBox();
? ? ? ? bBox.add(ta);
? ? ? ? bBox.add(ka);
? ? ? ? Box cBox = Box.createHorizontalBox();
? ? ? ? cBox.add(bBox);
? ? ? ? cBox.add(aBox);
? ? ? ? //將cBox加入到frame框架里面
? ? ? ? frame.add(cBox);
? ? ? ? //關(guān)閉frame窗口
? ? ? ? frame.addWindowListener(new WindowAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void windowClosing(WindowEvent e) {
? ? ? ? ? ? ? ? System.exit(0);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? frame.pack();
? ? ? ? frame.setVisible(true);
? ? }
}

里面用到了awt相關(guān)的類,做界面的時(shí)候很方便,大家如果看不懂,可以先去熟悉一下awt類,很容易上手,而且走時(shí)功能也只需要一個(gè)timer調(diào)度器便可以實(shí)現(xiàn)。

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

相關(guān)文章

最新評論

涟水县| 五指山市| 新兴县| 满城县| 泰顺县| 章丘市| 寻乌县| 大宁县| 商都县| 精河县| 正安县| 长沙县| 东乌珠穆沁旗| 巧家县| 大埔县| 德清县| 夏河县| 会同县| 托克托县| 丰顺县| 碌曲县| 元氏县| 衡阳市| 河东区| 潮安县| 青龙| 成都市| 舒城县| 留坝县| 庆元县| 泾川县| 长汀县| 宁阳县| 西和县| 台南市| 庆安县| 保靖县| 攀枝花市| 邵武市| 奉贤区| 平遥县|