この記事では、参照用の動的クロック設定目覚まし時計のJava実装を共有しています。特定のコンテンツは次のとおりです
上の図に示すように動的クロックを表示し、MP3を再生するようにアラームを設定できます。
最初に使用するのは、システムの現在の時間を取得するためのクロック(タイマー)とカレンダー(カレンダー)です。
コードは次のとおりです。
java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.geom.ellipse2dをインポートします。 java.awt.geom.line2dをインポート; java.io.bufferedinputStreamをインポートします。 java.io.fileをインポートします。 java.io.fileinputStreamをインポートします。 java.io.ioexceptionをインポートします。 java.util.calendarをインポートします。 java.util.gregoriancalendarをインポートします。 java.util.timerをインポートします。 java.util.timertaskをインポートします。 javax.media.cannotrealizeexceptionをインポートします。 javax.media.managerをインポートします。 javax.media.medialocatorをインポートします。 javax.media.noplayerexceptionをインポートします。 javax.swing.jframeをインポートします。 javax.swing.joptionpaneをインポートします。 javax.swing.jpanelをインポートします。 javazoom.jl.player.playerをインポートします。パブリッククラスクロックはjFrame {mypanel clockpanel; Ellipse2d.Double E; int x; int y; line2d.double hourline; line2d.double minline; line2d.double secondline;グレゴリアンカレンダーカレンダー。 int時間; int minute; int second;文字列タイムスト= "";静的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( "Dynamic Clock"); clockpanel = new mypanel(); add(clockpanel);タイマーT =新しいタイマー();タスクタスク= new Task(); t.schedule(task、0、1000); // 1秒ごとに1回更新} file file = new file( "you.mp3"); public static void playmusic(file file){// mp3ファイルの絶対パスを表示する{javax.media.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(); // readingを読み取ります} else {system.out.println( "no file fund"); }} catch(can nable realizeexception ex){ex.printstacktrace(); } catch(noplayerexception ex){ex.printstacktrace(); } catch(ioException ex){ex.printstacktrace(); }} public void play(){// mp3 file try {bufferedinputStream buffer = new bufferedinputStream(new fileinputStream( "you.mp3"));プレーヤープレーヤー= new Player(バッファー); 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); //画面の中央にフォームが表示されますsetminute = integer.parseint(joptionpane.showinputdialog( "interning minutes:")); setSecond = integer.parseint(joptionpane.showinputdialog( "seconds:")); } 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); // chuse time 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); // hour hand g2.draw(minline); // minute hand g2.draw(secondline); // secondhand}}クラスタスクはティマタスク{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);遊ぶ(); } Timestr = "現在の時間:" + hour + ":" + minute + ":" + second; hour.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は動的時計を実装します
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。