Este artigo compartilha a implementação do Java do despertador de configuração de relógio dinâmico para sua referência. O conteúdo específico é o seguinte
Exibe o relógio dinâmico, como mostrado na figura acima, e pode definir o alarme para reproduzir MP3.
A primeira coisa a usar é o relógio (timer) e o calendário (calendário) para obter o horário atual do sistema.
O código é o seguinte:
importar java.awt.graphics; importar java.awt.graphics2d; importar java.awt.geom.ellipse2d; importar java.awt.geom.line2d; importar java.io.bufferInputStream; importar java.io.file; importar java.io.fileInputStream; importar java.io.ioException; importar java.util.calendar; importar java.util.gregorianCalendar; importar java.util.timer; importar java.util.timertak; importar javax.media.CannotRealizeException; importar javax.media.manager; importar javax.media.medialocator; importar javax.media.noplayerexception; importar javax.swing.jframe; importar javax.swing.JOptionPane; importar javax.swing.jpanel; importar javazoom.jl.player.player; clock de classe pública estende JFrame {mypanel ClockPanel; Elipse2d.double e; int x; int y; Line2d.double hourline; Line2d.double minline; Line2d.double SecondLine; Calendário GregoriaCalendar; int hora; minuto int; int segundo; String timeSTer = ""; estático int sethour; estático int setMinute; estático int se 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 ("Clock Dynamic"); clockPanel = new MyPanel (); add (clockpanel); Timer t = new timer (); Tarefa de tarefa = new tarefa (); t.schedule (tarefa, 0, 1000); // Atualizar uma vez a cada segundo arquivo de arquivo = novo arquivo ("Quando eu sinto sua falta.mp3"); public static void playmusic (arquivo de arquivo) {// mostra o caminho absoluto do arquivo mp3, tente {javax.media.player player = null; if (file.exists ()) {locator medialocator = new medialocator ("arquivo:" + file.getabsolutepath ()); System.out.println (file.getabsolutepath ()); player = gerenciador.CreaReAlizedPlayer (localizador); player.prefetch (); // 新 está pronto para ler player.start (); // comece a ler} else {System.out.println ("nenhum arquivo encontrado"); }} Catch (não é realizeException ex) {ex.PrintStackTrace (); } catch (noplayerexception ex) {ex.printStackTrace (); } catch (ioexception ex) {ex.printStackTrace (); }} public void play () {// play mp3 Arquivo Try {bufferInputStream buffer = new BufferInputStream (new FileInputStream ("Quando eu sinto sua falta.mp3"); Jogador jogador = novo jogador (buffer); player.play (); } catch (Exceção e) {System.out.println (e); }} public static void main (string [] args) {relógio t = new Clock (); t.SetDefaultCloseoperation (jframe.exit_on_close); t.setVisible (true); //t.setLocationRelativeTo(Null); // O formulário é exibido no centro da tela // Digite o tempo de alarme para ser sethour = Integer.parseint (joptionpane.showInputDialog ("Por favor, digite a hora:")); setMinute = Integer.parseint (joptionpane.showInputDialog ("Por favor, digite minutos:")); setSecond = Integer.parseint (joptionpane.showInputDialog ("Por favor, digite segundos:")); } classe myPanel estende 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 (Gráfico G) {super.PaintComponent (G); Graphics2d G2 = (Graphics2d) g; g2.DrawString ("12", 55, 25); // Tempo de ocasião 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 (hora); // hora de hora g2.draw (minline); // minuto de mão g2.draw (SecondLine); // Segunda mão}} classe Tarefa estende Timertosk {public void run () {calendar = new Gregoriancalendar (); hora = calendário.get (calendário.hour); minuto = calendário.get (calendário.minute); segundo = calendário.get (calendário.second); if (sethour == hour && setMinute == minute && setSecond == Second) {playmusic (file); jogar(); } timest = "Hora atual:" + hora + ":" + minuto + ":" + segundo; hour.x2 = x + 40 * math.cos (hora * (math.pi / 6) - math.pi / 2); hourline.y2 = y + 40 * math.sin (hora * (math.pi / 6) - math.pi / 2); minline.x2 = x + 45 * math.cos (minuto * (math.pi / 30) - math.pi / 2); minline.y2 = y + 45 * math.sin (minuto * (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); repintar (); }}} Tocar o arquivo MP3 requer o download do pacote JAR correspondente, caso contrário, ele não poderá ser reproduzido.
Download Endereço: Java implementa o relógio dinâmico
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.