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

📄 notepad.java

📁 此程序是用标准的JAVA2实现的一个notepad程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        public void propertyChange(PropertyChangeEvent e) {            String propertyName = e.getPropertyName();            if (e.getPropertyName().equals(Action.NAME)) {                String text = (String) e.getNewValue();                menuItem.setText(text);            } else if (propertyName.equals("enabled")) {                Boolean enabledState = (Boolean) e.getNewValue();                menuItem.setEnabled(enabledState.booleanValue());            }        }    }    private JTextComponent editor;    private Hashtable commands;    private Hashtable menuItems;    private JMenuBar menubar;    private JToolBar toolbar;    private JComponent status;    private JFrame elementTreeFrame;    protected ElementTreePanel elementTreePanel;    protected FileDialog fileDialog;    /**     * Listener for the edits on the current document.     */    protected UndoableEditListener undoHandler = new UndoHandler();    /** UndoManager that we add edits to. */    protected UndoManager undo = new UndoManager();    /**     * Suffix applied to the key used in resource file     * lookups for an image.     */    public static final String imageSuffix = "Image";    /**     * Suffix applied to the key used in resource file     * lookups for a label.     */    public static final String labelSuffix = "Label";    /**     * Suffix applied to the key used in resource file     * lookups for an action.     */    public static final String actionSuffix = "Action";    /**     * Suffix applied to the key used in resource file     * lookups for tooltip text.     */    public static final String tipSuffix = "Tooltip";    public static final String openAction = "open";    public static final String newAction  = "new";    public static final String saveAction = "save";    public static final String exitAction = "exit";    public static final String showElementTreeAction = "showElementTree";    class UndoHandler implements UndoableEditListener {	/**	 * Messaged when the Document has created an edit, the edit is	 * added to <code>undo</code>, an instance of UndoManager.	 */        public void undoableEditHappened(UndoableEditEvent e) {	    undo.addEdit(e.getEdit());	    undoAction.update();	    redoAction.update();	}    }    /**     * FIXME - I'm not very useful yet     */    class StatusBar extends JComponent {        public StatusBar() {	    super();	    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));	}        public void paint(Graphics g) {	    super.paint(g);	}    }    // --- action implementations -----------------------------------    private UndoAction undoAction = new UndoAction();    private RedoAction redoAction = new RedoAction();    /**     * Actions defined by the Notepad class     */    private Action[] defaultActions = {	new NewAction(),	new OpenAction(),        new SaveAction(),	new ExitAction(),	new ShowElementTreeAction(),        undoAction,        redoAction    };    class UndoAction extends AbstractAction {	public UndoAction() {	    super("Undo");	    setEnabled(false);	}	public void actionPerformed(ActionEvent e) {	    try {		undo.undo();	    } catch (CannotUndoException ex) {		System.out.println("Unable to undo: " + ex);		ex.printStackTrace();	    }	    update();	    redoAction.update();	}	protected void update() {	    if(undo.canUndo()) {		setEnabled(true);		putValue(Action.NAME, undo.getUndoPresentationName());	    }	    else {		setEnabled(false);		putValue(Action.NAME, "Undo");	    }	}    }    class RedoAction extends AbstractAction {	public RedoAction() {	    super("Redo");	    setEnabled(false);	}	public void actionPerformed(ActionEvent e) {	    try {		undo.redo();	    } catch (CannotRedoException ex) {		System.out.println("Unable to redo: " + ex);		ex.printStackTrace();	    }	    update();	    undoAction.update();	}	protected void update() {	    if(undo.canRedo()) {		setEnabled(true);		putValue(Action.NAME, undo.getRedoPresentationName());	    }	    else {		setEnabled(false);		putValue(Action.NAME, "Redo");	    }	}    }    class OpenAction extends NewAction {	OpenAction() {	    super(openAction);	}        public void actionPerformed(ActionEvent e) {	    Frame frame = getFrame();            JFileChooser chooser = new JFileChooser();            int ret = chooser.showOpenDialog(frame);            if (ret != JFileChooser.APPROVE_OPTION) {		return;	    }            File f = chooser.getSelectedFile();	    if (f.isFile() && f.canRead()) {		Document oldDoc = getEditor().getDocument();		if(oldDoc != null)		    oldDoc.removeUndoableEditListener(undoHandler);		if (elementTreePanel != null) {		    elementTreePanel.setEditor(null);		}		getEditor().setDocument(new PlainDocument());                frame.setTitle(f.getName());		Thread loader = new FileLoader(f, editor.getDocument());		loader.start();	    } else {                JOptionPane.showMessageDialog(getFrame(),                        "Could not open file: " + f,                        "Error opening file",                        JOptionPane.ERROR_MESSAGE);	    }	}    }        class SaveAction extends AbstractAction {	SaveAction() {	    super(saveAction);	}        public void actionPerformed(ActionEvent e) {            Frame frame = getFrame();            JFileChooser chooser = new JFileChooser();            int ret = chooser.showSaveDialog(frame);            if (ret != JFileChooser.APPROVE_OPTION) {                return;            }            File f = chooser.getSelectedFile();            frame.setTitle(f.getName());            Thread saver = new FileSaver(f, editor.getDocument());            saver.start();	}    }    class NewAction extends AbstractAction {	NewAction() {	    super(newAction);	}	NewAction(String nm) {	    super(nm);	}        public void actionPerformed(ActionEvent e) {	    Document oldDoc = getEditor().getDocument();	    if(oldDoc != null)		oldDoc.removeUndoableEditListener(undoHandler);	    getEditor().setDocument(new PlainDocument());	    getEditor().getDocument().addUndoableEditListener(undoHandler);	    resetUndoManager();            getFrame().setTitle(resources.getString("Title"));	    revalidate();	}    }    /**     * Really lame implementation of an exit command     */    class ExitAction extends AbstractAction {	ExitAction() {	    super(exitAction);	}        public void actionPerformed(ActionEvent e) {	    System.exit(0);	}    }    /**     * Action that brings up a JFrame with a JTree showing the structure     * of the document.     */    class ShowElementTreeAction extends AbstractAction {	ShowElementTreeAction() {	    super(showElementTreeAction);	}	ShowElementTreeAction(String nm) {	    super(nm);	}        public void actionPerformed(ActionEvent e) {	    if(elementTreeFrame == null) {		// Create a frame containing an instance of 		// ElementTreePanel.		try {		    String    title = resources.getString			                ("ElementTreeFrameTitle");		    elementTreeFrame = new JFrame(title);		} catch (MissingResourceException mre) {		    elementTreeFrame = new JFrame();		}		elementTreeFrame.addWindowListener(new WindowAdapter() {		    public void windowClosing(WindowEvent weeee) {			elementTreeFrame.setVisible(false);		    }		});		Container fContentPane = elementTreeFrame.getContentPane();		fContentPane.setLayout(new BorderLayout());		elementTreePanel = new ElementTreePanel(getEditor());		fContentPane.add(elementTreePanel);		elementTreeFrame.pack();	    }	    elementTreeFrame.show();	}    }    /**     * Thread to load a file into the text storage model     */    class FileLoader extends Thread {	FileLoader(File f, Document doc) {	    setPriority(4);	    this.f = f;	    this.doc = doc;	}        public void run() {	    try {		// initialize the statusbar		status.removeAll();		JProgressBar progress = new JProgressBar();		progress.setMinimum(0);		progress.setMaximum((int) f.length());		status.add(progress);		status.revalidate();		// try to start reading		Reader in = new FileReader(f);		char[] buff = new char[4096];		int nch;		while ((nch = in.read(buff, 0, buff.length)) != -1) {		    doc.insertString(doc.getLength(), new String(buff, 0, nch), null);		    progress.setValue(progress.getValue() + nch);		}	    }	    catch (IOException e) {                final String msg = e.getMessage();                SwingUtilities.invokeLater(new Runnable() {                    public void run() {                        JOptionPane.showMessageDialog(getFrame(),                                "Could not open file: " + msg,                                "Error opening file",                                JOptionPane.ERROR_MESSAGE);	    }                });            }	    catch (BadLocationException e) {		System.err.println(e.getMessage());	    }            doc.addUndoableEditListener(undoHandler);            // we are done... get rid of progressbar            status.removeAll();            status.revalidate();            resetUndoManager();	    if (elementTreePanel != null) {		SwingUtilities.invokeLater(new Runnable() {		    public void run() {			elementTreePanel.setEditor(getEditor());		    }		});	    }	}	Document doc;	File f;    }    /**     * Thread to save a document to file     */    class FileSaver extends Thread {        Document doc;        File f;	FileSaver(File f, Document doc) {	    setPriority(4);	    this.f = f;	    this.doc = doc;	}        public void run() {	    try {		// initialize the statusbar		status.removeAll();		JProgressBar progress = new JProgressBar();		progress.setMinimum(0);		progress.setMaximum((int) doc.getLength());		status.add(progress);		status.revalidate();		// start writing		Writer out = new FileWriter(f);                Segment text = new Segment();                text.setPartialReturn(true);                int charsLeft = doc.getLength();		int offset = 0;                while (charsLeft > 0) {                    doc.getText(offset, Math.min(4096, charsLeft), text);                    out.write(text.array, text.offset, text.count);                    charsLeft -= text.count;                    offset += text.count;                    progress.setValue(offset);                    try {                        Thread.sleep(10);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                out.flush();                out.close();	    }	    catch (IOException e) {                final String msg = e.getMessage();                SwingUtilities.invokeLater(new Runnable() {                    public void run() {                        JOptionPane.showMessageDialog(getFrame(),                                "Could not save file: " + msg,                                "Error saving file",                                JOptionPane.ERROR_MESSAGE);	    }                });	    }	    catch (BadLocationException e) {		System.err.println(e.getMessage());	    }            // we are done... get rid of progressbar            status.removeAll();            status.revalidate();	}    }}

⌨️ 快捷键说明

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