java开发之闹钟的实现代码
程序员文章站
2024-02-11 17:26:52
项目结构:
效果图一:
效果图二:
效果图三:
关于闹钟:
复制代码 代码如下:alarm hongten v1.0 使用说明:&nbs...
项目结构:
效果图一:
效果图二:
效果图三:
关于闹钟:
复制代码 代码如下:
alarm hongten v1.0
使用说明:
此文件夹中包括:
src文件夹 存放图片和声音文件夹,使用程序的时候,请不要删除此文件夹
alarm.jar 主要的功能程序jar包(要求安装jdk1.5或以上版本)
readme.txt 说明文档
1.菜单功能说明
文件—— >
上传铃声(还没实现)
退出 功能:退出程序
工具—— >
截图工具 功能:进行截图
更改背景 功能:更改背景
帮助—— >
关于程序 功能:查看程序说明
2.设置闹表
2.1.可以在响铃时间里设置闹表时间
2.2.还可以用快速设置,+1表示在系统时间的基础上增加一分钟,-1表示在系统时间的基础上减去一分钟 (+5,+30,-5,-30同样原理)
3.提示铃声
可以选择铃声类型,进行试听,试听是时候可以停止(该功能还没完善)
4.系统托盘
4.1.在主窗体隐藏的时候,单击左键,主窗体可以显示出来;
4.2.在主窗体隐藏的时候,单击右键,可以进行功能选择:退出,恢复主窗体可见,截图
4.3.在设置的闹表时间剩下30秒是时候,系统会自动提示信息(电脑右下角会弹出消息提示框)
4.4.在设置更改背景的时候,同样会弹出提示信息(所有的提示信息会自动隐退)
下面是代码部分
==========================================================
/alarm/src/com/b510/main/alarmmain.java
复制代码 代码如下:
package com.b510.main;
import com.b510.ui.main.myalarm;
/**
*
* @author hongten
*/
public class alarmmain {
public static void main(string[] args) {
new myalarm("alarm hongten v-1.0");
}
}
/alarm/src/com/b510/menu/alarmbackground.java
复制代码 代码如下:
package com.b510.menu;
/**
* @author hongten
*
* @time 2012-3-4 2012
*/
public class alarmbackground {
/**
* 默认的背景图片
*/
private static string background_default="src/image/background.png";
/**
* 水果系列
*/
private static string background_fruit="src/image/background1.png";
/**
* 自行车系列
*/
private static string background_bick="src/image/background2.png";
/**
* 背景图片的list
*/
private static string[] backgroundlist={background_default,background_fruit,background_bick};
/**
* 获取背景图片的list的长度
* @return 返回背景图片的list的长度
*/
public static int getbackgroundlistsize(){
return backgroundlist.length;
}
public static string getbackground(int bg){
return backgroundlist[bg];
}
}
/alarm/src/com/b510/menu/alarmfile.java
复制代码 代码如下:
package com.b510.menu;
/**
*
* @author hongten
* 菜单-文件
*/
public class alarmfile {
/**
* 退出程序
*/
public static void exit(){
system.exit(0);
}
}
/alarm/src/com/b510/menu/alarmhelp.java
复制代码 代码如下:
/*
* to change this template, choose tools | templates
* and open the template in the editor.
*/
package com.b510.menu;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jframe;
import com.b510.ui.main.myalarm;
/**
*
* @author hongten 菜单-帮助
*/
public class alarmhelp extends jframe {
/**
* 版本号
*/
private static final long serialversionuid = 5248482602468160509l;
public alarmhelp(string title) {
super(title);
initcomponents();
addwindowlistener(new windowadapter() {
public void windowclosing(windowevent e) {
alarmhelp.this.setvisible(false);
}});
}
/**
*关闭按钮
*/
private jbutton closebutton = new jbutton();
/**
* 应用程序名称
*/
javax.swing.jlabel apptitlelabel = new javax.swing.jlabel();
/**
* 版本号 前
*/
javax.swing.jlabel versionlabel = new javax.swing.jlabel();
/**
* 版本号
*/
javax.swing.jlabel appversionlabel = new javax.swing.jlabel();
/**
* 主页 前
*/
javax.swing.jlabel homepagelabel = new javax.swing.jlabel();
/**
* homepage
*/
javax.swing.jlabel apphomepagelabel = new javax.swing.jlabel();
/**
* 说明
*/
javax.swing.jlabel appdesclabel = new javax.swing.jlabel();
/**
* 图片
*/
javax.swing.jlabel imagelabel = new javax.swing.jlabel();
private void initcomponents() {
this.setvisible(true);
// 设置大小不能变
setresizable(false);
this.setlocation(myalarm.pointx+60, myalarm.pointy+190);// 设置窗体的初始位置
closebutton.addmouselistener(new mouseadapter() {
public void mouseentered(java.awt.event.mouseevent evt) {
closebutton.seticon(new imageicon("src/image/biao2.png"));
}
public void mouseexited(mouseevent evt){
closebutton.seticon(new imageicon("src/image/biao.png"));
}
});
apptitlelabel.setfont(apptitlelabel.getfont().derivefont(
apptitlelabel.getfont().getstyle() | java.awt.font.bold,
apptitlelabel.getfont().getsize() + 4));
apptitlelabel.settext("应用程序名称:"); // noi18n
apptitlelabel.setname("apptitlelabel"); // noi18n
versionlabel.setfont(versionlabel.getfont().derivefont(
versionlabel.getfont().getstyle() | java.awt.font.bold));
versionlabel.settext("版本号:"); // noi18n
versionlabel.setname("versionlabel"); // noi18n
appversionlabel.settext("1.0"); // noi18n
appversionlabel.setname("appversionlabel"); // noi18n
homepagelabel.setfont(homepagelabel.getfont().derivefont(
homepagelabel.getfont().getstyle() | java.awt.font.bold));
homepagelabel.settext("主页:"); // noi18n
homepagelabel.setname("homepagelabel"); // noi18n
apphomepagelabel.settext("http://www.cnblogs.com/hongten"); // noi18n
apphomepagelabel.setname("apphomepagelabel"); // noi18n
appdesclabel.settext("这是一个小应用程序,闹钟"); // noi18n
appdesclabel.setname("appdesclabel"); // noi18n
imagelabel
.seticon(new imageicon("src/image/about.png")); // noi18n
imagelabel.setname("imagelabel"); // noi18n
closebutton
.seticon(new imageicon("src/image/biao.png"));
javax.swing.grouplayout layout = new javax.swing.grouplayout(
getcontentpane());
getcontentpane().setlayout(layout);
layout
.sethorizontalgroup(layout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addgroup(
layout
.createsequentialgroup()
.addcomponent(imagelabel)
.addgap(18, 18, 18)
.addgroup(
layout
.createparallelgroup(
javax.swing.grouplayout.alignment.trailing)
.addgroup(
javax.swing.grouplayout.alignment.leading,
layout
.createsequentialgroup()
.addgroup(
layout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addcomponent(
versionlabel)
.addcomponent(
homepagelabel))
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addgroup(
layout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addcomponent(
appversionlabel)
.addcomponent(
apphomepagelabel)))
.addcomponent(
apptitlelabel,
javax.swing.grouplayout.alignment.leading)
.addcomponent(
appdesclabel,
javax.swing.grouplayout.alignment.leading,
javax.swing.grouplayout.default_size,
266,
short.max_value)
.addcomponent(
closebutton))
.addcontainergap()));
layout
.setverticalgroup(layout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addcomponent(imagelabel,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
short.max_value)
.addgroup(
layout
.createsequentialgroup()
.addcontainergap()
.addcomponent(apptitlelabel)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
appdesclabel,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.preferred_size)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addgroup(
layout
.createparallelgroup(
javax.swing.grouplayout.alignment.baseline)
.addcomponent(
versionlabel)
.addcomponent(
appversionlabel))
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addgroup(
layout
.createparallelgroup(javax.swing.grouplayout.alignment.baseline))
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addgroup(
layout
.createparallelgroup(
javax.swing.grouplayout.alignment.baseline)
.addcomponent(
homepagelabel)
.addcomponent(
apphomepagelabel))
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related,
19, short.max_value)
.addcomponent(closebutton)
.addcontainergap()));
pack();
}
}
/alarm/src/com/b510/menu/alarmtools.java
复制代码 代码如下:
/*
* to change this template, choose tools | templates
* and open the template in the editor.
*/
package com.b510.menu;
import mai.capture.desktopcapture;
/**
*
* @author hongten
* 菜单-工具
*/
public class alarmtools {
/** 定义截图功能 */
@suppresswarnings("unused")
private static desktopcapture desktopcapture;
/**
* 截图
*/
public static void screenshot(){
desktopcapture=new desktopcapture();
}
}
/alarm/src/com/b510/ui/main/myalarm.java
复制代码 代码如下:
package com.b510.ui.main;
import java.awt.color;
import java.awt.graphics;
import java.awt.point;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
import java.io.file;
import java.io.ioexception;
import java.util.calendar;
import javax.sound.sampled.audioformat;
import javax.sound.sampled.audioinputstream;
import javax.sound.sampled.audiosystem;
import javax.sound.sampled.dataline;
import javax.sound.sampled.sourcedataline;
import javax.swing.imageicon;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jpanel;
import com.b510.menu.alarmbackground;
import com.b510.menu.alarmfile;
import com.b510.menu.alarmhelp;
import com.b510.menu.alarmtools;
/**
*
* @author hongten
*/
public class myalarm extends jframe implements actionlistener {
/**
*+1
*/
private javax.swing.jbutton add1;
/**
* +30
*/
private javax.swing.jbutton add30;
/**
* +5
*/
private javax.swing.jbutton add5;
/**
* 时
*/
private javax.swing.jcombobox dorp_down_hours;
/**
* 分
*/
private javax.swing.jcombobox dorp_down_minute;
/**
* 秒
*/
private javax.swing.jcombobox drop_down_second;
/**
* 试听
*/
private javax.swing.jbutton listening_test;
/**
* 试听-停止
*/
private javax.swing.jbutton listening_test_stop;
/**
* 主panel
*/
private javax.swing.jpanel mainpanel;
/**
* 菜单bar
*/
private javax.swing.jmenubar menubar;
/**
* -1
*/
private javax.swing.jbutton minus1;
/**
* -30
*/
private javax.swing.jbutton minus30;
/**
* -5
*/
private javax.swing.jbutton minus5;
/**
* 当前时间
*/
private javax.swing.jlabel now;
/**
* 铃声
*/
private javax.swing.jcombobox ring_setup;
/**
* 截屏
*/
private javax.swing.jmenuitem screenshot;
/**
* 更改背景图片
*/
private javax.swing.jmenuitem changebackground;
/**
* 停止
*/
private javax.swing.jbutton stop;
/**
* 工具
*/
private javax.swing.jmenu tools;
/**
* 帮助
*/
private javax.swing.jmenu help;
/**
* 关于
*/
private javax.swing.jmenuitem about;
/**
* 退出
*/
private javax.swing.jmenuitem exit;
/**
* 上传铃声
*/
private javax.swing.jmenuitem uploadring;
/**
* wenjian
*/
private javax.swing.jmenu file;
/**
* 结果,即剩余时间
*/
private jlabel result;
/**
* 分割线
*/
private javax.swing.jseparator line;
/**
* 变量-->小时
*/
private string h;
/**
* 变量-->分钟
*/
private string m;
/**
* 变量-->秒
*/
private string s;
/**
* 线程的一个标志
*/
private boolean running = true;
/**
* 定义图盘图盘标志
*/
public boolean iconed = false;
/**
* 背景图片选择标志
*/
private int background = 0;
/**
* 获取result的秒数
*/
private int secondofresult;
/**
* 更改背景图片的标志
*/
private boolean flagofbackground = false;
/**
* myalarm的x坐标
*/
public static int pointx = 0;
/**
* myalarm的y坐标
*/
public static int pointy = 0;
/** 定义托盘 */
mytray mytray;
/**
* 版本号
*/
private static final long serialversionuid = -6601825053136983041l;
public myalarm(string title) {
this.settitle(title);
init();
}
/**
* 初始化背景图片
*/
public void initmainpanel() {
mainpanel = new jpanel() {
private static final long serialversionuid = 1l;
protected void paintcomponent(graphics g) {
imageicon icon = new imageicon(alarmbackground
.getbackground(background));
g.drawimage(icon.getimage(), 0, 0, 545, 463, null);
}
};
}
/**
* 主界面初始化
*/
public void init() {
initmainpanel();
now = new javax.swing.jlabel();
stop = new javax.swing.jbutton();
add1 = new javax.swing.jbutton();
add5 = new javax.swing.jbutton();
add30 = new javax.swing.jbutton();
minus1 = new javax.swing.jbutton();
minus5 = new javax.swing.jbutton();
minus30 = new javax.swing.jbutton();
listening_test = new javax.swing.jbutton();
listening_test_stop = new javax.swing.jbutton();
dorp_down_hours = new javax.swing.jcombobox();
dorp_down_minute = new javax.swing.jcombobox();
drop_down_second = new javax.swing.jcombobox();
ring_setup = new javax.swing.jcombobox();
menubar = new javax.swing.jmenubar();
file = new javax.swing.jmenu();
uploadring = new javax.swing.jmenuitem();
exit = new javax.swing.jmenuitem();
tools = new javax.swing.jmenu();
screenshot = new javax.swing.jmenuitem();
changebackground = new javax.swing.jmenuitem();
help = new javax.swing.jmenu();
about = new javax.swing.jmenuitem();
line = new javax.swing.jseparator();
result = new javax.swing.jlabel();
mainpanel.setname("mainpanel"); // noi18n
result.setforeground(color.red);
result.setname("result");
result.setvisible(false);
now.setfont(now.getfont().derivefont(
now.getfont().getstyle() | java.awt.font.bold,
now.getfont().getsize() + 70));
now.setname("now"); // noi18n
// 时间监听器,得到系统时间和设置好时间后,得到剩余时间
timelistener();
stop.settext("stop"); // noi18n
stop.setname("stop"); // noi18n
// 初始化的时候是不可见的
stop.setvisible(false);
stop.addactionlistener(this);
add1.settext("+1"); // noi18n
add1.setname("add1"); // noi18n
add1.addactionlistener(this);
add5.settext("+5"); // noi18n
add5.setname("add5"); // noi18n
add5.addactionlistener(this);
add30.settext("+30"); // noi18n
add30.setname("add30"); // noi18n
add30.addactionlistener(this);
minus1.settext("-1"); // noi18n
minus1.setname("minus1"); // noi18n
minus1.addactionlistener(this);
minus5.settext("-5"); // noi18n
minus5.setname("minus5"); // noi18n
minus5.addactionlistener(this);
minus30.settext("-30"); // noi18n
minus30.setname("minus30"); // noi18n
minus30.addactionlistener(this);
listening_test.settext("试听"); // noi18n
listening_test.setname("listening_test"); // noi18n
listening_test.addactionlistener(this);
listening_test_stop.settext("停止"); // noi18n
listening_test_stop.setname("listening_test_stop"); // noi18n
listening_test_stop.addactionlistener(this);
dorp_down_hours.setmodel(new javax.swing.defaultcomboboxmodel(
new string[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23" }));
dorp_down_hours.setname("dorp_down_hours"); // noi18n
dorp_down_hours.addactionlistener(this);
dorp_down_minute.setmodel(new javax.swing.defaultcomboboxmodel(
new string[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33",
"34", "35", "36", "37", "38", "39", "40", "41", "42",
"43", "44", "45", "46", "47", "48", "49", "50", "51",
"52", "53", "54", "55", "56", "57", "58", "59" }));
dorp_down_minute.setname("dorp_down_minute"); // noi18n
dorp_down_minute.addactionlistener(this);
drop_down_second.setmodel(new javax.swing.defaultcomboboxmodel(
new string[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33",
"34", "35", "36", "37", "38", "39", "40", "41", "42",
"43", "44", "45", "46", "47", "48", "49", "50", "51",
"52", "53", "54", "55", "56", "57", "58", "59" }));
drop_down_second.setname("drop_down_second"); // noi18n
drop_down_second.addactionlistener(this);
ring_setup.setmodel(new javax.swing.defaultcomboboxmodel(new string[] {
"铃声一", "铃声二", "铃声三", "铃声四", "铃声五", "铃声六", "铃声七" }));
ring_setup.setname("ring_setup"); // noi18n
// 主要的布局
omponentlayout();
menubar.setname("menubar"); // noi18n
file.settext("文件"); // noi18n
file.setname("file"); // noi18n
uploadring.settext("上传铃声"); // noi18n
uploadring.setname("uploadring"); // noi18n
uploadring.addactionlistener(this);
file.add(uploadring);
file.add(line);
exit.settext("退出");
exit.setname("exit"); // noi18n
exit.addactionlistener(this);
file.add(exit);
menubar.add(file);
tools.settext("工具"); // noi18n
tools.setname("tools"); // noi18n
screenshot.settext("截图工具"); // noi18n
screenshot.setname("screenshot"); // noi18n
screenshot.addactionlistener(this);
tools.add(screenshot);
changebackground.settext("更改背景");
changebackground.setname("changebackground");
changebackground.addactionlistener(this);
tools.add(changebackground);
menubar.add(tools);
help.settext("帮助"); // noi18n
help.setname("help"); // noi18n
about.settext("关于软件");
about.setname("about"); // noi18n
about.addactionlistener(this);
help.add(about);
menubar.add(help);
this.add(mainpanel);
setjmenubar(menubar);
this.setvisible(true);
this.setsize(550, 516);
// this.pack();
this.setlocationrelativeto(null);
this.setresizable(false);
// this.setlocation(470, 250);
// this.setdefaultcloseoperation(jframe.exit_on_close);
// 当点击"-"最小化按钮时,系统会最小化到托盘
addwindowlistener(new windowadapter() {
public void windowiconified(windowevent e) {
iconed = true;
setvisible(false);
}
public void windowclosing(windowevent e) {
// 当点击"x"关闭窗口按钮时,会询问用户是否要最小化到托盘
// 是,表示最小化到托盘,否,表示退出
int option = joptionpane.showconfirmdialog(myalarm.this,
"是否最小化到托盘?", "提示:", joptionpane.yes_no_option);
if (option == joptionpane.yes_option) {
iconed = true;
myalarm.this.setvisible(false);
} else {
alarmfile.exit();
}
}
});
// 初始化自定义托盘
mytray = new mytray(myalarm.this);
}
/**
* 组件的布局,不要轻易动啊
*/
private void omponentlayout() {
javax.swing.grouplayout mainpanellayout = new javax.swing.grouplayout(
mainpanel);
mainpanel.setlayout(mainpanellayout);
mainpanellayout
.sethorizontalgroup(mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addgroup(
javax.swing.grouplayout.alignment.trailing,
mainpanellayout
.createsequentialgroup()
.addcontainergap(170, short.max_value)
.addgroup(
mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addgroup(
mainpanellayout
.createsequentialgroup()
.addcomponent(
result)
.addcontainergap())
.addgroup(
javax.swing.grouplayout.alignment.trailing,
mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addgroup(
mainpanellayout
.createsequentialgroup()
.addcomponent(
now,
javax.swing.grouplayout.default_size,
483,
short.max_value)
.addcontainergap())
.addgroup(
javax.swing.grouplayout.alignment.trailing,
mainpanellayout
.createsequentialgroup()
.addgroup(
mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addgroup(
mainpanellayout
.createsequentialgroup()
.addcomponent(
dorp_down_hours,
javax.swing.grouplayout.preferred_size,
74,
javax.swing.grouplayout.preferred_size)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
dorp_down_minute,
javax.swing.grouplayout.preferred_size,
65,
javax.swing.grouplayout.preferred_size)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
drop_down_second,
javax.swing.grouplayout.preferred_size,
62,
javax.swing.grouplayout.preferred_size)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
stop))
.addgroup(
mainpanellayout
.createsequentialgroup()
.addcomponent(
add1)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
add5)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
add30)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
minus1)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
minus5)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
minus30))
.addgroup(
mainpanellayout
.createsequentialgroup()
.addcomponent(
ring_setup,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.preferred_size)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
listening_test)
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addcomponent(
listening_test_stop)))
.addgap(
73,
73,
73))))));
mainpanellayout
.setverticalgroup(mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.leading)
.addgroup(
javax.swing.grouplayout.alignment.trailing,
mainpanellayout
.createsequentialgroup()
.addgap(120, 120, 120)
.addcomponent(result)
.addgap(24, 24, 24)
.addcomponent(
now,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.default_size,
short.max_value)
.addgap(36, 36, 36)
.addgroup(
mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.baseline)
.addcomponent(
dorp_down_hours,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.preferred_size)
.addcomponent(
dorp_down_minute,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.preferred_size)
.addcomponent(
drop_down_second,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.preferred_size)
.addcomponent(stop))
.addpreferredgap(
javax.swing.layoutstyle.componentplacement.related)
.addgroup(
mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.baseline)
.addcomponent(add1)
.addcomponent(add5)
.addcomponent(add30)
.addcomponent(minus1)
.addcomponent(minus5)
.addcomponent(minus30))
.addgap(13, 13, 13)
.addgroup(
mainpanellayout
.createparallelgroup(
javax.swing.grouplayout.alignment.baseline)
.addcomponent(
ring_setup,
javax.swing.grouplayout.preferred_size,
javax.swing.grouplayout.default_size,
javax.swing.grouplayout.preferred_size)
.addcomponent(
listening_test)
.addcomponent(
listening_test_stop))
.addgap(58, 58, 58)));
}
/**
* 时间监听器,得到系统时间和设置好时间后,得到剩余时间
*/
public void timelistener() {
new thread(new runnable() {// 设置一个线程
public void run() {
while (true) {
try {
thread.sleep(1000);
} catch (exception e) {
e.printstacktrace();
}
initmainpanel();
now.settext(now());// 得到系统时间
result.settext(surplus_time());// 得到剩余时间
}
}
}).start();
}
/**
* 播放声音的监听器
*/
public void mylistener() {
new thread(new runnable() {// 设置一个线程
public void run() {
while (true) {
try {
thread.sleep(1000);
} catch (exception e) {
e.printstacktrace();
}
executesound();// 播放声音
}
}
}).start();
}
/**
* 获取返回结果
*
* @return result值
*/
public string surplus_time() {
string r = null;
// 获取系统时,分,秒
int h = gethour();
int m = getmunite();
int s = getsecond();
// 获取设置的响铃时间
int dh = 0;
int dm = 0;
int ds = 0;
if (dorp_down_hours.getselectedindex() != 0) {
dh = dorp_down_hours.getselectedindex() - 1;
}
if (dorp_down_minute.getselectedindex() != 0) {
dm = dorp_down_minute.getselectedindex() - 1;
}
if (drop_down_second.getselectedindex() != 0) {
ds = drop_down_second.getselectedindex() - 1;
}
int hour = dh - h;
int min = dm - m;
int sec = ds - s;
if (hour == 0) {
if (min == 0) {
if (sec == 0) {
r = "时间到了哦!";
}
if (sec < 0) {
hour += 23;
min += 59;
sec += 59;
}
}
if (min < 0) {
hour += 23;
if (sec < 0) {
min -= 1;
sec += 59;
}
min += 60;
}
if (min >= 0) {
if (sec < 0 || sec == 0) {
min -= 1;
sec += 59;
}
if (sec > 0) {
// sec=sec;
}
}
}
if (hour < 0) {
if (min <= 0) {
if (sec <= 0) {
hour -= 1;
min += 59;
sec += 59;
}
}
if (min > 0) {
if (sec <= 0) {
min -= 1;
sec += 59;
}
}
hour += 24;
}
if (hour > 0) {
if (min == 0) {
if (sec <= 0) {
hour -= 1;
min += 59;
sec += 59;
}
}
if (min < 0) {
if (sec < 0) {
min -= 1;
sec += 59;
}
min += 60;
hour -= 1;
}
if (min > 0) {
if (sec < 0 || sec == 0) {
min -= 1;
sec += 59;
}
}
}
if (sec == 30 && min == 0 && hour == 0) {
setsecondofresult(sec);
}
r = new string("剩:" + hour + "时" + min + "分" + sec + "秒");
if (hour == 0 && min == 0 && sec < 0) {
r = "时间到了哦!";
}
// result.settext(r);
return r;
}
public int getsecondofresult() {
return secondofresult;
}
public void setsecondofresult(int sec) {
this.secondofresult = sec;
}
public boolean getflagofbackground() {
return flagofbackground;
}
public void setflagofbackground(boolean flag) {
this.flagofbackground = flag;
}
/**
* 时间到了的时候就播放声音
*/
public void executesound() {
// 获取系统时,分,秒
int h = gethour();
int m = getmunite();
int s = getsecond();
// 获取设置的响铃时间
int dh = 0;
int dm = 0;
int ds = 0;
if (dorp_down_hours.getselectedindex() != 0) {
dh = dorp_down_hours.getselectedindex() - 1;
}
if (dorp_down_minute.getselectedindex() != 0) {
dm = dorp_down_minute.getselectedindex() - 1;
}
if (drop_down_second.getselectedindex() != 0) {
ds = drop_down_second.getselectedindex() - 1;
}
int hour = dh - h;
int min = dm - m;
int sec = ds - s;
if (hour == 0 && min == 0 && sec == 0) {
// 主窗体设置为可见
setvisible(true);
// 设置窗口前端显示
setextendedstate(jframe.normal);
setalwaysontop(true);
// 播放声音
new thread(new alarmsound(ring_setup.getselectedindex())).start();
// 震动
shack();
}
}
/**
* 得到系统时间当前时间,并返回
*
* @return 返回系统当前时间
*/
public string now() {
calendar calendar = calendar.getinstance();
int hour = calendar.get(calendar.hour_of_day);
int min = calendar.get(calendar.minute);
int sec = calendar.get(calendar.second);
// 小于10的时候,在前面加0
string h = hour < 10 ? "0" : "";
string m = min < 10 ? "0" : "";
string s = sec < 10 ? "0" : "";
string current = new string(h + hour + ":" + m + min + ":" + s + sec);
return current;
}
/**
* 布局下面的按钮和下拉选项的监听器
*/
@suppresswarnings("deprecation")
@override
public void actionperformed(actionevent e) {
/**
* 获取dorp_down_hours,dorp_down_minute,drop_down_second的值
*/
if (e.getsource() == dorp_down_hours) {
valuejudgment();
}
if (e.getsource() == dorp_down_minute) {
valuejudgment();
}
if (e.getsource() == drop_down_second) {
valuejudgment();
}
if (e.getsource() == stop) {
stopactionperformed();
}
if (e.getsource() == add1) {
add1action();
mylistener();
}
if (e.getsource() == add5) {
add5action();
mylistener();
}
if (e.getsource() == add30) {
add30action();
mylistener();
}
if (e.getsource() == minus1) {
minus1action();
mylistener();
}
if (e.getsource() == minus5) {
minus5action();
mylistener();
}
if (e.getsource() == minus30) {
minus30action();
mylistener();
}
alarmsound alarmsound = new alarmsound(ring_setup.getselectedindex());
thread alarmthread = new thread(alarmsound);
if (e.getsource() == listening_test) {
// 试听
if (!alarmthread.isalive()) {
alarmthread.start();
}
if (running) {
alarmthread.resume();
}
listening_test.setenabled(false);
}
if (e.getsource() == listening_test_stop) {
// 停止试听
listening_test.setenabled(true);
if (running) {
alarmthread.suspend();
}
running = !running;
}
if (e.getsource() == uploadring) {
// 上传文件
}
if (e.getsource() == exit) {
// 退出程序
alarmfile.exit();
}
if (e.getsource() == screenshot) {
// 主窗体隐藏
iconed = true;
myalarm.this.setvisible(false);
// 开始截图
alarmtools.screenshot();
}
if (e.getsource() == changebackground) {
changebg();
}
if (e.getsource() == about) {
pointx = getmyalarmx();
pointy = getmyalarmy();
new alarmhelp("关于程序");
}
}
/**
* 更改背景图片</br> 当点击工具-- >更改背景,这里就可以处理更改背景图片
*/
private void changebg() {
setflagofbackground(true);
if (background < alarmbackground.getbackgroundlistsize()) {
iconed = true;
setvisible(false);
if (background != alarmbackground.getbackgroundlistsize() - 1) {
++background;
} else {
background = 0;
}
initmainpanel();
}
}
/**
* 点击-30按钮的时候执行的动作
*/
private void minus30action() {
ishmszero();
if (dorp_down_minute.getselectedindex() - 30 < 0) {
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() + 30);// 60-30
if (dorp_down_hours.getselectedindex() == 0) {
dorp_down_hours.setselectedindex(24);// 设置为23
} else {
// 小时数-1
dorp_down_hours.setselectedindex(dorp_down_hours
.getselectedindex() - 1);
}
} else {
// 分钟数-30
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() - 30);
}
valuejudgment();
}
/**
* 点击-5按钮的时候执行的动作
*/
private void minus5action() {
ishmszero();
if (dorp_down_minute.getselectedindex() - 5 < 0) {
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() + 55);// 60-5
if (dorp_down_hours.getselectedindex() == 0) {
dorp_down_hours.setselectedindex(24);// 设置为23
} else {
// 小时数-1
dorp_down_hours.setselectedindex(dorp_down_hours
.getselectedindex() - 1);
}
} else {
// 分钟数-5
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() - 5);
}
valuejudgment();
}
/**
* 点击-1按钮的时候执行的动作
*/
private void minus1action() {
ishmszero();
if (dorp_down_minute.getselectedindex() - 1 == 0) {
dorp_down_minute.setselectedindex(60);// 设置为59
if (dorp_down_hours.getselectedindex() == 0) {
dorp_down_hours.setselectedindex(24);// 设置为23
} else {
// 小时数-1
dorp_down_hours.setselectedindex(dorp_down_hours
.getselectedindex() - 1);
}
} else {
// 分钟数-1
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() - 1);
}
valuejudgment();
}
/**
* 判断dorp_down_hours,dorp_down_minute,drop_down_second当前是否为0,即:"关闭"
*/
private void ishmszero() {
// 如果小时数还没有设置,那么就设置为当前小时数
if (dorp_down_hours.getselectedindex() == 0) {
dorp_down_hours.setselectedindex(gethour() + 1);
}
// 如果分钟数还没有设置,那么就设置为当前分钟数
if (dorp_down_minute.getselectedindex() == 0) {
dorp_down_minute.setselectedindex(getmunite() + 1);
}
// 如果秒钟还没有设置,那么就设置为当前秒钟
if (drop_down_second.getselectedindex() == 0) {
drop_down_second.setselectedindex(getsecond());
}
}
/**
* 点击+30按钮的时候执行的动作
*/
private void add30action() {
ishmszero();
if (dorp_down_minute.getselectedindex() + 30 > 60) {
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() - 30);// +30-60
if (dorp_down_hours.getselectedindex() > 23) {
dorp_down_hours.setselectedindex(1);// 设置为00
} else {
// 小时数+1
dorp_down_hours.setselectedindex(dorp_down_hours
.getselectedindex() + 1);
}
} else {
// 分钟数+30
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() + 30);
}
valuejudgment();
}
/**
* 点击+5按钮的时候执行的动作
*/
private void add5action() {
ishmszero();
if (dorp_down_minute.getselectedindex() + 5 > 60) {
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() - 55);// +5-60
if (dorp_down_hours.getselectedindex() > 23) {
dorp_down_hours.setselectedindex(1);// 设置为00
} else {
// 小时数+1
dorp_down_hours.setselectedindex(dorp_down_hours
.getselectedindex() + 1);
}
} else {
// 分钟数+5
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() + 5);
}
valuejudgment();
}
/**
* 点击+1按钮的时候执行的动作
*/
private void add1action() {
ishmszero();
if (dorp_down_minute.getselectedindex() + 1 > 60) {
dorp_down_minute.setselectedindex(1);// 设置为00
if (dorp_down_hours.getselectedindex() > 23) {
dorp_down_hours.setselectedindex(1);// 设置为00
} else {
// 小时数+1
dorp_down_hours.setselectedindex(dorp_down_hours
.getselectedindex() + 1);
}
} else {
// 分钟数+1
dorp_down_minute.setselectedindex(dorp_down_minute
.getselectedindex() + 1);
}
valuejudgment();
}
/**
* 给h,m,s三个变量赋值,并判断他们的值
*/
private void valuejudgment() {
h = dorp_down_hours.getselecteditem().tostring();
m = dorp_down_minute.getselecteditem().tostring();
s = drop_down_second.getselecteditem().tostring();
hsmcheck();
}
/**
* 检查时,分,秒的值,如果都不是"关闭",那么</br>
*
* <li>stop按钮要显示出来</li><br/>
* <li>result要显示出来剩余时间</li><br/>
* <li>ring_setup要设置为不可用</li> <li>listening_test按钮为不可用</li> <li>
* listening_test_stop按钮为不可用</li>
*/
private void hsmcheck() {
if (h != "关闭" && m != "关闭" && s != "关闭") {
stop.setvisible(true);
result.setvisible(true);
ring_setup.setenabled(false);
listening_test.setenabled(false);
listening_test_stop.setenabled(false);
}
}
/**
* stop按钮的动作: <li>
* 把dorp_down_hours,dorp_down_minute,drop_down_second的值设置为"关闭"</li> <li>
* 隐藏result</li> <li>ring_setup设置为可用</li> <li>listening_test按钮为可用</li> <li>
* listening_test_stop按钮为可用</li> <li>stop按钮设置为不可见</li><li>停止声音播放</li>
*/
private void stopactionperformed() {
dorp_down_hours.setselectedindex(0);
dorp_down_minute.setselectedindex(0);
drop_down_second.setselectedindex(0);
result.setvisible(false);
ring_setup.setenabled(true);
listening_test.setenabled(true);
listening_test_stop.setenabled(true);
// 这里要停止声音
stop.setvisible(false);
}
/**
* 获取当前小时数
*
* @return 返回当前小时数
*/
private int gethour() {
return calendar.getinstance().get(calendar.hour_of_day);
}
/**
* 获取当前分钟数
*
* @return 返回当前分钟数
*/
private int getmunite() {
return calendar.getinstance().get(calendar.minute);
}
/**
* 获取当前秒钟数
*
* @return 返回当前秒钟数
*/
private int getsecond() {
return calendar.getinstance().get(calendar.second);
}
/**
* 实现震动功能
*/
public void shack() {
point p = myalarm.this.getlocation();
int x = (int) p.getx();
int y = (int) p.gety();
for (int i = 0; i < 5000; i++) {
myalarm.this.setlocation(x - 5, y);
myalarm.this.setlocation(x, y + 5);
myalarm.this.setlocation(x + 5, y);
myalarm.this.setlocation(x, y - 5);
}
myalarm.this.setlocation(x, y);
}
/**
* 获取myalarm的x坐标
*
* @return 返回myalarm的x坐标
*/
public int getmyalarmx() {
return (int) getlocation().getx();
}
/**
* 获取myalarm的y坐标
*
* @return 返回myalarm的y坐标
*/
public int getmyalarmy() {
return (int) myalarm.this.getlocation().gety();
}
/**
* 闹铃类
*
* @author hongten
*
* @time 2012-3-2 2012
*/
class alarmsound implements runnable {
private string temalarm;
private string alarm0path = "src/image/sound/alarm0.wav";//
private string alarm1path = "src/image/sound/alarm1.wav";//
private string alarm2path = "src/image/sound/alarm2.wav";//
private string alarm3path = "src/image/sound/alarm3.wav";//
private string alarm4path = "src/image/sound/alarm4.wav";//
private string alarm5path = "src/image/sound/alarm5.wav";//
private string alarm6path = "src/image/sound/alarm6.wav";//
private string alarm7path = "src/image/sound/alarm7.wav";//
private string alarm8path = "src/image/sound/alarm8.wav";//
public alarmsound(int a) {
switch (a) {
case 0:
temalarm = alarm0path;
break;
case 1:
temalarm = alarm1path;
break;
case 2:
temalarm = alarm2path;
break;
case 3:
temalarm = alarm3path;
break;
case 4:
temalarm = alarm4path;
break;
case 5:
temalarm = alarm5path;
break;
case 6:
temalarm = alarm6path;
break;
case 7:
temalarm = alarm7path;
break;
case 8:
temalarm = alarm8path;
break;
}
}
// 读取声音文件,并且播放出来
public void run() {
file soundfile = new file(temalarm);
audioinputstream audioinputstream = null;
try {
audioinputstream = audiosystem.getaudioinputstream(soundfile);
} catch (exception e1) {
e1.printstacktrace();
return;
}
audioformat format = audioinputstream.getformat();
sourcedataline auline = null;
dataline.info info = new dataline.info(sourcedataline.class, format);
try {
auline = (sourcedataline) audiosystem.getline(info);
auline.open(format);
} catch (exception e) {
e.printstacktrace();
return;
}
auline.start();
int nbytesread = 0;
byte[] abdata = new byte[512];
try {
while (nbytesread != -1) {
nbytesread = audioinputstream
.read(abdata, 0, abdata.length);
if (nbytesread >= 0)
auline.write(abdata, 0, nbytesread);
}
} catch (ioexception e) {
e.printstacktrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
}
/alarm/src/com/b510/ui/main/mytray.java
复制代码 代码如下:
package com.b510.ui.main;
import java.awt.awtexception;
import java.awt.image;
import java.awt.menuitem;
import java.awt.popupmenu;
import java.awt.systemtray;
import java.awt.trayicon;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
import javax.swing.imageicon;
import javax.swing.jframe;
import com.b510.menu.alarmfile;
import com.b510.menu.alarmtools;
public class mytray implements actionlistener, mouselistener {
private image icon;// 图标
private trayicon trayicon;
private systemtray systemtray;// 系统托盘
private myalarm myalarm; // 托盘所属主窗体
private popupmenu pop = new popupmenu(); // 弹出菜单
// 菜单选项
/** 截图 */
private menuitem screenshot = new menuitem("screenshot");
/** 还原 */
private menuitem open = new menuitem("restore");
/** 退出*/
private menuitem exit =new menuitem("exit");
public mytray(myalarm myalarm) {
this.myalarm = myalarm;
// 得到托盘的图标
icon = new imageicon(this.getclass().getclassloader().getresource(
"image/mytray.png")).getimage();
if (systemtray.issupported()) {
systemtray = systemtray.getsystemtray();
// 设置鼠标经过图标时,显示的内容
trayicon = new trayicon(icon, "open alarm", pop);
pop.add(screenshot);
pop.add(open);
pop.add(exit);
// 添加系统托盘
try {
systemtray.add(trayicon);
} catch (awtexception e1) {
e1.printstacktrace();
trayicon.addmouselistener(this);
}
}
displayinfolistener();
trayicon.addmouselistener(this);
exit.addactionlistener(this);
open.addactionlistener(this);
screenshot.addactionlistener(this);
}
@override
public void actionperformed(actionevent e) {
if(e.getsource()==exit){
//退出系统
alarmfile.exit();
}else if (e.getsource() == open) {
// 单点击菜单中的"还原"选项时,还原窗口
//displayinfo();
//trayicon.displaymessage("温馨提示", "hello,world", trayicon.messagetype.info);
myalarm.iconed = false;
friendlistset(true);
} else if (e.getsource() == screenshot) {
// 但点击“截图”选项时,进行截图
alarmtools.screenshot();
}
}
@override
public void mouseclicked(mouseevent e) {
// 但鼠标点击一次的时候,进行弹出窗口
if (e.getclickcount() == 1 && e.getbutton() != mouseevent.button3) {
if (!myalarm.isvisible()) {
friendlistset(true);
} else {
friendlistset(false);
}
}
// 但鼠标点击两次的时候,进行弹出窗口
// 如果窗口有显示,则隐藏窗口,否则显示窗口
if (e.getclickcount() == 2 && e.getbutton() != mouseevent.button3) {
if (!myalarm.isvisible()) {
friendlistset(true);
} else {
friendlistset(false);
}
}
}
/**
* 设置friendlist的可见性
*/
private void friendlistset(boolean flag) {
myalarm.setvisible(true);
myalarm.setextendedstate(jframe.normal);
}
public void displayinfolistener() {
new thread(new runnable() {// 设置一个线程
public void run() {
while (true) {
try {
thread.sleep(1000);
} catch (exception e) {
e.printstacktrace();
}
if(myalarm.getsecondofresult()==30){
trayicon.displaymessage("温馨提示","距闹钟设置时间还剩:\n00 时 00 分 " +myalarm.getsecondofresult() + " 秒",trayicon.messagetype.info);
myalarm.setsecondofresult(29);
}
if(myalarm.getflagofbackground()){
trayicon.displaymessage("温馨提示","您已经更改了背景,点击托盘图标\n可以看到效果,右键托盘图标\n可以进行功能选择。",trayicon.messagetype.info);
myalarm.setflagofbackground(false);
}
}
}
}).start();
}
@override
public void mouseentered(mouseevent arg0) {
// todo auto-generated method stub
}
@override
public void mouseexited(mouseevent arg0) {
// todo auto-generated method stub
}
@override
public void mousepressed(mouseevent arg0) {
// todo auto-generated method stub
}
@override
public void mousereleased(mouseevent arg0) {
// todo auto-generated method stub
}
}
/alarm/src/com/b510/ui/up/alarmshownow.java
package com.b510.ui.up;
import java.util.calendar;
/**
*
* @author hongten
* 主界面区-显示现在的时间
*/
public class alarmshownow {
/**
* 获得系统的当前时间,并返回
* @return 返回系统当前时间
*/
public static string now() {
calendar cal = calendar.getinstance();
int hour = cal.get(calendar.hour_of_day);
int min = cal.get(calendar.minute);
int sec = cal.get(calendar.second);
string current = new string(hour+ ":" + min + ":" + sec);
return current;
}
public static void main(string args[]){
system.out.println(now());
}
}