⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notepad.java

📁 一个用swt开发的JAVA记事本
💻 JAVA
字号:
/* * Notepad.java * * Created on 2007年1月4日, 下午12:00 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package net.vlinux.notepad;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.ObjectInputStream;import java.io.FileInputStream;import java.io.ObjectOutputStream;import net.vlinux.notepad.gui.NotepadGUI;/** * * @author vlinux */public class Notepad {        public static final String CONFIGURE_FILE = "notepad.cfg";    public static final int DEFAULT_LOCAL_X = 250;    public static final int DEFAULT_LOCAL_Y = 190;    public static final int DEFAULT_WIDTH = 850;    public static final int DEFAULT_HEIGHT = 650;    public static final boolean DEFAULT_LINE_WRAP = false;    public static final String DEFAULT_TITLE = "新建文件.txt";        public static final String AUTHOR = "yushui";    public static final String HELP = "你个猪头,连这个都不会用,去死!";        private NotepadGUI gui;    private boolean changed = false;        private String title = this.DEFAULT_TITLE;    private String content = "";    private File editFile;        public boolean isChanged(){        return changed;    }        public void setChanged(boolean changed){        this.changed = changed;        if( changed == true ){            gui.setTitle( "未保存-"+title );        } else {            gui.setTitle( title );        }    }        public Notepad() throws NotepadException {        this(null);    }        public Notepad(String filename) throws NotepadException {        NotepadConfigure config = this.getConfigure();        if( filename!=null ){   //打开一个文件            editFile = openEditFile(new File(filename));        } else {                //新建文件            editFile = null;            title = this.DEFAULT_TITLE;            content = "";        }                if( config==null ){            gui = new NotepadGUI(this.DEFAULT_LOCAL_X,this.DEFAULT_LOCAL_Y,this.DEFAULT_WIDTH,this.DEFAULT_HEIGHT, DEFAULT_LINE_WRAP, this);        } else {            gui = new NotepadGUI(config.getLocalx(), config.getLocaly(), config.getWidth(), config.getHeight(), config.isLineWrap(), this);        }        show();    }        public void show() {        gui.setTextContent(content);        gui.setTitle(title);        gui.setVisible(true);    }            /**     * 打开要编辑的文件     */    public File openEditFile( File editFile ) throws NotepadException {        title = editFile.getName();        if( editFile.exists()==false ){    //文件不存在,创建之            try{                editFile.createNewFile();            }catch(IOException ioe){                throw new NotepadException("创建文件"+title+"失败");            }        }        //文件已经被创建,读取之        try {            content = getEditFileContent(editFile);        } catch (IOException ex) {            throw new NotepadException("读取文件"+title+"失败");        }        //如果不是一开始就打开        if( gui!=null ) {            show();            setChanged(false);        }        return this.editFile = editFile;    }            /**     * 创建新文件     */    public void createNewFile() {        editFile = null;        title = this.DEFAULT_TITLE;        content = "";        show();        setChanged(false);    }            /**     * 获取被编辑的文件     */    public File getEditFile() {        return this.editFile;    }                /**     * 获取被编辑的文件内容     */    public String getEditFileContent(File editFile) throws IOException {        StringBuffer content = new StringBuffer();        BufferedReader br = new BufferedReader( new FileReader( editFile ) );        while( br.ready() ){            content.append(br.readLine()+"\n");        }        br.close();        return content.toString();    }                /**     * 读取配置文件notepad.cfg     */    public NotepadConfigure getConfigure() {        NotepadConfigure config = null;        try {            ObjectInputStream ois = new ObjectInputStream( new FileInputStream( new File(CONFIGURE_FILE) ) );            config = (NotepadConfigure)ois.readObject();            ois.close();        } catch (Exception ex) {            //        } finally {            return config;        }    }            /**     * 保存配置文件notepad.cfg     */    public void saveConfigure( NotepadConfigure config ) {        try {            ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( new File(CONFIGURE_FILE) ) );            oos.writeObject(config);            oos.close();        } catch (Exception ex) {            //        } finally {            //        }    }            /**     * 保存被编辑的文件     */    public void saveEditFile(File editFile, String content) throws IOException {        editFile.delete();        FileOutputStream fos = new FileOutputStream( editFile );        fos.write(content.getBytes());        fos.close();        setChanged(false);    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -