大佬教程收集整理的这篇文章主要介绍了java实现动态时钟并设置闹钟功能,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例为大家分享了java实现动态时钟设置闹钟功能,供大家参考,具体内容如下
显示如上图所示的动态时钟,并且可以设置闹钟,播放mp3。
首先用到的是时钟(Timer)和日历(Calendar)得到系统的当前时间。
代码如下:
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,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); secondline = new line2D.Double(X,Y); } public voID paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.drawString("12",55,25);//整点时间 g2.drawString("6",105); g2.drawString("9",15,65); g2.drawString("3",65); g2.drawString(timestr,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实现动态时钟
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的java实现动态时钟并设置闹钟功能全部内容,希望文章能够帮你解决java实现动态时钟并设置闹钟功能所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。