自制简单的记事本
程序代码:
package com.tocean.ch15.exec; import java.awt.BorderLayout; public class NotePad extends JFrame implements ActionListener { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { NotePad frame = new NotePad(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * 通过拖拉方式弄成的界面 */ public NotePad() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 684, 444); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("\u6587\u4EF6"); menuBar.add(menu); newMenuItem = new JMenuItem("\u65B0\u5EFA"); newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK)); menu.add(newMenuItem); openMenuItem = new JMenuItem("\u6253\u5F00"); openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK)); menu.add(openMenuItem); saveMenuItem = new JMenuItem("\u4FDD\u5B58"); saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); menu.add(saveMenuItem); saveasMenuItem = new JMenuItem("\u53E6\u5B58\u4E3A"); menu.add(saveasMenuItem); exitMenuItem = new JMenuItem("\u9000\u51FA"); menu.add(exitMenuItem); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); textPane = new JTextPane(); contentPane.add(textPane, BorderLayout.CENTER); addListeners(); } /** * 注册事件 */ private void addListeners() { exitMenuItem.addActionListener(this); saveasMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); textPane.addKeyListener(new KeyAdapter(){ @Override public void keyTyped(KeyEvent e) { isSaved=false; super.keyTyped(e); } }); } /** * 打开 */ private void open(){ if ( !isSaved) {// 表示该文件是一个新建的文件 int ok = JOptionPane.showConfirmDialog(this, "是否保存正在编辑的文件?"); if (JOptionPane.YES_OPTION == ok) { if (saveFile == null) { saveAs(); } else { save(); } } } JFileChooser jfc = new JFileChooser(); int option=jfc.showOpenDialog(this); if(JFileChooser.APPROVE_OPTION==option){ saveFile =jfc.getSelectedFile(); isSaved=true; this.setTitle(saveFile.getAbsolutePath()); //用流读取文件内容,然后显示到JTextPanes上 BufferedReader in = null; try { in = new BufferedReader(new FileReader(saveFile)); String line=null; StringBuffer sb=new StringBuffer(); while((line=in.readLine())!=null){ sb.append(line).append("\n"); } textPane.setText(sb.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if (null != in) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 新建文件 * 保存正在编辑的文件 */ private void newFile() { if ( !isSaved) {// 表示该文件是一个新建的文件 int ok = JOptionPane.showConfirmDialog(this, "是否保存正在编辑的文件?"); if (JOptionPane.YES_OPTION == ok) { if (saveFile == null) { saveAs(); } else { save(); } } } textPane.setText(""); saveFile = null; isSaved=false; this.setTitle("新建文件"); } /** * 保存 */ private void save() { if (null == saveFile) {// 表示该文件是一个新建的文件 saveAs(); } else {// 如果不为空,表明已经当前正在编辑的文件就是saveFile String content = textPane.getText(); saveFile(saveFile, content); isSaved = true; } } /** * 另存为 */ private void saveAs() { JFileChooser jfc = new JFileChooser(); int option = jfc.showSaveDialog(this); if (JFileChooser.APPROVE_OPTION == option) { File file = jfc.getSelectedFile(); String content = textPane.getText(); if (file.exists()) {// 如果原有文件已经存在,则提示覆盖 int ok = JOptionPane.showConfirmDialog(this, "该文件已经存在,是否覆盖文件?"); if (JOptionPane.YES_OPTION == ok) { saveFile(file, content); isSaved = true; saveFile = file; this.setTitle(file.getAbsolutePath()); } } else {// 如果文件不存在,则直接保存 saveFile(file, content); isSaved = true; saveFile = file; this.setTitle(file.getAbsolutePath()); } } } /** * 保存文件 */ private void saveFile(File file, String content) { BufferedWriter out = null; try { out = new BufferedWriter(new FileWriter(file)); out.write(content); out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != out) try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 退出 */ private void exit() { if (!isSaved) { int ok = JOptionPane.showConfirmDialog(this, "是否保存文件"); if (JOptionPane.YES_OPTION == ok) { saveAs(); System.exit(0); } else if (JOptionPane.NO_OPTION == ok) { System.exit(0); } } System.exit(0); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == exitMenuItem) { exit(); } else if (e.getSource() == saveasMenuItem) { saveAs(); } else if (e.getSource() == saveMenuItem) { save(); } else if (e.getSource() == newMenuItem) { newFile(); }else if (e.getSource() == openMenuItem) { open(); } } private JTextPane textPane; private File saveFile; private boolean isSaved = false; private JMenuItem exitMenuItem; private JMenuItem saveasMenuItem; private JMenuItem saveMenuItem; private JMenuItem newMenuItem; private JMenuItem openMenuItem; }