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

📄 notepad.java

📁 JAVA实现记事本
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        this.setTitle("未命名文件 - JAVA记事本");
        return true;
    }

    public boolean open()
    {
        if (isModified)
        {
            int ans = JOptionPane.showConfirmDialog(this, "文件已被修改,是否要保存该文件?", "保存文件", JOptionPane.YES_NO_CANCEL_OPTION);
            if (ans == JOptionPane.OK_OPTION)
                save();
            else if (ans == JOptionPane.CANCEL_OPTION)
                return false;
        }
        textArea.setText("");
        chooser.setTitle("打开文件");
        chooser.setMode(FileDialog.LOAD);
        chooser.setVisible(true);
        if (chooser.getFile() == null)
            return false;
        file = new File(chooser.getDirectory(), chooser.getFile());
        RandomAccessFile access = null;
        try
        {
            access = new RandomAccessFile(file, "r");
            String str = "";
            while ((str = access.readLine()) != null)
            {
                textArea.append(str + System.getProperty("line.separator"));
            }
            isModified = false;
            this.setTitle(file.getName() + " - JAVA记事本");
            editActions.get("undo").setEnabled(false);
        } catch (IOException ioe)
        {
            JOptionPane.showMessageDialog(this, "读取文件发生错误!", "错误", JOptionPane.ERROR_MESSAGE);
            ioe.printStackTrace();
            return false;
        } finally
        {
            if (access != null)
            {    try
                {
                    access.close();
                } catch (IOException ex)
                {
                }
            }
        }
        return true;
    }
    
    public boolean save()
    {
        if (file == null)
            return saveAs();
        RandomAccessFile access = null;
        try
        {
            access = new RandomAccessFile(file, "rw");
            access.writeBytes(textArea.getText());
            isModified = false;
        } catch (IOException ex)
        {
            JOptionPane.showMessageDialog(this, "保存文件发生错误!", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        } finally
        {
            if (access != null)
            {
                try
                {
                    access.close();
                } catch (IOException ex)
                {
                    Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return true;
    }
    
    public boolean saveAs()
    {
        chooser.setMode(FileDialog.SAVE);
        chooser.setTitle("保存文件...");
        chooser.setVisible(true);
        if (chooser.getFile() == null)
            return false;
        file = new File(chooser.getDirectory(), chooser.getFile());
        RandomAccessFile access = null;
        try
        {
            if (!file.exists())
                file.createNewFile();
            access = new RandomAccessFile(file, "rw");
            access.writeBytes(textArea.getText());
            isModified = false;
            this.setTitle(file.getName() + " - JAVA记事本");
        } catch (IOException ex)
        {
            JOptionPane.showMessageDialog(this, "保存文件发生错误!", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        } finally
        {
            if (access != null)
            {
                try
                {
                    access.close();
                } catch (IOException ex)
                {
                    Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return true;
    }
    public KeyStroke getKeys(int keyChar, int modifiers)
    {
        return KeyStroke.getKeyStroke(keyChar, modifiers);
    }
    
    public void exit()
    {
        if (isModified)
        {
            int ans = JOptionPane.showConfirmDialog(this, "是否要保存对文件的修改?", "保存", JOptionPane.YES_NO_CANCEL_OPTION);
            if (ans == JOptionPane.OK_OPTION)
                save();
            else if (ans == JOptionPane.CANCEL_OPTION)
                return ;
        }
        System.exit(0);
    }
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == toolbarItem)
            toolbar.setVisible(toolbarItem.isSelected());
        if (e.getSource() == statebarItem)
            statebar.setVisible(statebarItem.isSelected());
        if (e.getSource() == wordWrapItem)
            textArea.setLineWrap(wordWrapItem.isSelected());
        if (e.getSource() == dateItem)
            textArea.insert(DateFormat.getInstance().format(new Date()), textArea.getCaretPosition());
        if (e.getSource() == openItem || e.getSource() == openButton)
            open();
        if (e.getSource() == saveButton || e.getSource() == saveItem)
            save();
        if (e.getSource() == saveAsItem)
            saveAs();
        if (e.getSource() == delButton || e.getSource() == delItem)
            textArea.replaceSelection("");
        if (e.getSource() == exitItem)
            exit();
        if (e.getSource() == newItem || e.getSource() == newButton)
            newFile();
        if (e.getSource() == aboutButton || e.getSource() == aboutItem)
            aboutDialog.setVisible(true);
        if (e.getSource() == helpButton || e.getSource() == helpItem)
            showHelp();
        if ( e.getSource() == searchButton || e.getSource() == searchItem || e.getSource() == findNextButton || e.getSource() == findNextItem
            || e.getSource() == fontButton || e.getSource() == fontItem || e.getSource() == gotoItem
            || e.getSource() == replaceButton || e.getSource() == replaceItem)
            JOptionPane.showMessageDialog(this, "\u672c\u8bb0\u4e8b\u672c\u6ca1\u6709\u8d85\u7ea7\u725b\u529b ^_^ \u8c22\u8c22\u60a8\u7684\u4f7f\u7528\uff01");
    }

    public void caretUpdate(CaretEvent e)
    {
        boolean isSelectText = textArea.getSelectedText() != null && !textArea.getSelectedText().equals("");
        editActions.get("undo").setEnabled(undo.canUndo());
        editActions.get("redo").setEnabled(undo.canRedo());
        editActions.get(DefaultEditorKit.copyAction).setEnabled(isSelectText);
        editActions.get(DefaultEditorKit.cutAction).setEnabled(isSelectText);
        delItem.setEnabled(isSelectText);
        delButton.setEnabled(isSelectText);
        popdel.setEnabled(isSelectText);
    }
    
    public void changedUpdate(DocumentEvent e)
    {
        
    }

    public void insertUpdate(DocumentEvent e)
    {
        isModified = true;
    }

    public void removeUpdate(DocumentEvent e)
    {
        isModified = true;
    }
    
    private class MyMenuItem extends JMenuItem
    {

        public MyMenuItem()
        {
            super();
        }

        public MyMenuItem(String text, int mnemonic)
        {
            super(text, mnemonic);
            addActionListener(Notepad.this);
        }

        public MyMenuItem(String text, int mnemonic, KeyStroke key)
        {
            super(text, mnemonic);
            setAccelerator(key);
            addActionListener(Notepad.this);
        }

        public MyMenuItem(String text, int mnemonic, KeyStroke key, Action action)
        {
            super(text, mnemonic);
            setAction(action);
            setAccelerator(key);
            setText(text);
            addActionListener(Notepad.this);
        }
    }

    private class MyButton extends JButton
    {

        public MyButton(Icon icon, String toolTip)
        {
            this(icon, toolTip, null);
        }
        
        public MyButton(Icon icon, String toolTip, Action action)
        {
            super(action);
            setIcon(icon);
            setToolTipText(toolTip);
            setFocusable(false);
            setText("");
            addActionListener(Notepad.this);
        }
    }
    
    private class UndoAction extends AbstractAction
    {

        public UndoAction()
        {
            super("撤消(U)");
        }

        public void actionPerformed(ActionEvent e)
        {
            if (undo.canUndo())
            {
                undo.undo();
            }
        }        
    }
    
    private class RedoAction extends AbstractAction
    {

        public void actionPerformed(ActionEvent e)
        {
            if (undo.canRedo())
            {
                undo.redo();
            }
        }
    }
    
    private class AboutDialog extends JDialog
    {
        public AboutDialog(Frame parent) 
        {
            super(parent, "关于", true);
            init();
        }
        public void init()
        {
            JLabel lab = new JLabel(new ImageIcon("Notepad.png"));
            setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
            getContentPane().add( new JLabel(" "), BorderLayout.PAGE_START);
            getContentPane().add( new JLabel("  "), BorderLayout.LINE_START);
            getContentPane().add(new JLabel("    "), BorderLayout.LINE_END);
            getContentPane().add(new JLabel(" "), BorderLayout.PAGE_END);
            lab.setHorizontalAlignment(SwingConstants.CENTER);
            lab.setText("<html><TABLE><TR><TD colspan=\"2\"><center><strong>关于JAVA记事本</strong></center></TD></TR><TR><TD colspan=\"2\"><center>杭州电子科技大学电子信息学院</center></TD></TR><TR><TD>作者:</TD><TD>dunququ</TD></TR><TR><TD>Email:</TD><TD>dunququ10@163.com</TD></TR><TR><TD></TD></TR></TABLE></hmtl>");
            lab.setVerticalAlignment(SwingConstants.TOP);
            getContentPane().add(lab, BorderLayout.CENTER);
            setResizable(false);
            pack();
            this.addMouseListener(new MouseAdapter() 
            {
                public void mouseClicked(MouseEvent e)
                {
                    setVisible(false);
                }
            });
        }
        
    }
   
    
    public static void main(String[] args)
    {
        try        
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex)        
        {
        }
        
        EventQueue.invokeLater(new Runnable()        
        {

            public void run()            
            {
                new Notepad("未命名文件 - JAVA记事本").setVisible(true);
            }
        });
    }
}

⌨️ 快捷键说明

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