java课程设计报告简单日历程序.docx
《java课程设计报告简单日历程序.docx》由会员分享,可在线阅读,更多相关《java课程设计报告简单日历程序.docx(16页珍藏版)》请在冰豆网上搜索。
java课程设计报告简单日历程序
课程设计题目
2.题目说明
通过编写一个基于JAVA的应用系统综合实例,自定义一个日历组件显示日期和时间并进行适当的功能扩充,实践Java语言编程技术。
3.系统设计
2.1设计目标
一个完整的程序应具有以下功能:
1)显示当月日历、当前日期、当前时间;
2)可查寻任意月以及任意年的日历;
3)使用图形化界面能够弹出对话框;
5)正常退出程序。
2.2设计思想
设计一个类用来构成日历系统的主窗口,然后编写一个框架类显示时间和提示信息。
在设计中应用了多种容器和控件。
2.3系统模块划分
publicstaticclassCalendarTable(日历类)
publicstaticvoidmain(String[]args)
publicstaticclassRollbyJFrame
(滚动时间类)
publicclassMyCalendarextendsJApplet
(主日历框架类)
图1:
简易日历的程序结构图
2.3.1初始化:
publicvoidinit()完成界面初始化,形成一个以挂历形式显示当前日期的窗口。
2.3.2日历描述:
(1)publicvoidupdateView()改变日期后完成更新界面;
(2)抽象类java.util.Calendar获取系统日期并传递日期数据而且在人工改变日期后得出当天是周几;
(3)publicstaticvoidmain(String[]args)
主函数完成系统各算法的调用并对主窗口的一些属性进行设置;
2.3.3滚动时间:
将时间以文本的形式在文本框中滚动播出,并能改变滚动的速度。
4.使用类及接口
仅仅简单说明类的功能,详细资料请参看《JavaTM 2 PlatformStandard Ed.6》的电子文档,常规的接口与包则省略不屑。
//以下是日历程序块中使用的类
packagefancy;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;//主要用此包中的日期和时间类
importjavax.swing.*;
importjavax.swing.event.*;
importjavax.swing.table.*;
//以下是对滚动时间程序块所使用的类和接口,
用到定时器类Timer
importjava.awt.Color;
importjava.awt.FlowLayout;
importjava.awt.event.ActionListener;
importjava.awt.event.ActionEvent;
importjava.awt.event.FocusListener;、
//用于监听鼠标单击(焦点改变)事件
importjava.awt.event.FocusEvent;
//用于响应鼠标单击(焦点改变)事件、
importjavax.swing.JFrame;
importjavax.swing.JTextField;
importjavax.swing.JSpinner;
//让用户从一个有序序列中选择一个数字或者一个对象值的单行输入字段。
importjavax.swing.JPanel;
importjavax.swing.Timer;
//在指定时间间隔触发一个或多个ActionEvent,创建一个Timer对象,在该对象上注册一个或多个动作侦听器,以及使用start方法启动该计时器。
并配合事件监听器支持时间的滚动播放。
importjavax.swing.event.ChangeListener;
importjavax.swing.event.ChangeEvent;
5.运行结果与分析
图2:
初始界面显示日历。
图3:
点击查看时间按钮,
弹出时间消息对话框。
图4:
滚动显示当前时间。
6.程序源代码
/**
*(#)MyCalendar.java
*authorfancy
*/
//日历使用的类
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.ponent;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.FocusEvent;
importjava.awt.event.FocusListener;
importjava.util.Calendar;
importjavax.swing.JApplet;
importjavax.swing.utton;
importjavax.swing.oBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JSpinner;
importjavax.swing.JTable;
importjavax.swing.JTextField;
importjavax.swing.ListSelectionModel;
importjavax.swing.Timer;
importjavax.swing.event.ChangeEvent;
importjavax.swing.event.ChangeListener;
importjavax.swing.table.AbstractTableModel;
importjavax.swing.table.TableCellRenderer;
importjavax.swing.table.TableModel;
//日历
publicclassMyCalendarextendsJApplet
{
publicstaticfinalStringWEEK_SUN="SUN";
publicstaticfinalStringWEEK_MON="MON";
publicstaticfinalStringWEEK_TUE="TUE";
publicstaticfinalStringWEEK_WED="WED";
publicstaticfinalStringWEEK_THU="THU";
publicstaticfinalStringWEEK_FRI="FRI";
publicstaticfinalStringWEEK_SAT="SAT";
publicstaticfinalColorbackground=Color.yellow;
publicstaticfinalColorforeground=Color.black;
publicstaticfinalColorheaderBackground=Color.blue;
publicstaticfinalColorheaderForeground=Color.white;
publicstaticfinalColorselectedBackground=Color.blue;
publicstaticfinalColorselectedForeground=Color.white;
privateJPanelcPane;
privateJLabelyearsLabel;
privateJSpinneryearsSpinner;
privateJLabelmonthsLabel;
privateoBoxmonthsboBox;
privateJTabledaysTable;
privateAbstractTableModeldaysModel;
privateCalendarcalendar;
privateuttonbutton1;
publicMyCalendar(){
cPane=(JPanel)getContentPane();
}
publicvoidinit(){
cPane.setLayout(newBorderLayout());
calendar=Calendar.getInstance();
calendar=Calendar.getInstance();
uttonbutton1=newutton("单击此处查看时间");
button1.setBounds(10,10,10,10);
cPane.add(button1,BorderLayout.SOUTH);
button1.addActionListener(
newjava.awt.event.ActionListener()
{publicvoidactionPerformed(ActionEvente){
RollbyJFramemyrili=newRollbyJFrame();
JOptionPane.showMessageDialog(null,"您点击了"+e.getActionmand().toString()+"按钮");
}
}
);
yearsLabel=newJLabel("Year:
");
yearsSpinner=newJSpinner();
yearsSpinner.setEditor(newJSpinner.NumberEditor(yearsSpinner,"0000"));
yearsSpinner.setValue(newInteger(calendar.get(Calendar.YEAR)));
yearsSpinner.addChangeListener(newChangeListener(){
publicvoidstateChanged(ChangeEventchangeEvent){
intday=calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.YEAR,((Integer)yearsSpinner.getValue()).intValue());
intmaxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,day>maxDay?
maxDay:
day);
updateView();
}
});
JPanelyearMonthPanel=newJPanel();
cPane.add(yearMonthPanel,BorderLayout.NORTH);
yearMonthPanel.setLayout(newBorderLayout());
yearMonthPanel.add(newJPanel(),BorderLayout.CENTER);
JPanelyearPanel=newJPanel();
yearMonthPanel.add(yearPanel,BorderLayout.WEST);
yearPanel.setLayout(newBorderLayout());
yearPanel.add(yearsLabel,BorderLayout.WEST);
yearPanel.add(yearsSpinner,BorderLayout.CENTER);
monthsLabel=newJLabel("Month:
");
monthsboBox=newoBox();
for(inti=1;i<=12;i++){
monthsboBox.addItem(newInteger(i));
}
monthsboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsboBox.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventactionEvent){
intday=calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.MONTH,monthsboBox.getSelectedIndex());
intmaxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,day>maxDay?
maxDay:
day);
updateView();
}
});
JPanelmonthPanel=newJPanel();
yearMonthPanel.add(monthPanel,BorderLayout.EAST);
monthPanel.setLayout(newBorderLayout());
monthPanel.add(monthsLabel,BorderLayout.WEST);
monthPanel.add(monthsboBox,BorderLayout.CENTER);
daysModel=newAbstractTableModel(){
publicintgetRowCount(){
return9;
}
publicintgetColumnCount(){
return7;
}
publicObjectgetValueAt(introw,intcolumn){
if(row==0){
returngetHeader(column);
}
row--;
Calendarcalendar=(Calendar)MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH,1);
intdayCount=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
intmoreDayCount=calendar.get(Calendar.DAY_OF_WEEK)-1;
intindex=row*7+column;
intdayIndex=index-moreDayCount+1;
if(indexdayCount){
returnnull;
}else{
returnnewInteger(dayIndex);
}
}
};
daysTable=newCalendarTable(daysModel,calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
daysTable.setDefaultRenderer(daysTable.getColumnClass(0),newTableCellRenderer(){
publicponentgetTableCellRendererponent(JTabletable,Objectvalue,booleanisSelected,
booleanhasFocus,introw,intcolumn){
Stringtext=(value==null)?
"":
value.toString();
JLabelcell=newJLabel(text);
cell.setOpaque(true);
if(row==0){
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
}else{
if(isSelected){
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
}else{
cell.setForeground(foreground);
cell.setBackground(background);
}
}
returncell;
}
});
updateView();
cPane.add(daysTable,BorderLayout.CENTER);
}
publicstaticStringgetHeader(intindex){
switch(index){
case0:
returnWEEK_SUN;
case1:
returnWEEK_MON;
case2:
returnWEEK_TUE;
case3:
returnWEEK_WED;
case4:
returnWEEK_THU;
case5:
returnWEEK_FRI;
case6:
returnWEEK_SAT;
default:
returnnull;
}
}
publicvoidupdateView(){
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK)-1,
calendar.get(Calendar.DAY_OF_WEEK)-1);
}
publicstaticclassCalendarTableextendsJTable{
privateCalendarcalendar;
publicCalendarTable(TableModelmodel,Calendarcalendar){
super(model);
this.calendar=calendar;
}
publicvoidchangeSelection(introw,intcolumn,booleantoggle,booleanextend){
super.changeSelection(row,column,toggle,extend);
if(row==0){
return;
}
Objectobj=getValueAt(row,column);
if(obj!
=null){
calendar.set(Calendar.DAY_OF_MONTH,((Integer)obj).intValue());
}
}
}
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame("简易时间日历");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendarmyCalendar=newMyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setLocation(330,80);
frame.setSize(360,212);
frame.setVisible(true);
}
//滚动字
publicstaticclassRollbyJFrameextendsJFrame
implementsActionListener,FocusListener,javax.swing.event.ChangeListener
{
privateJTextFieldtext;
privateJSpinnerspinner;
privateTimertimer;
privateuttonbutton;
publicRollbyJFrame()
{
super("滚动时间");
this.setSize(360,100);
this.setBackground(java.awt.Color.lightGray);
this.setLocation(700,120);
Containerc=getContentPane();
uttonbutton=newutton("修改速度");
this.add(button,"East");
button.addActionListener(this);
Calendarnow=Calendar.getInstance();
inthour=now.get(Calendar.HOUR);
intminute=now.get(