多功能日历
摘要
本文以研究日历基础功能为目的,采用JAVA语言编写。为实现简单的日历查询和跳转,在此基础上外加换肤、标签显示、闹钟等功能。
通过GridLayout布局类来完成对窗口的布局设计。在左侧面板上,通过重载paintComponent方法,利用Graphics2D类将时钟绘制在面板上,达到绘制出时钟的目的。而利用getComponent中的setBackground方法实现局部换肤。当用户点击日期,调用getTips显示Label信息。为闹钟按钮设计一个监听事件,一个弹出窗口,接收TextField输入的时间,并调用NiceTimer类计时,计时结束时调用JAVA中自带的palymusic方法播放音乐。
本文在日历功能的基础上拓展了记事、时钟、闹钟定时等日常常用功能,解决记事本记事不够便捷、闹钟定时太过单一、日历界面不够美观、交互性差等问题,综合与人们生活相关的软件,统一管理供用户使用,从而达到本项目的日历功能研发拓展的目的。结果表明该日历小程序具有可行性,可以实现多种日常常用功能,且综合性能好、用户满意度高。
详细功能设计
时钟绘制功能模块
NiceClock类,继承JPanel类,通过重写父类的printComponent方法将表盘绘制在面板上。而Graphics2D类拥用更强大的二维图形处理能力,提供、坐标转换、颜色管理以及文字布局等更精确的控制。可以通过设定和修改状态属性,指定画笔宽度和画笔的连接方式;设定平移、旋转、缩放或修剪变换图形;以及设定填充图形的颜色和图案等。图形状态属性用特定的对象存储。
绘制过程包括:
(1) 打开抗锯齿,放入背景图片用set background绘制指定图像中当前可用的图像。图像的左上角位于此图形上下文坐标空间的 (x, y)。以指定的背景色绘制透明像素。此操作等同于用给定颜色填充指定图像宽度和高度的矩形,然后在其上绘制图像,但此操作效率更高。
(2) 计算表盘半径,根据角度绘制数字与刻度。
(3) 根据角度计算出弧度绘制时针与分针当时刻下的旋转位置。
下面展示一些 核心代码
。
Graphics2D G2D = (Graphics2D)G;
G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
G2D.setColor(Color.RED);
G2D.drawImage(I.getImage(), 0, 0, getWidth(), getHeight(), I.getImageObserver());
G2D.setTransform(old);
Calendar C = Calendar.getInstance();
int hour = C.get(Calendar.HOUR_OF_DAY);
int minute = C.get(Calendar.MINUTE);
int second = C.get(Calendar.SECOND);
double hour_angle = (hour - 12 + minute / 60d) * 360d / 12d;
G2D.rotate(Math.toRadians(hour_angle), x_Wideth, y_Wideth);
int x_hour_array[] = {
x_Wideth, x_Wideth + 9, x_Wideth, x_Wideth – 9};
int y_hour_array[] = {
65, y_Wideth, y_Wideth + 10, y_Wideth };
G2D.drawPolygon(x_hour_array, y_hour_array, x_hour_array.length);
G2D.setTransform(old);
double minute_angle = (minute + second / 60d) * 360d / 60d;
G2D.rotate(Math.toRadians(minute_angle), x_Wideth, y_Wideth);
int x_minute_array[] = {
x_Wideth, x_Wideth + 6, x_Wideth, x_Wideth – 6};
int y_minute_array[] = {
45, y_Wideth, y_Wideth + 12, y_Wideth
};
G2D.drawPolygon(x_minute_array, y_minute_array, x_minute_array.length);
G2D.setTransform(old);
G2D.setBackground(Color.WHITE);//设置背景颜色
主菜单功能模块
在NiceMenubar类中,负责控制万年历所有的操作功能。首先在容器第一行添加菜单条JMenuBar,而JMenuBar中可有若干菜单JMenu,一个菜单中可有多个菜单项JMenuItem,通过对这些菜单项设置监听事件从而达到实现各个功能。
Start菜单条中,设置四个菜单项,分别为SignUp(注册)、SignIn(登录) 、Skin(换肤)、Exit(退出系统)四个功能,当鼠标点击时便会触发事件实现对应功能,Skin菜单项中嵌套四个菜单项以实现4种不同的底色。
Function菜单条中实现一些基础功能,如闹钟定时、跳转上一年下一年或指定年份等功能。
Help菜单条中显示一些个人信息。
下面展示一些 内联代码片
。
public class NiceMenubar extends JMenuBar implements ActionListener{
private static final long serialVersionUID = 1L;
NicePanel NP = new NicePanel();
NiceClock NC;
JMenu Menu_Start = new JMenu("Start(S)");
JMenu Menu_Function = new JMenu("Function(F)");
JMenu Menu_Help = new JMenu(" Help(H)");
JMenuItem Item_signIn = new JMenuItem("SignIn");
JMenuItem Item_signUp = new JMenuItem("SignUp");
JMenu Item_changeSkin = new JMenu("Skin");
JMenuItem Item_Exit = new JMenuItem("Exit");
JMenuItem Item_Alarm = new JMenuItem("闹钟");
JMenuItem Item_Skip = new JMenuItem("跳转");
JMenuItem Item_whiteSkin = new JMenuItem("QuietWhite");
JMenuItem Item_blackSkin = new JMenuItem("PureBlack");
JMenuItem Item_darkSkin = new JMenuItem("VesperalDark");
JMenuItem Item_yellowSkin = new JMenuItem("SoftYellow");
}
日历功能
NicePanel类,用for循环将按钮加入到面板里并设置按钮颜色和字体。在Nicebase类中计算出当前年份是否为闰年,计算不同月份的的不同天数。
refresh方法用来重新获取日历并显示。在日历界面中点击NOW按钮可实现跳转当前月份,点击上一月或下一月可以实现月份跳转。而点击日历上的日期则会调用NiceLabel类中的标签显示功能,显示点击日期对应的节日信息、二十四节气信息、宜忌信息等标签。
下面展示一些 内联代码片
。
NiceLabels(){
Label_Alarm.setFont(new Font("Arove", 1, 30));
Label_Alarm.setForeground(Color.BLUE);
Label_Remind.setFont(new Font("Arove", 1, 30));
Label_Remind.setForeground(Color.BLUE);
Label_Tips.setFont(new Font("Arove", 1, 30));
Label_Tips.setForeground(Color.BLUE);
setLayout(new GridLayout(3, 1));
add(Label_Remind);
add(Label_Tips);
add(Label_Alarm);
}
//更新日期函数
void refresh()
{
String day[] = NB.getCalendar();
String month;
for(int i = 0; i<42; i++)
{
Button_Day[i].setForeground(Color.CYAN);
Button_Day[i].setFont(new Font("Arove", 1, 22));
Button_Day[i].setText(day[i]);
}
闹钟模块
当点击菜单的闹钟按钮时,弹出设定时间窗口。根据输入的当完成设定时间点击确定时,计算当前时间与输入时间的差值,实现倒计时,并在输出缓冲区内输出剩余时间。当剩余时间为零时调用musicStuff类中的playmusic方法,播放bg1.wav音频即可达到实现闹钟功能。
下面展示一些 内联代码片
。
public void actionPerformed(ActionEvent e){
if(e.getSource() == Button_Set){
this.dispose();
new NiceTimer(Integer.valueOf(Textfield_H.getText()),
Integer.valueOf(Textfield_M.getText()), NL).run();
System.out.print("ok");
String filepath = "bg1.wav";
musicStuff musicObject = new musicStuff();
musicObject.playMusic(filepath); }
}