JAVA记事本的图形用户界面应用程序 加密
题目简介:
整体分析:
实验代码:
import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFrame; import java.awt.BorderLayout; import javax.swing.JPanel; import javax.swing.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JSeparator; import javax.swing.JTextArea; import javax.xml.transform.Templates; /**记事本的图形用户界面应用程序 加密 * @author 李祖林 *2017-6-22 */ public class Note2 implements ActionListener{ private JFrame frame; JMenuItem New,Open,Save,Close; //四个菜单栏 JTextArea textArea; //输入框 File flieName = new File("D://file.txt"); //默认文件路径 public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Note2 window = new Note2(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Note2() { frame = new JFrame(); frame.setBounds(100, 100, 567, 350); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(0, 0)); textArea = new JTextArea(); frame.getContentPane().add(textArea, BorderLayout.CENTER); JMenuBar menuBar = new JMenuBar(); frame.getContentPane().add(menuBar, BorderLayout.NORTH); JMenu menu = new JMenu("u6587u4EF6"); menuBar.add(menu); New = new JMenuItem("u65B0u5EFA"); menu.add(New);New.addActionListener(this); Open = new JMenuItem("u6253u5F00"); menu.add(Open);Open.addActionListener(this); Save = new JMenuItem("u4FDDu5B58"); menu.add(Save);Save.addActionListener(this); JSeparator separator = new JSeparator(); menu.add(separator); Close = new JMenuItem("u9000u51FA"); menu.add(Close);Close.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == New){ textArea.setText(""); }else if(e.getSource() == Open){ textArea.setText(""); readFile(); }else if(e.getSource() == Save){ saveFile(); }else{ System.exit(0); } } /*解码的过程*/ public void readFile(){ try { BufferedReader bufferedReader = new BufferedReader(new FileReader(flieName)); String text; //每行字符串 char[] ch = new char[1024]; //字符数组 int tmp; //字符的ASCII码值 while((text = bufferedReader.readLine())!=null){ ch = text.toCharArray(); //字符串->字符数组 for(int i = 0;i < ch.length;i++){ tmp = (int)ch[i] - 30; //解密 if(tmp<0){ tmp += 127; } ch[i] = (char)tmp; //转化为字符 } text = String.valueOf(ch); //转化为字符串 textArea.append(text); } bufferedReader.close(); } catch (IOException e) { System.err.println("读取文件发生错误!"); } } /*加密的过程*/ public void saveFile(){ try { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(flieName)); String string = textArea.getText(); //获取文本框内容 char[] ch = new char[1024]; //字符数组 int tmp; //字符的ASCII码值 ch = string.toCharArray(); //字符串->字符数组 for(int i = 0;i < string.length();i++){ tmp = (int)ch[i] + 30; //加密:ASCII+30(<=127) if(tmp>127) tmp -= 127; ch[i] = (char)tmp; } string = String.valueOf(ch); //转化成字符串的两种方式 /*两种方式:string = ch.toString();*/ bufferedWriter.write(string); //写入 bufferedWriter.close(); } catch (IOException e) { System.err.println("写入文件错误!"); } } }
实验结果:
加密前:
加密后: