文件名称:java写的记事本
文件大小:29KB
文件格式:JAVA
更新时间:2014-12-29 04:22:10
java记事本
用java写的记事本,1000行代码,基本上所有的功能都全了(和微软系统自带的记事本的相似度>70%) 其中菜单里新建模块的代码如下: // 菜单 // 新建(N)按钮事件监听 newTextItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (selectedFile == null && textArea.getText().equals("")) { return; } else { int btn = JOptionPane.showConfirmDialog(container, "是否保存到" + selectedFile + "?", "是否保存", JOptionPane.YES_NO_CANCEL_OPTION); if (btn == JOptionPane.CANCEL_OPTION) { return; } else if (btn == JOptionPane.YES_OPTION) { if (selectedFile == null && !textArea.getText().equals("")) { choose = new JFileChooser(); int state = choose.showSaveDialog(container); if (state == JFileChooser.APPROVE_OPTION) { try { File file = choose.getSelectedFile(); BufferedWriter bw = new BufferedWriter( new FileWriter(file)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); file.createNewFile(); bw.close(); } catch (IOException e) { JOptionPane.showConfirmDialog(container, "保存文件失败!", "ERROR", JOptionPane.ERROR_MESSAGE); } } } else if (selectedFile != null) { try { BufferedWriter bw = new BufferedWriter( new FileWriter(selectedFile)); String str = textArea.getText(); String[] lines = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.flush(); bw.close(); } catch (IOException e) { // JOptionPane.showConfirmDialog(container, // "保存文件失败!", // "ERROR", JOptionPane.ERROR_MESSAGE); } } } } textArea.setText(""); newPage = true; selectedFile = null; textField.setText(""); } }); 如果你初学或也在写记事本,这个绝对对你有帮助,且最适合你1