java课程设计万年历程序设计报告.docx

上传人:b****3 文档编号:5453975 上传时间:2022-12-16 格式:DOCX 页数:28 大小:163.12KB
下载 相关 举报
java课程设计万年历程序设计报告.docx_第1页
第1页 / 共28页
java课程设计万年历程序设计报告.docx_第2页
第2页 / 共28页
java课程设计万年历程序设计报告.docx_第3页
第3页 / 共28页
java课程设计万年历程序设计报告.docx_第4页
第4页 / 共28页
java课程设计万年历程序设计报告.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

java课程设计万年历程序设计报告.docx

《java课程设计万年历程序设计报告.docx》由会员分享,可在线阅读,更多相关《java课程设计万年历程序设计报告.docx(28页珍藏版)》请在冰豆网上搜索。

java课程设计万年历程序设计报告.docx

java课程设计万年历程序设计报告

山东科技大学

信息学院

 

JAVA程序设计

 

学院:

信息科学与工程学院

班级:

计算机11-3

学号:

************

姓名:

**

目录1

前言1

1需求分析2

2.概要设计3

3.各模块的功能及程序说明5

4.测试分析13

5.源程序清单13

6.课程设计体会22

 

前言

Java的前身是Oak,它一开始只是被应用于消费性电子产品中。

后来它的开发者们发现它还可以被用于更大范围的Internet上。

1995年,Java语言的名字从Oak编程了Java。

1997年J2SE1.1发布。

1998年J2SE1.2发布,标志Java2的诞生。

十多年来,Java编程语言及平台成功地运用在网络计算及移动等各个领域。

Java的体系结构由Java语言、Javaclass、JavaAPI、Java虚拟机组成。

它具有简单、面向对象、健壮、安全、结构中立、可移植和高效能等众多优点。

Java支持多线程编程,Java运行时系统在多线程同步方面具有成熟的解决方案。

Java的平台标准有JavaME,JavaSE和JavaEE。

Java发展到今天,它的卓越成就及在业界的地位毋庸置疑。

目前在众多的支持Java的开发工具中主要的7有JavaDevelopmentKit,NetBeans,Jcreator,JBuilder,JDeveloper和Eclipse等。

其中JavaDevelopmentKit简称JDK是大多开发工具的基础。

以上的每种开发工具都有优缺点,对于开发者来说,重要的是要根据自己的开发规模、开发内容和软硬件环境等因素来选择一种合适的开发工具。

 

1需求分析

1.1需求分析

本程序的要求为:

1.使用图形用户界面;2.本程序能够实现日期与星期的查询。

1.2功能设计

本程序要构建的万年历程序,其功能有以下几个方面:

(1)通过网页形式运行,实现图形界面。

(2)能以月历形式显示日期与星期。

(3)支持用户自己输入年份,并提供月份的下拉形式菜单来选择月份。

(4)通过点击“更新”来刷新日历。

 

2.概要设计

2.1程序设计思路

1.总天数的算法:

首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否则,为365。

然后判断输入的年是否为定义年,若是,令总天数S=1,否则,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,若该月是闰年中的月份并且该月还大于二月,再使总天数加1,否则,不加,既算出从定义年一月一日到输出年的该月一日的总天数。

    2.输出月份第一天为星期几的算法:

使总天数除以7取余加2得几既为星期几,若是7,则为星期日。

3.算出输出月份第一天为星期几的算法:

算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历。

4.如果年份小于1582年则程序不予判断。

2.2程序运行界面

图2-1程序运行界面

2.3流程图

3.各模块的功能及程序说明

3.1.初始化组件

importjava.awt.*;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.io.OutputStreamWriter;

//importjava.sql.Date;

importjava.text.DateFormat;

importjava.text.SimpleDateFormat;

importjava.util.Calendar;

importjava.util.Date;

importjavax.swing.JButton;

importjavax.swing.JDialog;

importjavax.swing.JFrame;

importjavax.swing.JLabel;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

importjavax.swing.JScrollPane;

importjavax.swing.JTable;

importjavax.swing.JTextArea;

importjavax.swing.JTextField;

importjavax.swing.table.DefaultTableModel;

3.2.初始化数据

privateCalendarcld=Calendar.getInstance();

privateString[]astr={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};

privateDefaultTableModeldtm=newDefaultTableModel(null,astr);

