资源描述
课程设计阐明书 NO.1
多功能电子时钟得设计
1、课程设计目
Java语言是面向对象开发办法是当今世界最流行开发办法,是当今流行网络编程语言。它具备面向对象、跨平台、分布应用等特点。它不但具备更贴近自然语义,并且有助于软件维护和继承。诸多程序开发人员得第一选取。为了将课堂上得知识掌握得更加透彻,深刻把握Java语言重要概念及其面向对象特性,锻炼学生纯熟应用面向对象思想和设计办法解决实际问题能力,开设了Java程序设计课程设计。让学生自己动手动脑,将课本上得知识运用到实践当中去,使知识能更好得融会贯通。
学习任何知识得目都是要将它运用到实践中去,因此咱们要运用已有知识,独立得将这个课程设计完毕,只有这样,咱们才干将知识变成本领,变成属于自己得东西,通过一种学习得学习,咱们已有可一定得Java基本,当前咱们就要运用这些基本,来完毕课程设计。
这次课程设计我重要研究了运用已学Java知识编辑一种多功能电子时钟。通过本次课程设计,来巩固所学Java语言基本知识,增进Java语言编辑基本功,掌握JDK、JCreator等开发工具运用,拓宽惯用类库应用。并通过自己动手,自主得学习,对Java这门课程有更深得理解与结识。
2、设计方案论证
2.1课程设计基本规定
多功能电子时钟是一种多功能得计时工具,它得功能与钟表不同,而是可以将年,月,日通过java程序得到实现,并具备更多功能,更简洁外观,更以便用法,更大实用价值。
通过多功能电子时钟,咱们可以实现诸多功能,随意看某年得年份,某年得月份,随意调节某年得某天,并将其显示出来,并且有判断闰年闰月得功能,十分以便万年历有诸多作用,顾名思义它是一种很以便得计时计年工具,随着科技得发展,咱们已经可以通过计算机制作出精美万年历,通过计算机制作得万年历不但外观美观,并且功能强大。除此之外,万年历还具备记事本功能,通过记事本功能可以备忘某些事情,备忘得事情可以随意添加在万年历得任意年月日里,如果记录得内容已经不再需要,还可以任意将记录得内容进行删除,在删除得时候还会提示。
2.2方案论证
沈 阳 大 学
课程设计阐明书 NO.2
2.2.1办法
表1 办法表
办法名
功能
备注(须输入参数)
calendar
初始化各种数据
int year,int month,int day
SwitchMonth
选取月份
int month
PrintMonth
显示月份
int year,int month,int day
PrintMonthBody
月份体
int startday,int dayinmonth
IsLeapYear
GetStartDay
GetTotalNumOfDays
GetNumOfDaysInMonth
WriteRecord
ReadRecord
DeleteFile
AboutActionListenerWay
actionPerformed(ActionEvent)
mousePressed
mouseClicked
mouseReleased
mouseEntered
mouseExited
SaveLog
Main
判断与否为闰年
得到开始天
得到总天数
得到当月天数
写日记
看日记
删除日记
监听途径
时间体现
鼠标点击
鼠标点击生
点击释放
鼠标进入
鼠标推出
保存日期
主函数
int year
int year,int month
int year,int month
int year,int month
唯一主函数
沈 阳 大 学
课程设计阐明书 NO.3
开始
初始化
读、写日期、时间和温度
分离日期\时间\温度显示值
显示子程序
农历自动更新子程序
日期、时间修改子程序
闰月子程
返回
定期闹铃子程序
2.2.2程序流程框图
图1 主程序流程图
图2计算阳历程序流程图
沈 阳 大 学
课程设计阐明书 NO.4
图3时间调节程序流程图
沈 阳 大 学
课程设计阐明书 NO.5
2.3程序阐明
2.3.1成员变量见表2变量表
表2变量表
成员变量描述
变量类型
名称
年、月、日
int
Year、month、day
下一年,下一月
Int
Yearafterquery,monthafterquey
开始时间
Int
startday
选取月,天
String
SwitchMonth,key,day
变化年
变化月
前一月,前一年
判断与否变化
左、又面板
年、月
左上角信息标签
显示月、年标签
空格标签
右上部时间标签
星期标签数组
月份下拉列表
时间拟定按钮
保存、删除按钮
前、下一月按钮
文本区
星期字符串数组
显示天文本区
输入年文本区
Int
Int
Int
boolean
JPanel
JLabel
JLabel
JLabel
JLabel
JLabel
JLabel[]
JComboBox
JButton
JButton
JButton
JTextArea
String
JTextField
JTextField
Changeyearmessage
changemonthmessage
priormonth,prioryear
ischange,ischange_priornext
LeftPane,RightPane
YearLabel,MonthLabel
Ask
ShowDate
Blank
NorthMonthDayYear
TopBarTitle[]
MonthCombobox
Query
SouthSave,SouthDelete
PriorMonth,NextMonth
CenterText
week[]
ShowDays[]
YearText
沈 阳 大 学
课程设计阐明书 NO.6
2.4程序源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.Calendar;
import java.util.*;
import java.io.*;
import java.io.IOException;
public class calendar extends JFrame
implements ActionListener,MouseListener
{
int year,month,day;
int yearafterquery,monthafterquery;
int startday;
String SwitchMonth;
String key;
int changeyearmessage;
int changemonthmessage;
int priormonth;
int prioryear;
boolean ischange=false;
boolean ischange_priornext=false;
private JPanel LeftPane,RightPane;
//Left sub
private JLabel YearLabel;
private JLabel MonthLabel;
private JComboBox MonthCombobox;
private JTextField ShowDays[]= new JTextField[42];
private JTextField YearText;
private JLabel Ask;
private JLabel ShowDate;
private JLabel Blank;
private JLabel TopBarTitle[]=new JLabel[7];
private JButton ToToday;
private JButton Query;
private
String week[]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
//right sub
private JLabel NorthMonthDayYear;
沈 阳 大 学
课程设计阐明书 NO.7
private JTextArea CenterText;
private JButton SouthSave,SouthDelete;
private JButton PriorMonth;
private JButton NextMonth;
public calendar(int year,int month,int day)
{
setTitle("My Calendar&Textbook");
//the layout about left of jpane
LeftPane = new JPanel();
JPanel LeftCenter = new JPanel();
JPanel LeftNorth = new JPanel();
//JPanel LeftSouth = new JPanel();
LeftPane.setLayout(new BorderLayout());
LeftPane.add(LeftNorth,BorderLayout.NORTH);
LeftPane.add(LeftCenter,BorderLayout.CENTER);
LeftPane.add(ToToday = new JButton("Go to today",new ImageIcon("../images/Handle.gif")),BorderLayout.SOUTH);
ToToday.setBackground(Color.cyan);
ToToday.addActionListener(this);
LeftPane.validate();
//the layout of LeftPane
//LeftPane_North
LeftNorth.setLayout(new GridLayout(3,1,0,-2));
LeftNorth.add(Ask = new JLabel(" Plese input the informations which you want query:"));
JPanel North = new JPanel(new FlowLayout(0,8,0));
LeftNorth.add(North);
North.add(YearLabel=new JLabel("Year:"));
North.add(YearText = new JTextField(4));
YearText.setBackground(Color.getHSBColor(30,20,50));
YearText.setForeground(Color.blue);
YearText.setFont(new Font("TimesRoman",Font.BOLD,17));
YearText.addActionListener(this);
YearText.setFocusable(true);
North.add(Blank=new JLabel(" "));
North.add(MonthLabel = new JLabel("Month:"));
North.add(MonthCombobox = new JComboBox());
//add month to monthcombobox
for(int i=1;i<=12;i++)
{
MonthCombobox.addItem(new Integer(i));
}
沈 阳 大 学
课程设计阐明书 NO.8
//Switch the month
MonthCombobox.setForeground(Color.blue);
MonthCombobox.setFont(new Font("TimesRoman",Font.BOLD,12));
North.add(Blank=new JLabel(" "));
North.add(Query=new JButton("Query"));
Query.setForeground(Color.blue);
Query.addActionListener(this);
JPanel North2=new JPanel(new FlowLayout());
LeftNorth.add(North2);
North2.add(PriorMonth=new JButton(new ImageIcon("../images/prior.gif")));
PriorMonth.addActionListener(this);
PriorMonth.setActionCommand("prior");
priormonth=month;
prioryear=year;
SwitchMonth(month);
North2.add(ShowDate = new JLabel(SwitchMonth+" "+","+" "+String.valueOf(year),SwingConstants.CENTER));
ShowDate.setForeground(Color.blue);
ShowDate.setFont(new Font("TimesRoman",Font.BOLD,14));
North2.add(NextMonth=new JButton(new ImageIcon("../images/next.gif")));
NextMonth.addActionListener(this);
NextMonth.setActionCommand("next");
//LeftPane_Center
LeftCenter.setLayout(new GridLayout(7,7));
//print title
for(int i=0;i<7;i++)
{
TopBarTitle[i]=new JLabel();
TopBarTitle[i].setText(week[i]);
TopBarTitle[i].setForeground(Color.darkGray);
TopBarTitle[i].setHorizontalAlignment(0);
TopBarTitle[i].setBackground(Color.MAGENTA );
TopBarTitle[i].setBorder(BorderFactory.createRaisedBevelBorder());
LeftCenter.add(TopBarTitle[i]);
}
//print screen and add listener
for(int i=0;i<42;i++)
{
ShowDays[i]=new JTextField();
ShowDays[i].addMouseListener(this);
ShowDays[i].setEditable(false);
沈 阳 大 学
课程设计阐明书 NO.9
LeftCenter.add(ShowDays[i]);
}
//print the body of month
PrintMonth(year,month,day);
//the layout about right of jpane
RightPane = new JPanel(new BorderLayout());
JPanel RightCenter = new JPanel();
JPanel RightNorth = new JPanel();
JPanel RightSouth = new JPanel(new FlowLayout());
RightPane.add(RightNorth,BorderLayout.NORTH);
RightPane.add(RightCenter,BorderLayout.CENTER);
RightPane.add(RightSouth,BorderLayout.SOUTH);
RightNorth.add(NorthMonthDayYear=new JLabel(">>"+year+","+SwitchMonth+","+day+"<<"));
key=year+"_"+SwitchMonth+"_"+day;
NorthMonthDayYear.setForeground(Color.blue);
NorthMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,17));
RightCenter.add(CenterText=new JTextArea("please write today's things."));
CenterText.setLineWrap(true);
CenterText.setSelectedTextColor(Color.blue);
//CenterText.addActionListener(this);
RightSouth.add(SouthSave=new JButton(" Save "));
SouthSave.setBackground(Color.cyan);
SouthSave.addActionListener(this);
SouthSave.setActionCommand("Save");
RightSouth.add(SouthDelete=new JButton(" Delete "));
SouthDelete.setBackground(Color.cyan);
SouthDelete.addActionListener(this);
SouthDelete.setActionCommand("Delete");
this.year = year;
this.month = month;
this.day = day;
///add container to put LeftPane and RightPane
Container con=getContentPane();
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,RightPane,LeftPane);//gai bian
con.add(split,BorderLayout.CENTER);
con.validate();
//add CenterPane to notepad
//CenterPane initialize
setFont(new Font("Times New Roman",Font.PLAIN,12));
沈 阳 大 学
课程设计阐明书 NO.10
JScrollPane scrollpane = new JScrollPane(CenterText);
scrollpane.setPreferredSize(new Dimension(220,250));
RightCenter.add(scrollpane);
//init randomaccessfile
}
//switch the month in english
public void SwitchMonth(int month)
{
switch(month)
{
case 1:
SwitchMonth="Jan";break;
case 2:
SwitchMonth="Feb";break;
case 3:
SwitchMonth="Mar";break;
case 4:
SwitchMonth="Apr";break;
case 5:
SwitchMonth="May";break;
case 6:
SwitchMonth="Jun";break;
case 7:
SwitchMonth="Jul";break;
case 8:
SwitchMonth="Aug";break;
case 9:
SwitchMonth="Sep";break;
case 10:
SwitchMonth="Qct";break;
case 11:
SwitchMonth="Nov";break;
case 12:
SwitchMonth="Dec";break;
}
}
//print the body of the month
public void PrintMonth(int year,int month,int day)
{
//Get start day of the week for the first date in the month
int startday = GetStartDay(year,month);
沈 阳 大 学
课程设计阐明书 NO.11
//Get number of days in the month
int dayinmonth = GetNumOfDaysInMonth(year,month);
//Print header
//PrintTitleAndScreen();
//Print body
PrintMonthBody(startday,dayinmonth,day);
}
//PrintMonth(int year,int month,int day)'s burden
public void PrintMonth(int year,int month)
{
//Get start day of the week for the first date in the month
int startday = GetStartDay(year,month);
//Get number of days in the month
int dayinmonth = GetNumOfDaysInMonth(year,month);
//Print header
//Print body
PrintMonthBody(startday,dayinmonth);
}
//PrintMonthBody(int startday,int dayinmonth,int day)'s burden
public void PrintMonthBody(int startday,int dayinmonth)
{
for(int i=startday,n=1;i<startday+dayinmonth;i++)
{
ShowDays[i].setText(""+n);
ShowDays[i].setHorizontalAlignment(0);//let center
if(n==day)
{
ShowDays[i].setForeground(Color.green);
ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,20));
ShowDays[i].setBackground(Color.DARK_GRAY);
}
else
{
ShowDays[i].setFont(new Font("TimesRoman",Font.BOLD,12));
ShowDays[i].setForeground(Color.white);
ShowDays[i].setBackground(Color.DARK_GRAY);
}
n++;
}
for(int i=0;i<startday;i++)
{
沈 阳 大 学
课程设计阐明书 NO.12
ShowDays[i].setText("");
ShowDays[i].setBackground(Color.DARK_GRAY);
}
for(int i=startday+dayinmonth;i<42;i++)
{
ShowDays[i].setText("");
ShowDays[i].setBackground(Color.DARK_GRAY);
}
}
//judge leapyear is or not
public boolean IsLeapYear(int year)
{
if((year%400==0)||(year%4==0&&year%100!=0))
return true;
else
return false;
}
//judge the start day of a month
public int GetStartDay(int year,int month)
{
//get total number of day since1/1/0000
int startday0001=-32768;
long totalnumofdays=GetTotalNumOfDays(year,month);
//return the start day
return (int)((totalnumofdays+startday0001)%7);
}
//judge the days of a year
public long GetTotalNumOfDays(int year,int month)
{
long total=0;
//get the total days from -32767 to year
for(int i=-32767;i<year;i++)
{
if(IsLeapYear(i))
total=total+366;
else
total=total+365;
}
//Add days from jan to the month prior to the calendar month
for(int i=1;i<month;i++)
total=total+GetNumOfDaysInMonth(year,i);
沈 阳 大 学
课程设计阐明书 NO.13
return total;
}
//judge the days of a month
public int GetNumOfDaysInMonth(int year,int month)
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||month==12)
return 31;
if(month==4 || month==6 || month==9 || month==11)
return 30;
if(month==2)
{
if(IsLeapYear(year))
return 29;
else
return 28;
}
return 0;
}
public void WriteRecord()
{
String content;
content=CenterText.getText();
int n=content.length();
char[] contentarr=new char[n];
try
{
int i=0;
for(i=0;i<n;i++)
{
contentarr[i]=content.charAt(i);
}
File Diary = new File("Diary");
Diary.mkdir();
File myfile=new File("Diary\\"+key+".txt");
FileWriter Record=new FileWriter(myfile);
for(i=0;i<contentarr.length;i++)
{
Record.write(contentarr[i]);
}
Record.close();
JOptionPane.showMessageDialog(this,"Save success!");
沈 阳 大 学
课程设计阐明书 NO.14
}
catch(IOException ex)
{
}
}
public void ReadRecord()
{
try
{
String content="";
File myfile=new File("Diary\\"+key+".txt");
FileReader Record=new FileReader(myfile);
if(myfile.exists())
{
long b=myfile.length();
//char[] contentarr=new char[b];
int n=JOptionPane.showConfirmDialog(this,"Today has logs,are you read?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION)
{
while((b=Record.read())!=-1)
{
content=content+(char)b;
}
CenterText.setText(content);
}
}
Record.close();
}
catch(IOException ex)
{
CenterText.setText("Today has not logs.");
}
}
public void DeleteFile()
{
String filepath="Diary\\"+key+".txt";
File myfile=new File(filepath);
int n=JOptionPane.showConfirmDialog(this,"Are you sure delete the file?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION)
沈 阳 大 学
课程设计阐明书 NO.15
{
if(myfile.exists())
{
Runtime rt = Runtime.getRuntime();
try
{
rt.exec("cmd /c del "+filepath);
}
catch (IOException e)
{
e.printStackTrace();
}
JOptionPane.showMessageDialog(this,"Delete successed!");
CenterText.setText("Today has not logs.");
}
else
{
JOptionPane.showMessageDialog(this,"The file doesn't exist,delete failured!");
}
}
}
public void AboutActionListenerWay()
{
try
{
prioryear=Integer.parseInt(YearText.getText());
展开阅读全文