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

📄 mynotepad.java

📁 Java记事本,类似于Windows附带的记事本
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        popupMenu_Undo = new JMenuItem("撤销(Z)");        popupMenu_Redo = new JMenuItem("恢复撤销(B)");        popupMenu_Cut = new JMenuItem("剪切(X)");        popupMenu_Copy = new JMenuItem("复制(C)");        popupMenu_Paste = new JMenuItem("粘贴(V)");        popupMenu_Delete = new JMenuItem("删除(D)");        popupMenu_SelectAll = new JMenuItem("全选(A)");        popupMenu_Undo.setMnemonic('Z');        popupMenu_Redo.setMnemonic('B');        popupMenu_Cut.setMnemonic('X');        popupMenu_Copy.setMnemonic('C');        popupMenu_Paste.setMnemonic('V');        popupMenu_Delete.setMnemonic('D');        popupMenu_SelectAll.setMnemonic('A');        popupMenu.add(popupMenu_Undo);        popupMenu.add(popupMenu_Redo);        popupMenu.addSeparator();        popupMenu.add(popupMenu_Cut);        popupMenu.add(popupMenu_Copy);        popupMenu.add(popupMenu_Paste);        popupMenu.addSeparator();        popupMenu.add(popupMenu_Delete);        popupMenu.addSeparator();        popupMenu.add(popupMenu_SelectAll);        popupMenu_Undo.addActionListener(this);        popupMenu_Redo.addActionListener(this);        popupMenu_Cut.addActionListener(this);        popupMenu_Copy.addActionListener(this);        popupMenu_Paste.addActionListener(this);        popupMenu_Delete.addActionListener(this);        popupMenu_SelectAll.addActionListener(this);        //$$$$$$$$$$$$$$右键菜单完毕$$$$$$$$$$$$$$$$$$$$$$          this.getContentPane().add(tbar, BorderLayout.NORTH);        JScrollPane scrPane = new JScrollPane(tex);        scrPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);        getContentPane().add(scrPane);        //以下为实现撤销功能          tex.getDocument().addUndoableEditListener(new UndoableEditListener() {            public void undoableEditHappened(UndoableEditEvent e) {//添加撤销管理器                undo.addEdit(e.getEdit());            }        });        tex.getDocument().addDocumentListener(this);//添加监听器        setJMenuBar(mb);        statusLabel = new JLabel("按Ctrl+F1获取帮助");//添加状态栏        this.getContentPane().add(statusLabel, BorderLayout.SOUTH);        setSize(580, 700);        this.show(true);    }//¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥事件处理部分    public void actionPerformed(ActionEvent e) {        if (e.getSource() == bt_new || e.getSource() == mi1) {//新建            tex.requestFocusInWindow();            Object[] options = {"确定", "取消"};            int s = JOptionPane.showOptionDialog(null, "请注意保存文档!按“确定”新建文件",                    "警告!", JOptionPane.DEFAULT_OPTION,                    JOptionPane.WARNING_MESSAGE, null, options, options[0]);            if (s == JOptionPane.YES_OPTION) {                tex.setText("");            }            statusLabel.setText("空白文档");            file = null;        } else if (e.getSource() == mi2 || e.getSource() == bt_open) {//打开            tex.requestFocusInWindow();            if (file != null) {                filechooser.setSelectedFile(file);            }            filechooser.addChoosableFileFilter(new JAVAFileFilter("txt"));//实现文件过滤,见下面的JAVAFileFilter类            filechooser.addChoosableFileFilter(new JAVAFileFilter("c"));            filechooser.addChoosableFileFilter(new JAVAFileFilter("cpp"));            filechooser.addChoosableFileFilter(new JAVAFileFilter("sql"));            filechooser.addChoosableFileFilter(new JAVAFileFilter("doc"));            filechooser.addChoosableFileFilter(new JAVAFileFilter("class"));            filechooser.addChoosableFileFilter(new JAVAFileFilter("java"));            int returnVal = filechooser.showOpenDialog(DaveNotepad.this);            if (returnVal == JFileChooser.APPROVE_OPTION) {                file = filechooser.getSelectedFile();                openFile(tex);                isNewfile = false; //文件为已保存的                statusLabel.setText("当前打开文件:" + file.getAbsoluteFile() + "   行:" + Integer.toString(tex.getRows()) + " 列:" + Integer.toString(tex.getColumns()));//设置状态栏显示当前打开文档的路径和文件名                tex.setCaretPosition(0);//设置光标的位置在开始            }        } else if (e.getSource() == mi3 || e.getSource() == bt_save) {//保存            tex.requestFocusInWindow();            saveFile(tex);        } else if (e.getSource() == mi4) { //另存为            tex.requestFocusInWindow();            isNewfile = true;            saveFile(tex);        } else if (e.getSource() == mi5) {//页面设置            tex.requestFocusInWindow();        } else if (e.getSource() == mi6) {//打印            tex.requestFocusInWindow();            try {                tex.print();            } catch (PrinterException e1) {                e1.printStackTrace();            }        } else if (e.getSource() == mi7) {//退出            tex.requestFocusInWindow();            quitProg();        } else if (e.getSource() == me1 || e.getSource() == bt_undo || e.getSource() == popupMenu_Undo) {//撤销            tex.requestFocusInWindow();            if (undo.canUndo()) {                try {                    undo.undo();                    me12.setEnabled(true);                    popupMenu_Redo.setEnabled(true);                    bt_redo.setEnabled(true);                } catch (CannotUndoException ex) {                    System.out.println("Unable to undo: " + ex);                    ex.printStackTrace();                }            }            if (!undo.canUndo()) {                me1.setEnabled(false);                popupMenu_Undo.setEnabled(false);                bt_undo.setEnabled(false);                me12.setEnabled(true);                popupMenu_Redo.setEnabled(true);                bt_redo.setEnabled(true);            }        } else if (e.getSource() == me12 || e.getSource() == bt_redo || e.getSource() == popupMenu_Redo) {//恢复撤销            tex.requestFocusInWindow();            if (undo.canRedo()) {                try {                    undo.redo();                    me12.setEnabled(true);                    popupMenu_Redo.setEnabled(true);                    bt_redo.setEnabled(true);                } catch (CannotUndoException ex) {                    System.out.println("Unable to redo: " + ex);                    ex.printStackTrace();                }            }            if (!undo.canRedo()) {                me1.setEnabled(true);                popupMenu_Undo.setEnabled(true);                bt_undo.setEnabled(true);                me12.setEnabled(false);                popupMenu_Redo.setEnabled(false);                bt_redo.setEnabled(false);            }        } else if (e.getSource() == me2 || e.getSource() == bt_cut || e.getSource() == popupMenu_Cut) {//剪切            tex.requestFocusInWindow();            tex.cut();            checkMenuItemEnabled();//检查菜单的可用性        } else if (e.getSource() == me3 || e.getSource() == bt_copy || e.getSource() == popupMenu_Copy) {//复制            tex.requestFocusInWindow();            tex.copy();            checkMenuItemEnabled();//检查菜单的可用性        } else if (e.getSource() == me4 || e.getSource() == bt_paste || e.getSource() == popupMenu_Paste) {//粘贴            tex.requestFocusInWindow();            tex.paste();            checkMenuItemEnabled();//检查菜单的可用性        } else if (e.getSource() == me5 || e.getSource() == popupMenu_Delete) {//删除            tex.requestFocusInWindow();            String sCopy = tex.getSelectedText();//获取当前选中文本            if (sCopy != null) {                tex.replaceSelection("");            }            checkMenuItemEnabled();//检查菜单的可用性        } else if (e.getSource() == me6) {//查找            tex.requestFocusInWindow();            mySearch();        } else if (e.getSource() == me7) {//查找下一个            tex.requestFocusInWindow();            mySearch();        } else if (e.getSource() == me8) {//替换            tex.requestFocusInWindow();            mySearch();        } else if (e.getSource() == me9) {//转到            tex.requestFocusInWindow();        } else if (e.getSource() == me10 || e.getSource() == popupMenu_SelectAll) {//全选            tex.requestFocusInWindow();            tex.selectAll();        } else if (e.getSource() == me11) {//时间/日期            tex.requestFocusInWindow();            getDate();        } else if (e.getSource() == mo1) { //设置文本域可以自动换行            tex.requestFocusInWindow();            if (tex.getLineWrap()) {                tex.setLineWrap(false);            } else {                tex.setLineWrap(true);            }        } else if (e.getSource() == mo2) {//字体            tex.requestFocusInWindow();            SetFont setfont = new SetFont(tex);        } else if (e.getSource() == mh1) {//帮助主题            helpFrame();        } else if (e.getSource() == mh2) {//关于本记事本            aboutFrame();        } else if (e.getSource() == bt_bold) {//粗体设置            Font newfont;            if (tex.getFont().getStyle() == Font.BOLD) {                newfont = new Font(tex.getFont().getName(), Font.PLAIN, tex.getFont().getSize());            } else if (tex.getFont().getStyle() == Font.BOLD + Font.ITALIC) {                newfont = new Font(tex.getFont().getName(), Font.PLAIN + Font.ITALIC, tex.getFont().getSize());            } else {                newfont = new Font(tex.getFont().getName(), tex.getFont().getStyle() + Font.BOLD, tex.getFont().getSize());            }            tex.setFont(newfont);        } else if (e.getSource() == bt_italic) {            Font newfont;            if (tex.getFont().getStyle() == Font.ITALIC) {                newfont = new Font(tex.getFont().getName(), Font.PLAIN, tex.getFont().getSize());            } else if (tex.getFont().getStyle() == Font.ITALIC + Font.BOLD) {                newfont = new Font(tex.getFont().getName(), Font.PLAIN + Font.BOLD, tex.getFont().getSize());            } else {                newfont = new Font(tex.getFont().getName(), tex.getFont().getStyle() + Font.ITALIC, tex.getFont().getSize());            }            tex.setFont(newfont);        } else if (e.getActionCommand() == "Type") {            Font newfont = new Font(fontname, tex.getFont().getStyle(), tex.getFont().getSize());            tex.setFont(newfont);        } else if (e.getActionCommand() == "Size") {            Font newfont = new Font(tex.getFont().getName(), tex.getFont().getStyle(), fontsize);            tex.setFont(newfont);        }    }    //¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥事件处理结束    void quitProg() { //退出菜单的响应        Object[] options = {"确定", "取消"};        int s = JOptionPane.showOptionDialog(null, "请注意保存文档!按“确定”退出", "警告!",                JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,                options, options[0]);        if (s == JOptionPane.YES_OPTION) {            System.exit(0);        }    }    void getDate() { //获取当前系统日期和时间,对“日期/时间”菜单的响应        SimpleDateFormat TimeFormat = new SimpleDateFormat(                " yyyy-MM-dd HH:mm:ss ");        Calendar curdate = Calendar.getInstance();        curdate = Calendar.getInstance(Locale.CHINESE); //以上为获取系统当前的日期和时间        tex.insert(TimeFormat.format(curdate.getTime()), tex.getCaretPosition()); //将获得的日期和时间添加到文本的当前位置    }    void helpFrame() { //帮助菜单的弹出窗口        Icon help_icon = new ImageIcon("icons//doit.jpg");        String help = "感谢您的使用!\n本记事本程序暂无帮助,您可以参考WindowsXP的记事本帮助程序!\n" + "欢迎和作者联系交流!\n" + "Build By Davezhang\nQQ:442803117\nBolg:http://hi.baidu.com.442803117" + "\nEmail:442803117@qq.com";        JOptionPane.showConfirmDialog(null, help, "帮助",                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,                help_icon);    }    void aboutFrame() {//“关于本记事本”菜单的弹出窗口        Icon help_icon = new ImageIcon("icons//doit.jpg");        String help = "DOIT(R)记事本\n版本1.0 (内部版本号 2008.xp_sp2_dave.080423-10)\n版权" + "所有 (C) 2008-2008 DOIT。\n最终解释权归本人所有," + "授权给:\n\nBuild By Davezhang\nQQ:442803117\nBolg:http://hi.baidu.com.442803117" + "\nEmail:442803117@qq.com";        JOptionPane.showConfirmDialog(null, help, "帮助",                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,                help_icon);    }//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$保存文件和另存为文件    void saveFile(JTextArea text) {//保存文件和另存为文件        tex.requestFocusInWindow();        if (isNewfile) { //如果是未保存过的文件            JFileChooser fileChooser = new JFileChooser();            /*fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);            fileChooser.addChoosableFileFilter(new JAVAFileFilter("txt"));            fileChooser.addChoosableFileFilter(new JAVAFileFilter("c"));            fileChooser.addChoosableFileFilter(new JAVAFileFilter("cpp"));            fileChooser.addChoosableFileFilter(new JAVAFileFilter("sql"));                 fileChooser.addChoosableFileFilter(new JAVAFileFilter("doc"));            fileChooser.addChoosableFileFilter(new JAVAFileFilter("class"));            fileChooser.addChoosableFileFilter(new JAVAFileFilter("java"));*/            fileChooser.setApproveButtonText("确定");            fileChooser.setDialogTitle("另存为");            int result = fileChooser.showSaveDialog(this);            if (result == JFileChooser.CANCEL_OPTION) {                statusLabel.setText("没有选择任何文件");                return;            }            File saveFileName = fileChooser.getSelectedFile();            if (saveFileName == null || saveFileName.getName().equals("")) {                JOptionPane.showMessageDialog(this, "不合法的文件名", "不合法的文件名",                        JOptionPane.ERROR_MESSAGE);            } else {                try {                    FileWriter fw = new FileWriter(saveFileName);                    BufferedWriter bfw = new BufferedWriter(fw);                    bfw.write(tex.getText(), 0, tex.getText().length());                    bfw.flush();                    fw.close();                    isNewfile = false;                    file = saveFileName;                    oldValue = tex.getText();                    statusLabel.setText(" 当前打开文件:" + saveFileName.getAbsoluteFile());                } catch (IOException ioException) {                }            }        } else {//isNewfile为flase时,即文件已经保存过的情况            try {                FileWriter fw = new FileWriter(file);                BufferedWriter bfw = new BufferedWriter(fw);                bfw.write(tex.getText(), 0, tex.getText().length());                bfw.flush();                fw.close();            } catch (IOException ioException) {            }        }    }//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$打开文件    void openFile(JTextArea text) { //打开文件        try {            FileReader fr = new FileReader(file);            int len = (int) file.length();            char[] buffer = new char[len];            fr.read(buffer, 0, len);            fr.close();            text.setText(new String(buffer));        } catch (Exception e) {            e.printStackTrace();        }    }//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$查找和替换功能$$$$$$$$$$$$$$$$$$    public void mySearch() {        final JDialog findDialog = new JDialog(this, "查找与替换", true);        Container con = findDialog.getContentPane();        con.setLayout(new FlowLayout(FlowLayout.LEFT));        JLabel searchContentLabel = new JLabel("查找内容(N) :");

⌨️ 快捷键说明

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