privateJTabletable=newJTable(dtm);//装日期的表格

privateJScrollPanesp=newJScrollPane(table);

privateJButtonbLastYear=newJButton("上一年");

privateJButtonbNextYear=newJButton("下一年");privateJButbNextMonth=newJButton("下月");

privateJTextFieldjtfYear=newJTextField(5);//jtfYear年份显示和输入文本框

privateJTextFieldjtfMonth=newJTextField

(2);//jtfMonth月份显示文本框

privateJPanelp1=newJPanel();//装入控制日期按钮的模块

privateJPanelp2=newJPanel();

privateJPanelp3=newJPanel(newBorderLayout());

privateJPanelp4=newJPanel(newGridLayout(2,1));

privateJPanelp5=newJPanel(newBorderLayout());

privateJButtonbAdd=newJButton("保存日志");

privateJButtonbDel=newJButton("删除日志");

privateJTextAreajta=newJTextArea();//jta--JTextArea

privateJScrollPanejsp=newJScrollPane(jta);

privateJLabell=newJLabel("年份文本框中可直接键入要查找的年份,以提高查询效率");

privateJLabellt=newJLabel();

privateJLabelld=newJLabel();

privateintlastTime;

3.3.绘制程序界面

publicwannianli()

{

super("万年历");//框架命名

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭函数

this.getContentPane().setLayout(newBorderLayout(10,0));

jta.setLineWrap(true);

table.setGridColor(Color.GRAY);//星期之间的网格线是灰色的

table.setColumnSelectionAllowed(true);

table.setSelectionBackground(Color.BLACK);//当选定某一天时这一天背景黑色

table.setSelectionForeground(Color.GREEN);//选定的日期字体是绿色的

table.setBackground(newColor(184,207,229));//日期显示表格颜色浅蓝色

table.setFont(newFont("黑体",Font.BOLD,24));//日期数字字体格式

table.setRowHeight(30);//表格的高度

table.addMouseListener(this);//鼠标监听器

jtfYear.addActionListener(this);//可输入年份的文本框

//为各个按钮添加监听函数

bAdd.addActionListener(this);

bDel.addActionListener(this);

bLastYear.addActionListener(this);

bNextYear.addActionListener(this);

bLastMonth.addActionListener(this);

bNextMonth.addActionListener(this);

//将按钮添加到Jpanel上

p1.add(bLastYear);

p1.add(jtfYear);//年份输入文本框

p1.add(bNextYear);

p1.add(bLastMonth);

p1.add(jtfMonth);

p1.add(bNextMonth);

p2.add(bAdd);

p2.add(bDel);

p3.add(jsp,BorderLayout.CENTER);

p3.add(p2,BorderLayout.SOUTH);

p3.add(ld,BorderLayout.NORTH);

p4.add(l);

p4.add(lt);

p5.add(p4,BorderLayout.SOUTH);

p5.add(sp,BorderLayout.CENTER);

p5.add(p1,BorderLayout.NORTH);

this.getContentPane().add(p5,BorderLayout.CENTER);

this.getContentPane().add(p3,BorderLayout.EAST);

String[]strDate=DateFormat.getDateInstance().format(newDate()).split("-");//获得日期

cld.set(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1])-1,0);

showCalendar(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1]),cld);

jtfMonth.setEditable(false);//设置月份的文本框为不可编辑

jtfYear.setText(strDate[0]);

jtfMonth.setText(strDate[1]);

this.showTextArea(strDate[2]);

ld.setFont(newFont("新宋体",Font.BOLD,24));

newTimer(lt).start();

this.setBounds(200,200,600,320);

this.setResizable(false);

this.setVisible(true);

}

3.4触发器

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==jtfYear||e.getSource()==bLastYear||e.getSource()==bNextYear||

e.getSource()==bLastMonth||e.getSource()==bNextMonth)

