java闹钟程序声音_跪求高手帮忙写一个JAVA手机闹钟程序 实现添加铃声和设置多闹钟...

展开全部

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

public class AlarmClock extends Applet implements Runnable

{

Thread timer=null; //创建线程timer

Image clockp,gif1,gif2,clock6,clock7; //clockp:闹钟的外壳,闹铃和e68a84e8a2ad3231313335323631343130323136353331333262353365报时鸟

int s,m,h,hh;

AudioClip ipAu,danger,chirp;

boolean canPaint=true;

boolean flag=false;

boolean strike=true;

int counter=0;

int lasts;

Image offscreen_buf=null;

int i,j,t=0;

int timeout=166;

int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;

Date dummy=new Date(); //生成Data对象

GregorianCalendar cal=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式

Date dat=null;

Date timeNow=null;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Panel setpanel;

Color backcolor=Color.pink;

TextField showhour,showmin,showsec,sethour,setmin,setsec;//显示当前时间文本框和定时文本框

Button onbutton;

Button offbutton;

Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

Label info1=new Label("欢迎使用定时提醒闹钟"),info2=new Label("");

Label note1=new Label("当前时间:"),note2=new Label("闹钟设置:");

boolean setalerm=false,clickflag=false;//判断是否响铃和振动

int fixh=0,fixm=0,fixs=0;//记录闹钟的定时

public void init()//初始化方法

{

Integer gif_number;

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null); //将布局管理器初始化为null

setpanel=new Panel();

setpanel.setLayout(null);

setpanel.add(note1);

setpanel.add(note2);

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

note2.setBounds(30,180,60,20);

note2.setBackground(backcolor);

note2.setForeground(Color.black);

hlabel1=new Label();

mlabel1=new Label();

slabel1=new Label();

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//显示当前时间用的文本框

showhour=new TextField("00",5);

showmin=new TextField("00",5);

showsec=new TextField("00",5);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

//当前时间用的文本框的位置、大小

setpanel.add(showhour);

showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);

showhour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel1);

hlabel1.setText("时");

hlabel1.setBackground(backcolor);

hlabel1.setForeground(Color.black);

hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的分钟文本框的位置、大小

setpanel.add(showmin);

showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);

showmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel1);

mlabel1.setText("分");

mlabel1.setBackground(backcolor);

mlabel1.setForeground(Color.black);

mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的秒文本框的位置、大小

setpanel.add(showsec);

showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);

showsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel1);

slabel1.setText("秒");

slabel1.setBackground(backcolor);

slabel1.setForeground(Color.black);

slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=50;

//定时的小时文本框的位置、大小

setpanel.add(sethour);

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel2);

hlabel2.setText("时");

hlabel2.setBackground(backcolor);

hlabel2.setForeground(Color.black);

hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的分钟文本框的位置、大小

setpanel.add(setmin);

setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);

setmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel2);

mlabel2.setText("分");

mlabel2.setBackground(backcolor);

mlabel2.setForeground(Color.black);

mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的秒文本框的位置、大小

setpanel.add(setsec);

setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);

setsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel2);

slabel2.setText("秒");

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//设置闹钟控制按钮(on,off)

onbutton=new Button("开");

offbutton=new Button("关");

setpanel.add(onbutton);

setpanel.add(offbutton);

onbutton.setBounds(90,180,40,20);

offbutton.setBounds(140,180,40,20);

//加入一些附加的信息标签(题头,题尾)

setpanel.add(info1);

info1.setBackground(backcolor);

info1.setForeground(Color.blue);

info1.setBounds(50,50,150,20);

setpanel.add(info2);

info2.setBackground(backcolor);

info2.setForeground(Color.blue);

info2.setBounds(150,280,100,20);

//将面板加入当前容器中,并设置面板的大小和背景色

add(setpanel);

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

//获取声音文件

ipAu=getAudioClip(getDocumentBase(),"bells/仙剑.mid");

danger=getAudioClip(getDocumentBase(),"bells/0.mid");

chirp=getAudioClip(getDocumentBase(),"bells/3.mid");

int xcenter,ycenter,s,m,h;

xcenter=145;

ycenter=162;

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

lasts=s;

MediaTracker mt=new MediaTracker(this);//创建Tracke对象

clockp=getImage(getDocumentBase(),"休闲.png");

gif1=getImage(getDocumentBase(),"gif1.gif");

gif2=getImage(getDocumentBase(),"gif2.gif");

