이 기사에서는 참조를 위해 동적 시계 설정 알람 시계의 Java 구현을 공유합니다. 특정 내용은 다음과 같습니다
위의 그림과 같이 동적 시계를 표시하고 알람을 MP3를 재생하도록 설정할 수 있습니다.
가장 먼저 사용해야 할 것은 시스템의 현재 시간을 얻기위한 시계 (타이머) 및 캘린더 (캘린더)입니다.
코드는 다음과 같습니다.
java.awt.graphics 가져 오기; import java.awt.graphics2d; 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; java.util.calendar 가져 오기; java.util.GregorianCalendar 가져 오기; java.util.timer 가져 오기; 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; 공개 클래스 시계는 jframe {mypanel clockpanel; Ellipse2d.double e; int x; in y; Line2d.Double Hourline; Line2d.Double Minline; Line2d.Double Septline; Gregoriancalendar 달력; int 시간; int 분; int 두 번째; 문자열 timest = ""; 정적 int sethour; 정적 int setMinute; 정적 int setsecond; 공개 정적 최종 int x = 60; 공개 정적 최종 int y = 60; 공개 정적 최종 int x_begin = 10; 공개 정적 최종 int y_begin = 10; 공개 정적 최종 int radian = 50; public clock () {setsize (300, 200); settitle ( "동적 시계"); ClockPanel = New MyPanel (); 추가 (ClockPanel); 타이머 t = 새로운 타이머 (); 작업 작업 = 새로운 작업 (); t.schedule (task, 0, 1000); // 초기마다 새로 고침} 파일 = 새 파일 ( "내가 당신을 그리워 할 때"); public static void playmusic (파일 파일) {// mp3 파일의 절대 경로 표시 {javax.media.player player = null; if (file.exists ()) {medialocator locator = new medialocator ( "file :" + file.getabsolutepath ()); System.out.println (file.getabsolutepath ()); player = manager.createalizedplayer (로케이터); player.prefetch (); // 新 新를 읽을 준비가되었습니다 .Start (); // 읽기 시작} else {System.out.println ( "파일 없음"); }} catch (ca n'trealizeexception ex) {ex.printstacktrace (); } catch (noplayerexception ex) {ex.printstacktrace (); } catch (ioexception ex) {ex.printstacktrace (); }} public void play () {// play mp3 파일 try {bufferedInputStream buffer = new bufferedInputStream ( "new fileInputStream ("내가 그리울 때 .mp3 ")); 플레이어 플레이어 = 새로운 플레이어 (버퍼); player.play (); } catch (예외 e) {System.out.println (e); }} public static void main (String [] args) {clock t = new Clock (); T.SetDefaultCloseOperation (jframe.exit_on_close); T.set -Visible (true); //t.setLocationRelativeTo(null); // 화면 중앙에 양식이 표시됩니다. // retour = integer.parseint (joptionpane.showinputDialog ( "시간 :")); setMinute = integer.parseint (joptionpane.showinputDialog ( "minutes :")); setsecond = integer.parseint (joptionpane.showinputdialog ( "초달자 :")); } class myPanel은 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); Segleline = new line2d.double (x, y, x, y); } public void paintcomponent (그래픽 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 (Timest, 0, 130); G2.Draw (e); G2.Draw (Hourline); // Hour Hand G.Draw (Minline); // Minute Hand G.Draw (SecondLine); // Soid Hand}} 클래스 작업은 Timertask {public void run () {calendar = new GregorianCalendar (); 시간 = calendar.get (calendar.hour); minute = calendar.get (calendar.minute); 두 번째 = calendar.get (calendar.second); if (sethour == Hour && setMinute == minute && setsecond == second) {playMusic (파일); 놀다(); } timest = "현재 시간 :" + 시간 + ":" + 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); Segleline.x2 = x + 50 * math.cos (Second * (Math.pi / 30) -Math.pi / 2); Segleline.y2 = y + 50 * Math.sin (Second * (Math.pi / 30) -Math.pi / 2); 리 페인트 (); }}} MP3 파일을 재생하려면 해당 JAR 패키지를 다운로드해야합니다. 그렇지 않으면 재생할 수 없습니다.
주소 다운로드 : Java는 동적 시계를 구현합니다
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.