{

intm,y;

try//控制输入的年份正确,异常控制

{

if(jtfYear.getText().length()!

=4)

{

thrownewNumberFormatException();

}

y=Integer.parseInt(jtfYear.getText());

m=Integer.parseInt(jtfMonth.getText());

}

catch(NumberFormatExceptionex)

{

JOptionPane.showMessageDialog(this,"请输入4位0-9的数字!

","年份有误",JOptionPane.ERROR_MESSAGE);

return;

}

ld.setText("没有选择日期");

for(inti=0;i

if(e.getSource()==bLastYear){jtfYear.setText(String.valueOf(--y));}

if(e.getSource()==bNextYear){jtfYear.setText(String.valueOf(++y));}

if(e.getSource()==bLastMonth)

{

if(m==1)

{

jtfYear.setText(String.valueOf(--y));

m=12;

jtfMonth.setText(String.valueOf(m));

}

else

{

jtfMonth.setText(String.valueOf(--m));

}

}

if(e.getSource()==bNextMonth)

{

if(m==12)

{

jtfYear.setText(String.valueOf(++y));

m=1;

jtfMonth.setText(String.valueOf(m));

}

else

{

jtfMonth.setText(String.valueOf(++m));

}

}

cld.set(y,m-1,0);

showCalendar(y,m,cld);

}

if(e.getSource()==bAdd)

{

intr=table.getSelectedRow();

intc=table.getSelectedColumn();

if(!

ld.getText().equals("没有选择日期"))

{

try

{

Filefile=newFile(ld.getText()+".txt");

BufferedWriterbw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream(file)));

bw.write(jta.getText());

bw.close();

}

catch(FileNotFoundExceptionex)

{

ex.printStackTrace();

}

catch(IOExceptionex)

{

ex.printStackTrace();

}

}

}

if(e.getSource()==bDel)

{

intr=table.getSelectedRow();

intc=table.getSelectedColumn();

Filefiledel=newFile(ld.getText()+".txt");

if(filedel.exists())

{

if(filedel.delete())

{

jta.setText("日志删除成功");

}

else

{

jta.setText("日志删除失败");

}

}

else

{

jta.setText("没有找到日志文件");

}

}

}

publicvoidmouseClicked(MouseEvente)

{

jta.setText(null);

intr=table.getSelectedRow();

intc=table.getSelectedColumn();

if(table.getValueAt(r,c)==null)

{

ld.setText("没有选择日期");

}

else

{

this.showTextArea(table.getValueAt(r,c));

}

}

publicvoidmousePressed(MouseEvente)

{

}

publicvoidmouseReleased(MouseEvente)

{

}

publicvoidmouseEntered(MouseEvente)

{

}

publicvoidmouseExited(MouseEvente)

{

}

privatevoidshowTextArea(Objectselected)

{//将所选日期显示出来,能不能弄成农历显示

ld.setText(jtfYear.getText()+"年"+jtfMonth.getText()+"月"+selected+"日");

Filefilein=newFile(ld.getText()+".txt");

if(filein.exists())

{

try

{

BufferedReaderbr=newBufferedReader(newInputStreamReader(newFileInputStream(filein)));

StringstrRead=br.readLine();

jta.setText(null);

while(strRead!

=null)

{

jta.append(strRead);

strRead=br.readLine();

}

br.close();

}

catch(FileNotFoundExceptionex)

{

ex.printStackTrace();

}

catch(IOExceptionex)

{

ex.printStackTrace();

}

}

}

3.5时间获取

classTimerextendsThread//显示系统时间

{

privateJLabellt;

privateSimpleDateFormatfy=newSimpleDateFormat("yyyy.MM.ddG'at'HH:

mm:

ssz");

privateSimpleDateFormatfn=newSimpleDateFormat("yyyy.MM.ddG'at'HHmmssz");

privatebooleanb=true;

publicTimer(JLabellt)

{

this.lt=lt;

}

publicvoidrun()

{

while(true)

{

try

{

if(b)

{

lt.setText(fy.format(newDate()));

}

else

{

lt.setText(fn.format(newDate()));

}

b=!

b;

this.sleep(500);

}

catch(InterruptedExceptionex)

{

ex.printStackTrace();

}

}

}

}

4.测试分析

图4-1图4-2

图4-3图4-4

对程序进行正常使用,测试各项功能的实现过程是否存在bug,经测试,软件正常使用,无任何Bug。

5.源程序清单

importjava.awt.*;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.MouseEvent;

importjava.awt.event.MouseListener;

importjava.io.BufferedReader;

importjava.io.BufferedWriter;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStreamReader;

importjava.io.OutputStreamWriter;

//importjava.sql.Date;

importjava.text.DateFormat;

importjava.text.SimpleDateFormat;

importjava.util.Calendar;

importjava.util.Date;

importjavax.swing.JButton;

import

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1