资源描述
记事本java程序设计报告
25
2020年4月19日
文档仅供参考,不当之处,请联系改正。
~ 第1学期
《Java程序设计》课程
报 告
项目: 日历记事本
专业: 计算机科学与技术
学号: 10570235
姓名: 常兆华
班级: 计算机2班
分数:
角色
姓名
学号
任务分配
小组评定
组长
王思琪
10570226
强制练习工具
组员
常兆华
10570235
日历记事本
隋欣
10570219
日历记事本
杨海钰
10570232
小游戏
关乐
10570231
小游戏
王冕卿
10570223
Atm取款机系统
项 目 说 明
目 录
第一部分、项目整体概述……………………………………………3页
第二部分、我的任务…………………………………………………5页
第三部分、代码和详细注释…………………………………………6页
第四部分、心得体会………………………………………………14页
第一部分、项目整体概述
日历记事本----------带有日程提醒功能的日历。
2.显示信息:
用户能够看到这个月的信息,包括年份、日期等。
点击翻页按钮能够查询前一个月的日期,也能够向后翻页查询下一个月的日期。同样,能够根据年份查询不同年份的日期。
日期的显示有一些优化,用户不但能够查询到本月份的信息,还能够根据上个月与下个月的日期填充来方便查询日期和星期。
3.定时提醒:
用户能够针对某一天来添加、删除和编辑这一天的日程提醒信息
当系统时间和提醒时间相吻合时,给出具有提示信息的对话框。
4.查询信息:用户能够查询到某个月的所有的提示信息。
日历记事本共有4个java源文件。
CalendarPad.java
该java文件生成的类负责创立本日历记事本程序主窗口。该类含有main方法,程序从该类开始执行。
Year.java
该文件负责创立管理年份的对象。
Month.java
该文件负责创立管理月份的类对象。
NotePad.java
该文件负责创立记事本。
截图:
初始界面
可输入年份查看相应的日历与记事本
第二部分、我的任务
具体任务:
资料查找,汇总及需求分析,负责日历的编写,和板块的布局输出等。
编写大致思路:
我主要负责这个项目中日历的编写和输出显示的一部分。
经过调查自己电脑中的日历记事本和大家纸质的日历记事本,我知道若想完整地显示一个月的信息至少需要一个首先需要一个7*7的网格,要用到 GridLayout网格设置语言。其中每一列的顶层能够称它为title“标题”,也就是显示“星期几”,表头显示这个月所在的年份与月份。随后对日期进行编号,判断闰平年、大小月等必要的程序。为了能使用户查找到前一年、下一年,上一月、下一月的内容,还需要设置按钮,让成员变量实现其更改。
总体规划:
主类CanlendarPad
1.成员变量
成员变量描述
变量类型
名称
年、月、日
int
year,month,day
保存日志的散列表
Hanshtable
hanshtable
存放散列表的文件
File
file
显示日期
JtextFile[]
showDay
日历对象
Calendar
日历
记事本对象
NotePad
notepad
月
Month
负责改变月
年
Year
负责改变年
2.方法
名称
功能
备注
CalendarPad
创立窗口主程序
构造方法
设置日历牌
设置日立的年份、月份
排列号码
排列月份中的号码
mousePressed
处理MouseEvent事件
接口方法
main
程序开始运行
第三部分、源代码及详细注释:
CalendarPad
import java.util.Calendar; //写的Calendar类里面导入这个包下的所有类库
public class CalendarPad extends JFrame implements MouseListener
{ //定义CalendarPad类继承JFrame 父类的属性 MouseListener鼠标监听
int year,month,day;//定义年月日
Hashtable hashtable; //用来保存日志的散列哈希表
File file; //存在哈希表中的文件
JTextField showDay[]; //每天是7X7的一个小格子单元
JLabel title[]; //用来创立日历牌中的表格属性“星期几”
Calendar 日历;//定义“日历”方法是calendar
int 星期几; //整形定义“星期几”方法
NotePad notepad=null; //日记本对象,初值为空
Month 负责改变月;//定义“负责改变月”方法
Year 负责改变年;//定义“负责改变年”方法
String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JPanel leftPanel,rightPanel; //界面设计,左边是日历,右面为记事本
public CalendarPad(int year,int month,int day)//在CalendarPad类中定义年月日
{
getContentPane().setBackground(new Color(255, 228, 225));//初始化一个容器,设置颜色值为(255,,228,225)
setBackground(new Color(255, 250, 250));//颜色设置
setFont(new Font("微软雅黑", Font.BOLD, 12));//字体设置
setForeground(new Color(70, 130, 180));
setTitle("");
leftPanel=new JPanel();//传入一个布局对象作为参数来创立左边的面板
JPanel leftCenter=new JPanel();
JPanel leftNorth=new JPanel();
leftCenter.setLayout(new GridLayout(7,7)); //设置窗口,为7*7的网格,参数说明(行数,列数)
rightPanel=new JPanel();//设置右侧面板
this.year=year;
this.month=month;
this.day=day;
负责改变年=new Year(this);
负责改变年.setFont(new Font("微软雅黑", Font.BOLD, 12));//设置字体
负责改变年.setForeground(new Color(65, 105, 225));//设置前景色
负责改变年.setYear(year);//调用方法
负责改变月=new Month(this);
负责改变月.setFont(new Font("微软雅黑", Font.BOLD, 12));//设置字体
负责改变月.setForeground(new Color(65, 105, 225));//设置前景色
负责改变月.setMonth(month);//调用方法
title=new JLabel[7];//“星期几”标题,有7个
showDay=new JTextField[42]; //显示日期的文件有42个(对应42个网格)
for(int j=0;j<7;j++) //标题的写入,周日到周一
{
title[j]=new JLabel();
title[j].setText(星期[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());//创立边框
leftCenter.add(title[j]);
}
title[0].setForeground(Color.red);//设置周日为红色
title[6].setForeground(Color.blue);//设置周六为蓝色
for(int i=0;i<42;i++) //日期写入
{
showDay[i]=new JTextField();
showDay[i].addMouseListener(this);
showDay[i].setEditable(false);
leftCenter.add(showDay[i]);
}
日历=Calendar.getInstance();//年份月份的查询
Box box=Box.createHorizontalBox();
box.add(负责改变年);
box.add(负责改变月);
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);//上方的边界
leftPanel.add(leftCenter,BorderLayout.CENTER);//中间
Label label = new Label("请在年份输入框输入所查年份,按回车确定。负数为公元前");
label.setBackground(UIManager.getColor("ScrollBar.foreground"));//具体设置
label.setFont(new Font("黑体", Font.BOLD, 13));
label.setForeground(new Color(204, 0, 0));
leftPanel.add(label,
BorderLayout.SOUTH) ;//下方的边界
leftPanel.validate();
Container con=getContentPane();
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel,rightPanel);
con.add(split,BorderLayout.CENTER);
con.validate();
hashtable=new Hashtable();//初始化哈希表
file=new File("日历记事本.txt");//初始化文件
if(!file.exists())
{
try{
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(hashtable);
objectOut.close();
out.close();
}
catch(IOException e)//捕获输入输出异常
{
}
}
notepad=new NotePad(this); //调用notepad的数据
rightPanel.add(notepad);
设置日历牌(year,month);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,612,320);
validate();
}
public void 设置日历牌(int year,int month)
{
日历.set(year,month-1,1); // Calendar是JAVA默认的类,set(年,月,日)格式,月份是从0开始计为1月,以此类推。故设置月份参数为month-1
星期几=日历.get(Calendar.DAY_OF_WEEK)-1;//规定格式,得到此日为星期几
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//判断大小月
{
排列号码(星期几,31);
}
else if(month==4||month==6||month==9||month==11)
{
排列号码(星期几,30);
}
else if(month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0)) //判断闰年,平年
{
排列号码(星期几,29);
}
else
{
排列号码(星期几,28);
}
}
}
public void 排列号码(int 星期几,int 月天数)
{
for(int i=星期几,n=1;i<星期几+月天数;i++)
{
showDay[i].setText(""+n);//设置天数,累加
if(n==day)
{
showDay[i].setForeground(Color.green);
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
}
else
{
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
showDay[i].setForeground(Color.black);
}
if(i%7==6)
{
showDay[i].setForeground(Color.blue);
}
if(i%7==0)
{
showDay[i].setForeground(Color.red);
}
n++;
}
//程序优化:显示上个月和下个月部分日期
int lastmonth = month - 1;//取得上个月月份
if(lastmonth == 0)
lastmonth = 12;
int date = 0;
if(lastmonth==1||lastmonth==3||lastmonth==5||lastmonth==7||lastmonth==8||lastmonth==10||lastmonth==12)
date = 31;
else if(lastmonth==4||lastmonth==6||lastmonth==9||lastmonth==11)
date = 30;
else if(lastmonth==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))
date = 29;
else
date = 28;
}
for(int i=星期几 - 1;i >= 0;i--)//将上个月部分日期放入单元格
{
showDay[i].setForeground(Color.gray);
showDay[i].setText(date + "");
date --;
}
int flag = 1;//将下个月部分日期放入单元格
for(int i=星期几+月天数;i<42;i++)
{
showDay[i].setForeground(Color.gray);
showDay[i].setText(String.valueOf(flag));
flag ++;
}
}
public int getYear()
{
return year;
}
public void setYear(int y)
{
year=y;
notepad.setYear(year);
}
public int getMonth()
{
return month;
}
public void setMonth(int m)
{
month=m;
notepad.setMonth(month);
}
public int getDay()
{
return day;
}
public void setDay(int d)
{
day=d;
notepad.setDay(day);
}
public Hashtable getHashtable()
{
return hashtable;
}
public File getFile()
{
return file;
}
public void mousePressed(MouseEvent e)
{
JTextField source=(JTextField)e.getSource();
try{
day=Integer.parseInt(source.getText());
notepad.setDay(day);
notepad.设置信息条(year,month,day);
notepad.设置文本区(null);
notepad.获取日志内容(year,month,day);
}
catch(Exception ee)
{
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public static void main(String args[])
{
Calendar calendar=Calendar.getInstance();
int y=calendar.get(Calendar.YEAR);
int m=calendar.get(Calendar.MONTH)+1;
int d=calendar.get(Calendar.DAY_OF_MONTH);
new CalendarPad(y,m,d);
}
}
YEAR:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Year extends Box implements ActionListener
{
int year;
int month;
JTextField showYear=null;
JButton 明年,去年;
CalendarPad 日历;
public Year(CalendarPad 日历)
{
super(BoxLayout.X_AXIS);
showYear=new JTextField(4);
showYear.setBackground(new Color(230, 230, 250));
showYear.setForeground(new Color(255, 20, 147));
showYear.setFont(new Font("TimesRomn",Font.BOLD,14));
this.日历=日历;
year=日历.getYear();
明年=new JButton("下年");
明年.setForeground(new Color(0, 139, 139));
明年.setFont(new Font("微软雅黑", Font.BOLD, 13));
去年=new JButton("上年");
去年.setFont(new Font("微软雅黑", Font.BOLD, 13));
去年.setForeground(new Color(0, 139, 139));
add(去年);
add(showYear);
add(明年);
showYear.addActionListener(this);
去年.addActionListener(this);
明年.addActionListener(this);
}
public void setYear(int year)
{
this.year=year;
showYear.setText(""+year);
}
public int getYear()
{
return year;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==去年)
{
year=year-1;
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
else if(e.getSource()==明年)
{
year=year+1;
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
else if(e.getSource()==showYear)
{
try
{
year=Integer.parseInt(showYear.getText());
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
catch(NumberFormatException ee)
{
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
}
}
}
第四部分、心得体会
经过这次对日历记事本的程序编写,我对java有了更深一步的探求和理解。首先是对日历记事本的外观理解、分析,以及查找电脑上、手机上运用的日历记事本的成品,都让我们有了灵感和坚定了决心。我和隋欣一同完成它经历了许多困难。因为知识还是有些不足,我们对java许多语句进行了资料的查找和搜集,经过多方面的努力和一遍又一遍地报错排错,最后对于界面的设计,我们两个也是很久才达成共识。我们当然也学会了如何坚持一件事情。使我们非常默契的配合和精心的准备与编写,最终完成了这样一个我们眼中十分完美的程序。这是一次很难得的体会和经历,这个程序将永远保存在我的电脑里当做最宝贵的一次课程设计。
展开阅读全文