clock6=getImage(getDocumentBase(),"clock6.gif");

clock7=getImage(getDocumentBase(),"clock7.gif");

mt.addImage(clockp,i++);

mt.addImage(gif1,i++);

mt.addImage(gif2,i++);

mt.addImage(clock6,i++);

mt.addImage(clock7,i++);

try{mt.waitForAll();}catch(InterruptedException e){};//等待加载结束

resize(600,420);//设置窗口大小

}

public void paint(Graphics g){//重写paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

int xcenter,ycenter;

String today;

Integer gif_number;

xcenter=148;

ycenter=186;

dat=new Date();

cal.setTime(dat);

//读取当前时间

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

today=df.format(dat);

//指针位置

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

//设置字体和颜色

g.setFont(F);

g.setColor(fgcol);

g.setColor(fgcol2);

g.setColor(getBackground());

g.fillRect(1,1,634,419);

g.drawImage(clockp,75,110,this);

g.drawImage(clock6,83,280,this);

g.setColor(fgcol2);

g.setColor(getBackground());

g.setColor(fgcol2);

//以数字方式显示年、月、日和时间

g.drawString(today,55,415);

g.drawLine(xcenter,ycenter,xs,ys);

g.setColor(fgcol);

//画指针

g.drawLine(xcenter,ycenter-1,xm,ym);

g.drawLine(xcenter-1,ycenter,xm,ym);

g.drawLine(xcenter,ycenter-1,xh,yh);

g.drawLine(xcenter-1,ycenter,xh,yh);

lastxs=xs;lastys=ys;

lastxm=xh;lastym=ym;

lastxh=xh;lastyh=yh;

lastdate=today;

if(h<12)hh=h;//将系统时间变换到0-11区间

else hh=h-12;

if(hh==0) strike_times=12;//计算整点时钟声数

else strike_times=hh;

if((s==0&&m==0)||flag){//判断是否整点,是否是主动刷新

if(counter

flag=true;

g.drawImage(gif2,115,35,this);

if(lasts!=s){

if(strike){

counter++;

danger.play();//播放闹铃声

}

if(strike)strike=false;

else strike=true;

}

}

else {

counter=0;

flag=false;

}

}

else

g.drawImage(gif1,115,35,this);

int timedelta;//记录当前时间与闹铃定时的时差

Integer currh,currm,currs;//分别记录当前的时、分、秒

timeNow=new Date();

currh=new Integer(timeNow.getHours());

currm=new Integer(timeNow.getMinutes());

currs=new Integer(timeNow.getSeconds());

//判断是否要更新当前显示的时间,这样可以避免文本框出现频率闪动

if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())

showhour.setText(currh.toString());

if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())

showmin.setText(currh.toString());

if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())

showsec.setText(currh.toString());

if(setalerm){ //判断是否设置了闹钟

//判断当前时间是否为闹钟所定的时间

if((currh.intValue()==fixh)&&(currm.intValue()==fixm)&&(currs.intValue()==fixs))

clickflag=true;

timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;

if((timedelta<60)&&(clickflag==true)){ //若当前时间与闹钟相差时间达到60秒

chirp.play();

g.drawImage(clock7,83,280,this);

}

else{

chirp.stop();

clickflag=false;

}

}

if(lasts!=s)

ipAu.play();//播放滴答声

lasts=s;

if(canPaint){

t+=1;

if(t==12)t=0;

}

canPaint=false;

dat=null;

}

public void start(){

if(timer==null){

timer=new Thread(this);//将timer实例化

timer.start();

}

}

public void stop(){

timer=null;

}

public void run(){

while(timer!=null){

try{timer.sleep(timeout);}catch(InterruptedException e){}

canPaint=true;

repaint();//刷新画面

}

timer=null;

}

public void update(Graphics g){ //采用双缓冲技术的update()方法

if(offscreen_buf==null)

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_buf.getGraphics();

offg.clipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ong.clipRect(1,1,599,419);

ong.drawImage(offscreen_buf,0,0,this);

}

