JAVA课程设计写字留痕.docx
《JAVA课程设计写字留痕.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计写字留痕.docx(33页珍藏版)》请在冰豆网上搜索。
JAVA课程设计写字留痕
山东工商学院
《JAVA程序设计》课程设计报告
报告题目:
JAVA写字留痕
所在院系:
计算机科学与技术学院
所在班级:
网络工程
姓名:
***
一.设计思路
1.作为日记本,为了保密性,设计了登陆界面,只有知道密码才能登陆到主界面,在主界面文本区,右键单击菜单可以进行密码的修改。
如上图。
2.主界面用了两个Jpanel。
左边的图片,日历等为一个JPanel;右边的文本框,JButton等为另一个JPanel。
两个JPanel都是用的GridLayout,主界面的JFram用的是FlowLayout。
如上图。
3.当更换日期,关闭窗口,点击保存,都能把当前日期的日记保存到磁盘上。
4.当更换年月后,要先选择日期才能在文本区进行输入,否则会提示选择日期。
JLable的显示内容会变为“请选择日期”。
如下图。
二.实现代码
1.登陆界面的代码,只有输入正确的密码才能登陆日记程序。
publicclassPassValidateextendsJFrameimplementsActionListener{
JPasswordFieldpassfield;
JLabellabel,title1,title2,title3,title4;
JButtonyes,cancle;
PassValidate(){
super("登录");
setDefaultCloseOperation(3);
Toolkittool=this.getToolkit();
Imageim=tool.getImage("icon.jpg");
this.setIconImage(im);//更换窗口图标
yes=newJButton("确定");
yes.addActionListener(this);
cancle=newJButton("取消");
cancle.addActionListener(this);
Containercc=this.getContentPane();
cc.setLayout(newFlowLayout());
label=newJLabel("请输入密码:
");
title1=newJLabel("JAVA课程设计-----写字留痕");
title2=newJLabel("-----------------------------------------------");
title3=newJLabel("默认密码:
123登录后可更改密码");
title4=newJLabel("-----------------------------------------------------------");
passfield=newJPasswordField(15);
passfield.setEchoChar('*');
cc.add(title1);
cc.add(title2);
cc.add(title3);
cc.add(title4);
cc.add(label);
cc.add(passfield);
cc.add(yes);
cc.add(cancle);
setSize(300,200);
setVisible(true);
setLocation(600,250);
setResizable(false);
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
JButtonb=(JButton)e.getSource();
if(b==yes){
Stringjournalpass=null;
Filefil=newFile("data\\pass.info");
charc[]=newchar[(int)fil.length()];
try{
FileReaderin=newFileReader(fil);
in.read(c);
journalpass=newString(c);
in.close();
}catch(FileNotFoundExceptione1){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"读文件时,没有找到pass.info文件。
");//异常提示
e1.printStackTrace();
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"读文件时,pass.info文件异常。
");//异常提示
e1.printStackTrace();
}
Stringvalue=newString(passfield.getPassword());
if(value.endsWith(journalpass)){
newJournalTest();
this.dispose();
}
else{
passfield.setText("");
JOptionPane.showMessageDialog(null,"密码错误,请重新输入。
");
}
}
else
System.exit(0);
}
}
2.文本区JPanel的实现代码。
publicclassFileEditorextendsJPanelimplementsActionListener{
JPanelbuttonPanel;
staticJLabeltitle;
JTextAreaWithMenut;
JButtonsave,delete,inport,outport;
booleandateexistence=true;
intyear;
intmouth;
intday;
Hashtabledata
=newHashtable();
Filefile=newFile("data\\data.dat");
ObjectOutputStreamhashOut;
ObjectInputStreamhashIn;
Stringjournalpass;
//构造函数
FileEditor(){
this.setLayout(newBorderLayout(5,5));
journalpassInit();
titleInit();
buttonpanelInit();
t=newJTextAreaWithMenu(24,50);
add(title,BorderLayout.NORTH);
add(newJScrollPane(t),BorderLayout.CENTER);
add(buttonPanel,BorderLayout.SOUTH);
}
//密码初始
voidjournalpassInit(){
Filefil=newFile("data\\pass.info");
charc[]=newchar[(int)fil.length()];
try{
FileReaderin=newFileReader(fil);
in.read(c);
journalpass=newString(c);
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"读文件时,没有找到pass.info文件。
");//异常提示
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"读文件时,pass.info文件异常。
");//异常提示
e.printStackTrace();
}
;//测试save
}
//打开hashtable
voidhashOpen(){
try{
hashIn=newObjectInputStream(newFileInputStream(file));
data=(Hashtable)hashIn.readObject();
if(data.containsKey(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day)))
t.setText(data.get(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day)));
else
t.setText("");
hashIn.close();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"读文件时,没有找到data文件。
");//异常提示
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
//JOptionPane.showMessageDialog(null,"读文件时,没有找到data文件,读文件异常。
");//异常提示
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"读文件时,没有找到hashtable。
");
e.printStackTrace();
}
}
//标题初始化
voidtitleInit(){
title=newJLabel(DateTime.getYear()+"年"+DateTime.getMouth()+"月"+DateTime.getDay()+"日");
title.setFont(newFont("TimesRomn",Font.BOLD,18));
title.setHorizontalAlignment(SwingConstants.CENTER);
}
voidsetDateexistence(booleanb){
dateexistence=b;
}
voidsetCalendar(intyear,intmouth,intday){
this.year=year;
this.mouth=mouth;
this.day=day;
t.setDay(day);
}
//四个按钮初始化
voidbuttonpanelInit(){
buttonPanel=newJPanel(newFlowLayout());
save=newJButton("保存");
save.setFont(newFont("TimesRomn",Font.BOLD,12));
save.addActionListener(this);
delete=newJButton("删除");
delete.setFont(newFont("TimesRomn",Font.BOLD,12));
delete.addActionListener(this);
inport=newJButton("导入");
inport.setFont(newFont("TimesRomn",Font.BOLD,12));
inport.addActionListener(this);
outport=newJButton("导出");
outport.setFont(newFont("TimesRomn",Font.BOLD,12));
outport.addActionListener(this);
buttonPanel.add(save);
buttonPanel.add(delete);
buttonPanel.add(inport);
buttonPanel.add(outport);
}
voidsetTitle(intyear,intmouth,intday){
if(day!
=0){
remove(title);
title=newJLabel(year+"年"+mouth+"月"+day+"日");
title.setFont(newFont("TimesRomn",Font.BOLD,18));
title.setHorizontalAlignment(SwingConstants.CENTER);
add(title,BorderLayout.NORTH);
validate();
setDateexistence(true);//设置dateexistence
}
else{
remove(title);
title=newJLabel("请选择日期");
title.setFont(newFont("TimesRomn",Font.BOLD,18));
title.setHorizontalAlignment(SwingConstants.CENTER);
add(title,BorderLayout.NORTH);
validate();
setDateexistence(false);//设置dateexistence
}
}
//钮事件处理
voiddataSave(){
try{
hashOut=newObjectOutputStream(newFileOutputStream(file));
}catch(FileNotFoundExceptione2){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"save:
写文件时,没有找到data文件。
");//异常提示
e2.printStackTrace();
}catch(IOExceptione2){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"save:
写文件时,没有找到data文件,写入异常。
");//异常提示
e2.printStackTrace();
}
if(data.containsKey(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day))&&t.getText().trim().equals("")){
data.remove(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day));
}
if(!
t.getText().trim().equals("")){
try{
data.put(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day),t.getText());
hashOut.writeObject(data);
hashOut.close();
"indatasave");//测试save
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
JOptionPane.showMessageDialog(null,"save:
无法保存文件,Objectoutputstream异常。
");//异常提示
e1.printStackTrace();
}
}else
try{
hashOut.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
"datasave");//测试save
}
voidnewDate(){
if(data.get(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day))!
=null){
t.setText(data.get(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day)));
}
else
t.setText("");
}
//删除日记
voiddataDelete(){
//判断是当前日期是否有日记,给提示,然后删除,
if(data.containsKey(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day))){
if(JOptionPane.showConfirmDialog(null,"询问",
"是否删除"+year+"年"+mouth+"月"+day+"日"+"的日记记录?
",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
data.remove(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day));
t.setText("");
}
}
else
JOptionPane.showMessageDialog(null,"此日期无日志。
");//异常提示
}
//导入日记
voidinport(){
JFileChooserfc=newJFileChooser();
fc.setFileFilter(newTxtFilter());
intval=fc.showOpenDialog(this);
if(val==fc.APPROVE_OPTION){
Filename=fc.getSelectedFile();
charc[]=newchar[(int)name.length()];
FileReaderin;
try{
in=newFileReader(name);
in.read(c);
t.setText(newString(c));
dataSave();
in.close();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
//导出日记
voidoutport(){
Stringpass=JOptionPane.showInputDialog("请输入密码!
");
if(!
pass.equals(journalpass)){
JOptionPane.showMessageDialog(newJDialog(),"密码错误,不能导出!
");
return;
}
JFileChooserfc=newJFileChooser();
fc.setFileFilter(newTxtFilter());
fc.showSaveDialog(this);
Filename=fc.getSelectedFile();
if(name==null)return;
try{
ObjectInputStreamin=newObjectInputStream(newFileInputStream(file));
PrintWriterout=newPrintWriter(newFileOutputStream(name));
Hashtableo=(Hashtable)in.readObject();
TreeMapmap=newTreeMap(o);
Setmapping=map.entrySet();
for(Iteratori=mapping.iterator();i.hasNext();){
Map.Entryme=(Map.Entry)i.next();
out.println(me.getKey());
out.println((String)