一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - JAVA教程 - java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能

java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能

2021-03-26 11:18HQUZkP JAVA教程

這篇文章主要為大家詳細介紹了java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)動態(tài)時鐘設(shè)置鬧鐘功能,供大家參考,具體內(nèi)容如下

java實現(xiàn)動態(tài)時鐘并設(shè)置鬧鐘功能

顯示如上圖所示的動態(tài)時鐘,并且可以設(shè)置鬧鐘,播放mp3。

首先用到的是時鐘(timer)和日歷(calendar)得到系統(tǒng)的當(dāng)前時間。

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.geom.ellipse2d;
import java.awt.geom.line2d;
import java.io.bufferedinputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.ioexception;
import java.util.calendar;
import java.util.gregoriancalendar;
import java.util.timer;
import java.util.timertask;
 
import javax.media.cannotrealizeexception;
import javax.media.manager;
import javax.media.medialocator;
import javax.media.noplayerexception;
import javax.swing.jframe;
import javax.swing.joptionpane;
import javax.swing.jpanel;
 
import javazoom.jl.player.player;
 
 
public class clock extends jframe {
 
  mypanel clockpanel;
  ellipse2d.double e;
  int x;
  int y;
  line2d.double hourline;
  line2d.double minline;
  line2d.double secondline;
  gregoriancalendar calendar;
   
  int hour;
  int minute;
  int second;
  string timestr = "";
   
  static int sethour;
  static int setminute;
  static int setsecond;  
   
  public static final int x = 60;
  public static final int y = 60;
  public static final int x_begin = 10;
  public static final int y_begin = 10;
  public static final int radian = 50;
   
  public clock(){
    setsize(300, 200);
    settitle("動態(tài)時鐘");
    clockpanel = new mypanel();
    add(clockpanel);
    timer t = new timer();
    task task = new task();
    t.schedule(task, 0, 1000);//每秒刷新一次
  }
   
  file file = new file("當(dāng)我想你的時候.mp3");
   
  public static void playmusic(file file) { //顯示mp3文件的絕對路徑
    try
     javax.media.player player = null
      if (file.exists()) { 
  medialocator locator = new medialocator("file:"
                + file.getabsolutepath()); 
  system.out.println(file.getabsolutepath()); 
      player = manager.createrealizedplayer(locator); 
         player.prefetch();// ?準備讀取 
      player.start();// 開始讀取 
        } else
         system.out.println("沒找到文件"); 
        
       } catch (cannotrealizeexception ex) { 
          ex.printstacktrace(); 
       } catch (noplayerexception ex) { 
          ex.printstacktrace(); 
       } catch (ioexception ex) { 
        ex.printstacktrace(); 
        
      
 
  public void play() {//播放mp3文件
    try {
      bufferedinputstream buffer = new bufferedinputstream(new fileinputstream("當(dāng)我想你的時候.mp3"));
      player player = new player(buffer);
      player.play();
    } catch (exception e) {
      system.out.println(e);
    }  
  
   
  public static void main(string[] args) {
    clock t = new clock();
    t.setdefaultcloseoperation(jframe.exit_on_close);
    t.setvisible(true);
    //t.setlocationrelativeto(null);//窗體顯示在屏幕中央
     
    //輸入要設(shè)置的鬧鐘時間
    sethour = integer.parseint(joptionpane.showinputdialog("請輸入小時:"));
    setminute = integer.parseint(joptionpane.showinputdialog("請輸入分鐘:"));
    setsecond = integer.parseint(joptionpane.showinputdialog("請輸入秒:"));
     
  }
 
class mypanel extends jpanel {
  public mypanel() {
    e = new ellipse2d.double(x_begin, y_begin, 100, 100);
    hourline = new line2d.double(x, y, x, y);
    minline = new line2d.double(x, y, x, y);
    secondline = new line2d.double(x, y, x, y);
  }
 
  public void paintcomponent(graphics g) {
    super.paintcomponent(g);
    graphics2d g2 = (graphics2d) g;
    g2.drawstring("12", 55, 25);//整點時間
    g2.drawstring("6", 55, 105);
    g2.drawstring("9", 15, 65);
    g2.drawstring("3", 100, 65);
    g2.drawstring(timestr, 0, 130);
    g2.draw(e);
    g2.draw(hourline);//時針
    g2.draw(minline);//分針
    g2.draw(secondline);//秒針
  }
}
 
class task extends timertask {
  public void run() {
    calendar = new gregoriancalendar();
    hour = calendar.get(calendar.hour);
    minute = calendar.get(calendar.minute);
    second = calendar.get(calendar.second);
     
    if(sethour == hour && setminute == minute && setsecond == second){
      playmusic(file);
      play();
      }
     
    timestr = "當(dāng)前時間:" + hour + " : " + minute + " : " + second;
     
    hourline.x2 = x + 40 * math.cos(hour * (math.pi / 6) - math.pi / 2);
    hourline.y2 = y + 40 * math.sin(hour * (math.pi / 6) - math.pi / 2);
    minline.x2 = x + 45
        * math.cos(minute * (math.pi / 30) - math.pi / 2);
    minline.y2 = y + 45
        * math.sin(minute * (math.pi / 30) - math.pi / 2);
    secondline.x2 = x + 50
        * math.cos(second * (math.pi / 30) - math.pi / 2);
    secondline.y2 = y + 50
        * math.sin(second * (math.pi / 30) - math.pi / 2);
    repaint();
   }
  }
}

其中播放mp3文件需要下載對應(yīng)的jar包,否則不能播放。

下載地址:java實現(xiàn)動態(tài)時鐘

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

原文鏈接:http://blog.csdn.net/qq_32353771/article/details/51726312

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产91在线九色 | 俄罗斯美女尿尿 | 亚洲妇熟xxxxx妇色黄 | 小寡妇好紧进去了好大看视频 | 9re视频这里只有精品 | 超级乱淫伦短篇在车上 | 成人人免费夜夜视频观看 | 欧美牛逼aa | 欧美调教打屁股spank视频 | 美女操批 | 刺激一区仑乱 | 国产欧美日韩视频在线观看一区二区 | 亚洲人成伊人成综合网久久 | 俄罗斯一级在线播放 | a一区二区三区视频 | 边摸边吃奶边做爽gif动态图 | 98色花堂永久地址国产精品 | 久久99视热频国只有精品 | 大桥未久aⅴ一区二区 | 欧美男人的天堂 | www.亚洲天堂 | 小仙夜晚慰自催眠mp3护士篇 | 久久热在线视频精品店 | 91精品国产9l久久久久 | 亚洲国产高清视频 | 91视频夜色 | 精品成人一区二区三区免费视频 | 18欧美同性videos可播放 | 久久精品视在线观看85 | 日日爽日日操 | www免费视频com | 日本人与黑人做爰视频网站 | 农村老妇1乱69系列小说 | 精品一区二区三区免费观看 | 精品午夜寂寞影院在线观看 | 王淑兰与铁柱全文免费阅读 | 5g影院天天5g天天爽大陆 | 午夜免费无码福利视频麻豆 | 久见久热 这里只有精品 | 久久精品国产只有精品 | 夫妇交换小说 |