public boolean action(Event evt,Object arg){ //按钮事件处理函数

if(evt.target instanceof Button){

String lable=(String)arg;

if(lable.equals("开")){

setalerm=true;

//获取输入的时间

fixh=Integer.valueOf(sethour.getText()).intValue();

fixm=Integer.valueOf(setmin.getText()).intValue();

fixs=Integer.valueOf(setsec.getText()).intValue();

clickflag=false;

}

if(lable.equals("关")){

setalerm=false;

if(chirp!=null)

chirp.stop();

clickflag=false;

}

return true;

}

return false;

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442934.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

摩托罗拉ex232java_摩托罗拉ex232r如何刷机?摩托罗拉ex232r评测

导语&#xff1a;随着 高科 技产业的发展&#xff0c;手机作为一个深受影响的产业&#xff0c;其竞争的激烈程度也是不言而喻的。市场好比战场&#xff0c;而为了在这个手机战场中赢 得胜 利&#xff0c;不论国内或者是国外的各大厂商也都全身心的投入到新技术的开发和新产品的…

JAVA捕捉输入格式异常_Java学习(四).异常处理

异常处理任何一个软件或程序都可能在运行的过程中出现故障&#xff0c;问题的关键是故障出现以后如何处理&#xff1f;谁来处理&#xff1f;怎样处理&#xff1f;处理后系统能否恢复正常的运行&#xff1f;本章在介绍Java处理这类问题基本方法的基础上&#xff0c;讨论包含异常…

php去除html属性,PHP如何去掉所有HTML标签?

PHP如何去掉所有HTML标签&#xff1f;在PHP中可以使用“strip_tags()”函数将字符串中的所有HTML标签去除&#xff0c;该函数用于从字符串中去除HTML和 PHP标记&#xff0c;其语法是“strip_tags(str)”&#xff0c;其参数str表示要进行操作的字符串&#xff0c;返回值为处理后…

php获取本机root,通过PHP执行root命令

慕村225694在尝试之前&#xff0c;请阅读整个文章&#xff0c;然后进行选择。使用二进制包装器(带有suid位)的解决方案1)创建一个脚本(最好是.sh)&#xff0c;其中包含要作为root用户运行的脚本。# cat > php_shell.sh < wrapper.c < #include #include int mai…

java寂静岭 攻略,GBA版《寂静岭》HARRY篇图文流程攻略

“Play Novel: Silent Hill”是KONAMI于2001年3月21日在GBA上推出的一款文字冒险游戏&#xff0c;剧情内容取自同社的恐怖冒险游戏——Silent Hill(《寂静岭》)。游戏基本上是纯粹的文字冒险游戏&#xff0c;过程中穿插着几段动画CG作为过场&#xff0c;游戏中绝大部分的图片和…

实验一熟悉matlab环境,数字信号处理报告实验一:熟悉MATLAB环境.doc

数字信号处理报告实验一&#xff1a;熟悉MATLAB环境.doc实验一熟悉MATLAB环境一 实验目的1. 熟悉MATLAB的主要操作命令。2. 学会简单的矩阵输入和数据读写。3. 掌握简单的绘图命令。4. 用MATLAB编程并学会创建函数。5. 观察离散系统的频率响应。二 实验内容2.用MATLAB实现下列序…

微擎cloud.mod.php,微擎“could not resolve ”、“could not resolve host”系列问题处理办法...

今天一个用户跟我说模块安装出现”could not resolve host:update.we10d.cn“报错&#xff0c;昨天在整理最近十个月用户搜索关键词排行的时候也发现”could not resolve host“、”could not resolve“还是占据不小的比例的。其实这类报错的处理方法大致相同&#xff0c;都是/…

php类如何变为静态调用,PHP类中静态方法如何调用非静态方法?_后端开发

php如何将字符串转成json_后端开发php将字符串转成json的方法是&#xff1a;可以通过json_encode()函数来实现。json_encode()函数用于对变量进行JSON编码&#xff0c;该函数如果执行成功&#xff0c;则返回JSON数据&#xff0c;反之则返回FALSE。PHP类中静态方法调用非静态方法…

PHP的注释标记是什么,html的注释标记是什么

html的注释标记是“”&#xff0c;用来在源文档中插入注释&#xff0c;注释的内容不会被显示在浏览器页面上。只有在文本编辑器中&#xff0c;或浏览器的“查看源代码”选项打开文档时&#xff0c;才能看到注释。本教程操作环境&#xff1a;windows7系统、HTML5版、Dell G3电脑…

linux 监控命令行输入,监控 Linux 容器性能的命令行神器

ctop是一个新的基于命令行的工具&#xff0c;它可用于在容器层级监控进程。容器通过利用控制器组(cgroup)的资源管理功能&#xff0c;提供了操作系统层级的虚拟化环境。该工具从cgroup收集与内存、CPU、块输入输出的相关数据&#xff0c;以及拥有者、开机时间等元数据&#xff…

linux子系统停止运行,linux 系统部署spring boot 退出连接端后项目停止运行问题

准备&#xff1a;Linux系统、jar包、一个启动项目文件(starup.sh)文件名自定义1、首先需要编辑starup.sh文件&#xff0c;这里我也是从网上找了一个比较好的内容如下&#xff1a;#!/bin/bash#这里可替换为你自己的执行程序&#xff0c;其他代码无需更改APP_NAMEkaRen.jar#使用说…

linux程序mktime无权限,linux中mktime算法解析

Linux源码中的mktime算法解析我们知道&#xff0c;从CMOS中读出来的系统时间并不是time_t类型&#xff0c;而是类似于struct tm那样&#xff0c;年月日时分秒是分开存储的。那么&#xff0c;要把它转化为系统便于处理的time_t类型&#xff0c;就需要算法进行转换。我们都知道我…

linux 无线网卡连接网络连接不上去,【已解决】Ubuntu 10.04 无线网卡 无法连接

----------------------------------搬家声明------------------------------------------------------------------------搬家声明--------------------------------------【已解决】Ubuntu 10.04 无线网卡 无法连接【问题】新装的Ubuntu 10.04&#xff0c;其他一切正常&#…

Linux 软件包管理器的目的是什么,Linux软件包的管理--RPM包管理器

在Linux中如何安装、升级、卸载软件&#xff0c;是我们日常运维中重要的组成部分。接下来一CentOS6系统为例&#xff0c;讲解如何使用 RPM 包管理器&#xff0c;yum包管理器以及源码的方式来管理我们的软件包。一、相关的背景知识许多程序的源程序&#xff0c;例如使用C语言编写…

嵌入式linux mongodb,小白在Ubuntu安装mongoDB与mongo-c-driver

目的&#xff1a;本人从事嵌入式软件&#xff0c;项目中需要使用MongoDB&#xff0c;最终需要熟悉c-driver的API&#xff0c;小白在搭建环境有些浪费时间&#xff0c;故写这文章让与我同样状态的开发人员学习学习。在Ubuntu上的安装mongodb方法一&#xff1a;sudo apt-get ins…

湘潭大学-软件工程-选择判断题复习

说明 期末考试单选题和判断题占30分&#xff0c;单选20&#xff0c;判断10分 单选题 选错误的 B依靠松散组合的互联网大众是无法开发出高质量软件产品的 D、所有命名都应尽量使用缩写 C、采用团队的组织方式 D、软件需求一旦确定就不允许变化 以下哪一项是通过运行程序…

c语言运算符ppt,C语言知识学习运算符.ppt

C语言知识学习运算符.ppt 第三章,C语言运算符,回顾,变量和常量的含义 熟悉基本数据类型 - int、char、float 和 double 使用算术运算符 理解类型转换 熟练使用 scanf 和 printf 函数,课程目标,算术运算符 增量运算符 逻辑运算符 关系运算符 按位运算符 C的特殊运算符 --“” “…

ionic判断android版本,$ionicplatform 判断是android还是ios?

一只名叫tom的猫安装环境首先应该安装好 node.js (略)&#xff0c;然后安装cordova、ionic等sudo npm install -g cordova ionic ios-sim创建不同类型的项目目前可以用blank&#xff0c;tabs&#xff0c;sidemenu三种ionic start myApp tabs常用插件cordova plugin add com.ion…

华为鸿蒙等不急了,华为最强巨作,鸿蒙OS+六摄+麒麟985,网友:等不及了!

原标题&#xff1a;华为最强巨作&#xff0c;鸿蒙OS六摄麒麟985&#xff0c;网友&#xff1a;等不及了&#xff01;华为最强旗舰机一般公认是Mate系列&#xff0c;虽然华为跟三星一样&#xff0c;也是走双旗舰&#xff0c;Mate和P系列都是旗舰机&#xff0c;但是华为和三星有所…

华为鸿蒙2.0操作页面,华为鸿蒙2.0开面界面确认,这一变化你可懂

最近一段时间关于华为鸿蒙手机端操作系统的消息非常多&#xff0c;但是由于目前仅是测试阶段&#xff0c;我们无法全面的了解这个全新的操作系统长什么样。在操作上有何不同等等&#xff0c;在这样的前提之下很多参与内测有朋友时不时的就会放出一些信息。5月5日有人放出了华为…