JAVA记事本的图形用户界面应用程序含过滤
JAVA记事本的图形用户界面应用程序 过滤
题目简介:
整体分析:
实验代码:
package note; 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.JMenuBar; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JSeparator; import javax.swing.JTextArea; import java.awt.Font; /**记事本的图形用户界面应用程序 过滤 * @author 李祖林 *2017-6-23 */ public class Note3 implements ActionListener{ private JFrame frame; JMenuItem New,Open,Save,Close; JTextArea textArea; File fileName = new File("D://file.txt"); public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Note3 window = new Note3(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Note3() { frame = new JFrame(); frame.setBounds(100, 100, 725, 392); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(0, 0)); textArea = new JTextArea(); textArea.setFont(new Font("华文中宋", Font.BOLD, 17)); frame.getContentPane().add(textArea, BorderLayout.CENTER); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu menu = new JMenu("\u6587\u4EF6"); menuBar.add(menu); New = new JMenuItem("\u65B0\u5EFA"); menu.add(New);New.addActionListener(this); Open = new JMenuItem("\u6253\u5F00"); menu.add(Open);Open.addActionListener(this); Save = new JMenuItem("\u4FDD\u5B58"); menu.add(Save);Save.addActionListener(this); JSeparator separator = new JSeparator(); menu.add(separator); Close = new JMenuItem("\u9000\u51FA"); menu.add(Close);Close.addActionListener(this); JMenu menu_1 = new JMenu("\u7F16\u8F91"); menuBar.add(menu_1); JMenuItem menuItem_4 = new JMenuItem("\u590D\u5236"); menu_1.add(menuItem_4); JMenuItem menuItem_5 = new JMenuItem("\u526A\u5207"); menu_1.add(menuItem_5); JMenuItem menuItem_6 = new JMenuItem("\u7C98\u8D34"); menu_1.add(menuItem_6); } public void actionPerformed(ActionEvent e) { if (e.getSource() == Close) { System.exit(0); } else if (e.getSource() == New) { textArea.setText(""); } else if (e.getSource() == Save) { saveFile(); } else if (e.getSource() == Open) { textArea.setText(""); openFile(); } } public void saveFile(){ try { BufferedWriter bWriter = new BufferedWriter(new FileWriter(fileName)); //打开写入流 String text = textArea.getText(); //获取文本框内容 char[] ch = new char[1024]; //字符串->>字符数组 ch = text.toCharArray(); int tmp; for(int i = 0;i < ch.length;i++){ tmp = (int)ch[i]; //字符->ASCII值 if(tmp>='A'&&tmp<='Z'){ //过滤 tmp += 32; } ch[i] = (char)tmp; //ASCII值->字符 } text = String.valueOf(ch); //字符数组->>字符串 bWriter.write(text); //写入 bWriter.close(); } catch (IOException e) { System.err.println("保存文件发生错误!"); e.printStackTrace(); //打印轨迹 } } public void openFile(){ try { BufferedReader bReader = new BufferedReader(new FileReader(fileName)); String text; while((text = bReader.readLine())!=null){ textArea.append(text+"\n"); } bReader.close(); } catch (IOException e) { System.err.println("打开文件发生错误!"); e.printStackTrace(); } } }
实验结果:
过滤前:
过滤后: