1、JAVA课程设计写字留痕山东工商学院JAVA程序设计课程设计报告报告题目: JAVA写字留痕 所在院系: 计算机科学与技术学院 所在班级: 网络工程 姓 名: * 一设计思路 1. 作为日记本,为了保密性,设计了登陆界面,只有知道密码才能登陆到主界面,在主界面文本区,右键单击菜单可以进行密码的修改。如上图。2. 主界面用了两个Jpanel。左边的图片,日历等为一个JPanel;右边的文本框,JButton等为另一个JPanel。两个JPanel都是用的GridLayout,主界面的JFram用的是FlowLayout。如上图。3. 当更换日期,关闭窗口,点击保存,都能把当前日期的日记保存到磁盘
2、上。4. 当更换年月后,要先选择日期才能在文本区进行输入,否则会提示选择日期。JLable的显示内容会变为“请选择日期”。如下图。二实现代码1. 登陆界面的代码,只有输入正确的密码才能登陆日记程序。public class PassValidate extends JFrame implements ActionListener JPasswordField passfield; JLabel label,title1,title2,title3,title4; JButton yes,cancle; PassValidate() super(登录); setDefaultCloseOpera
3、tion(3); Toolkit tool=this.getToolkit(); Image im=tool.getImage(icon.jpg); this.setIconImage(im); /更换窗口图标 yes=new JButton(确定); yes.addActionListener(this); cancle=new JButton(取消); cancle.addActionListener(this); Container cc=this.getContentPane(); cc.setLayout(new FlowLayout(); label=new JLabel(请输入密
4、码:); title1=new JLabel( JAVA课程设计-写字留痕 ); title2=new JLabel(-); title3=new JLabel( 默认密码:123 登录后可更改密码 ); title4=new JLabel(-); passfield=new JPasswordField(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.a
5、dd(cancle); setSize(300,200); setVisible(true); setLocation(600, 250); setResizable(false); Override public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub JButton b=(JButton) e.getSource(); if(b=yes) String journalpass = null; File fil=new File(datapass.info); char c=new char(
6、int) fil.length(); try FileReader in =new FileReader(fil); in.read(c); journalpass=new String(c); in.close(); catch (FileNotFoundException e1) / TODO Auto-generated catch block JOptionPane.showMessageDialog(null, 读文件时,没有找到pass.info文件。); /异常提示 e1.printStackTrace(); catch (IOException e1) / TODO Auto-
7、generated catch block JOptionPane.showMessageDialog(null, 读文件时,pass.info文件异常。); /异常提示 e1.printStackTrace(); String value=new String(passfield.getPassword(); if(value.endsWith(journalpass) new JournalTest(); this.dispose(); else passfield.setText(); JOptionPane.showMessageDialog(null, 密码错误,请重新输入。); e
8、lse System.exit(0); 2. 文本区JPanel的实现代码。public class FileEditor extends JPanel implements ActionListener JPanel buttonPanel; static JLabel title; JTextAreaWithMenu t; JButton save,delete,inport,outport; boolean dateexistence=true; int year; int mouth; int day; Hashtable data = new Hashtable(); File fi
9、le=new File(datadata.dat); ObjectOutputStream hashOut; ObjectInputStream hashIn; String journalpass; /构造函数 FileEditor() this.setLayout(new BorderLayout(5,5); journalpassInit(); titleInit(); buttonpanelInit(); t=new JTextAreaWithMenu(24,50); add(title,BorderLayout.NORTH); add(new JScrollPane(t),Borde
10、rLayout.CENTER); add(buttonPanel,BorderLayout.SOUTH); /密码初始 void journalpassInit() File fil=new File(datapass.info); char c=new char(int) fil.length(); try FileReader in =new FileReader(fil); in.read(c); journalpass=new String(c); catch (FileNotFoundException e) / TODO Auto-generated catch block JOp
11、tionPane.showMessageDialog(null, 读文件时,没有找到pass.info文件。); /异常提示 e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block JOptionPane.showMessageDialog(null, 读文件时,pass.info文件异常。); /异常提示 e.printStackTrace(); ; /测试save /打开hashtable void hashOpen() try hashIn=new ObjectInputStream(new
12、 FileInputStream(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 (FileNotFoundExcepti
13、on e) / TODO Auto-generated catch block JOptionPane.showMessageDialog(null, 读文件时,没有找到data文件。); /异常提示 e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block /JOptionPane.showMessageDialog(null, 读文件时,没有找到data文件,读文件异常。); /异常提示 e.printStackTrace(); catch (ClassNotFoundException e)
14、/ TODO Auto-generated catch block JOptionPane.showMessageDialog(null, 读文件时,没有找到hashtable。); e.printStackTrace(); /标题初始化 void titleInit() title=new JLabel(DateTime.getYear()+年+DateTime.getMouth()+月+DateTime.getDay()+日); title.setFont(new Font(TimesRomn,Font.BOLD,18); title.setHorizontalAlignment(Swin
15、gConstants.CENTER); void setDateexistence(boolean b) dateexistence=b; void setCalendar(int year,int mouth,int day) this.year=year; this.mouth=mouth; this.day=day; t.setDay(day); /四个按钮初始化 void buttonpanelInit() buttonPanel=new JPanel(new FlowLayout(); save=new JButton(保存); save.setFont(new Font(Times
16、Romn,Font.BOLD,12); save.addActionListener(this); delete=new JButton(删除); delete.setFont(new Font(TimesRomn,Font.BOLD,12); delete.addActionListener(this); inport=new JButton(导入); inport.setFont(new Font(TimesRomn,Font.BOLD,12); inport.addActionListener(this); outport=new JButton(导出); outport.setFont
17、(new Font(TimesRomn,Font.BOLD,12); outport.addActionListener(this); buttonPanel.add(save); buttonPanel.add(delete); buttonPanel.add(inport); buttonPanel.add(outport); void setTitle(int year,int mouth,int day) if(day!=0) remove(title); title=new JLabel(year+年+mouth+月+day+日); title.setFont(new Font(Ti
18、mesRomn,Font.BOLD,18); title.setHorizontalAlignment(SwingConstants.CENTER); add(title,BorderLayout.NORTH); validate(); setDateexistence(true); /设置dateexistence else remove(title); title=new JLabel(请选择日期); title.setFont(new Font(TimesRomn,Font.BOLD,18); title.setHorizontalAlignment(SwingConstants.CEN
19、TER); add(title,BorderLayout.NORTH); validate(); setDateexistence(false); /设置dateexistence /钮事件处理 void dataSave() try hashOut= new ObjectOutputStream(new FileOutputStream(file); catch (FileNotFoundException e2) / TODO Auto-generated catch block JOptionPane.showMessageDialog(null, save:写文件时,没有找到data文
20、件。); /异常提示 e2.printStackTrace(); catch (IOException e2) / TODO Auto-generated catch block JOptionPane.showMessageDialog(null, save:写文件时,没有找到data文件,写入异常。); /异常提示 e2.printStackTrace(); if(data.containsKey(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day)&t.getText().trim().equals()
21、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 (IOException e1) / T
22、ODO Auto-generated catch block JOptionPane.showMessageDialog(null, save:无法保存文件,Objectoutputstream异常。); /异常提示 e1.printStackTrace(); else try hashOut.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); datasave); /测试save void newDate() if(data.get(Integer.toString(yea
23、r)+Integer.toString(mouth)+Integer.toString(day)!=null) t.setText(data.get(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day); else t.setText(); /删除日记 void dataDelete() /判断是当前日期是否有日记,给提示,然后删除, if(data.containsKey(Integer.toString(year)+Integer.toString(mouth)+Integer.toString(day)
24、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, 此日期无日志。); /异常提示 /导入日记 void inport()
25、JFileChooser fc = new JFileChooser(); fc.setFileFilter(new TxtFilter(); int val=fc.showOpenDialog(this); if(val=fc.APPROVE_OPTION) File name=fc.getSelectedFile(); char c=new char(int) name.length(); FileReader in; try in = new FileReader(name); in.read(c); t.setText(new String(c); dataSave(); in.clo
26、se(); catch (FileNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); /导出日记 void outport() String pass = JOptionPane.showInputDialog(请输入密码!); if(!pass.equals(journalpass) JOptionPane.showMessageDialog
27、(new JDialog(),密码错误,不能导出!); return ; JFileChooser fc = new JFileChooser(); fc.setFileFilter(new TxtFilter(); fc.showSaveDialog(this); File name = fc.getSelectedFile(); if(name = null) return ; try ObjectInputStream in = new ObjectInputStream(new FileInputStream(file); PrintWriter out = new PrintWriter(new FileOutputStream(name); Hashtable o = (Hashtable)in.readObject(); TreeMap map = new TreeMap(o); Set mapping = map.entrySet(); for (Iterator i = mapping.iterator(); i.hasNext();) Map.Entry me = (Map.Entry)i.next(); out.println(me.getKey(); out.println(String)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1