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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - JAVA教程 - java實現動態時鐘并設置鬧鐘功能

java實現動態時鐘并設置鬧鐘功能

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

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

本文實例為大家分享了java實現動態時鐘設置鬧鐘功能,供大家參考,具體內容如下

java實現動態時鐘并設置鬧鐘功能

顯示如上圖所示的動態時鐘,并且可以設置鬧鐘,播放mp3。

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

代碼如下:

?
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("動態時鐘");
    clockpanel = new mypanel();
    add(clockpanel);
    timer t = new timer();
    task task = new task();
    t.schedule(task, 0, 1000);//每秒刷新一次
  }
   
  file file = new file("當我想你的時候.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("當我想你的時候.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);//窗體顯示在屏幕中央
     
    //輸入要設置的鬧鐘時間
    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 = "當前時間:" + 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文件需要下載對應的jar包,否則不能播放。

下載地址:java實現動態時鐘

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本老妇乱子伦中文视频 | 天堂资源在线www中文 | 69av美女| 欧美日韩一区二区三区韩大 | 欧美精品亚洲精品日韩专区va | 亚洲狠狠网站色噜噜 | 黑人video粗暴日本 | 免费lulu网站 | 亚洲视频在线观看免费视频 | 亚洲国内精品 | 青草国产在线观看 | 喷潮女王cytherea全部视频 | 亚洲另类中文字幕 | 2019国内自拍大神视频 | 亚洲国产精品久久卡一 | 黑人艹| 日本哺乳期网站xxxx | 我和岳的性事小说 | 99精品久久精品一区二区小说 | 99精品视频一区在线观看miya | 免费看全黄特黄毛片 | 99久久精品6在线播放 | 精品91自产拍在线 | 青青国产在线观看 | 亚洲图片 自拍偷拍 | 午夜影院网页 | 肉浦团在线观看 | 色噜噜狠狠狠综合曰曰曰88av | 成人在线第一页 | 午夜亚洲WWW湿好爽 午夜想想爱午夜剧场 | 免费一级特黄特色大片在线观看 | 亚洲 欧美 国产 视频二区 | 亚洲精品一区二区三区在线看 | 日韩综合第一页 | 911精品国产亚洲日本美国韩国 | 小小水蜜桃免费影院 | 久久久久久久久女黄 | 亚洲精品国产国语 | 国模丰满美女冰漪34d | 国产亚洲精品第一综合另类 | 日韩无遮挡大尺度啪啪影片 |