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

📄 texteditor.java

📁 java语言编辑的一个文本编辑器,模块清晰明了,用到JAVA的反射机制,可以给选中的单词加不同的颜色进行高亮显示.调整字符大小.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**addMenuItem()方法用来设置菜单*/
    private void addMenuItem(int count,int num, String menulabel, String itemlabel,
            char shortlabel, ActionListener listen,JMenu menu,boolean bool,ImageIcon icon) {
        JMenuItem menuItem;
        if(bool) menuBar.add(menu);        
        if(icon!=null){
         menuItem = new JMenuItem(itemlabel,icon);  
        }
        else  menuItem=new JMenuItem(itemlabel);
        menuItem.setMnemonic(count);  //添加快捷方式
        if (shortlabel != '0')
            menuItem.setAccelerator(KeyStroke.getKeyStroke(shortlabel,KeyEvent.CTRL_MASK));//添加组合快捷键的操作
        menuItem.addActionListener(listen);//注册监听器的接口ActionListener
        /*设置文件*/
        if (menulabel.equals("文件"))  {
            fileMenu.add(menuItem);
            if(num==3) fileMenu.addSeparator();
        }
        /*设置编辑*/
        else if (menulabel.equals("编辑")) {
            editMenu.add(menuItem);
            switch (num) {
                case 0:undoItem = menuItem; //撤销
                undoItem.setEnabled(false);
                break;
                case 1: redoItem=menuItem;         //恢复
                redoItem.setEnabled(false);
                editMenu.addSeparator();break;
                case 5:                                                                         //删除
                    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
                    editMenu.addSeparator(); break;
                case 6:findItem = menuItem;
                findItem.setEnabled(false);break;//查找
                case 7:findnextItem = menuItem;   //查找下一个
                findnextItem.setEnabled(false);
                menuItem.setAccelerator(KeyStroke.getKeyStroke("F3"));break;
                case 10:menuItem.setAccelerator(KeyStroke.getKeyStroke("F5")); //时间/日期
            }
        }
        /*设置格式*/
        else if (menulabel.equals("格式")) {
            switch (num) {
                case 0:formatMenu.add(menuItem);     //插入
                formatMenu.addSeparator(); break;
                case 1:formatMenu.add(menuItem);break;     //字体
                case 2:JMenu colorMenu = new JMenu(itemlabel);   //颜色
                formatMenu.add(colorMenu);
                JMenuItem backColor = new JMenuItem("背景颜色(B)");
                JMenuItem fontColor = new JMenuItem("字体颜色(F)");                              
                colorMenu.add(backColor);
                colorMenu.addSeparator();
                colorMenu.add(fontColor);
                colorMenu.setMnemonic(KeyEvent.VK_C);
                backColor.setMnemonic(KeyEvent.VK_B);
                fontColor.setMnemonic(KeyEvent.VK_F);
                backColor.addActionListener(listen);//注册监听器的接口ActionListener
                fontColor.addActionListener(listen);//注册监听器的接口ActionListener
                break;
                case 3:                                 //视图风格
                    JMenu styleMenu= new JMenu(itemlabel);
                    styleMenu.setMnemonic(KeyEvent.VK_S);
                    formatMenu.add(styleMenu);
                    styleMenu.add(metal);
                    styleMenu.add(windows);
                    styleMenu.add(motif);
                    metal.addActionListener(listen);
                    windows.addActionListener(listen);
                    motif.addActionListener(listen);  break;
                case 4:formatMenu.add(menuItem);
                menuItem.setAction( new StyledEditorKit.UnderlineAction()); //添加加下划线侦听器
                menuItem.setText("下划线(U)");
                break;
            }
        }
        /*设置查看*/
        else if (menulabel.equals("查看")) {                          
                if(num==1)viewMenu.add(menuItem);   //字数统计            
        }
        /*设置帮助*/
        else if (menulabel.equals("帮助")) {
            helpMenu.add(menuItem);
            if (num==0)
                helpMenu.addSeparator(); //帮助主题
        }
    }
    /***************************************************************
     *    Note类的内部类的相关操作
     **************************************************************/
    /** 内部类fileAction,用来处理文件菜单事件 **/
    public class fileAction implements ActionListener {
        //重写actionPerformed方法
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command == "新建(N)"||e.getSource()==newButton) {
                if(change) {
                    newflag=true;
                    exitText();
                    newflag=false;
                } else {
                    frame.dispose();                 
                    new Note();
                }
            } else if (command == "打开(O)..."||e.getSource()==openButton)
            {
               if(change) {
                   openflag=true;
                    exitText();
                    openflag=false;                    
                } else 
                 openFile();
            }
            else if (command == "保存(S)"||e.getSource()==saveButton)
                saveFile();
            else if (command == "另存为(A)...")
                saveAsFile();
            else if (command == "退出(X)"){
                if(change) {                	
                    exitflag=true;
                    exitText();
                    exitflag=false;
                } else   frame.dispose();
            }
        }
    }
    /** 内部类editAction,用来处理编辑菜单事件 **/
    public class editAction implements ActionListener {
        //	重写actionPerformed方法
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command == "撤销(U)"||e.getSource()==undoButton) {                
                if(undomang.canUndo())
                    undomang.undo();               
                redoItem.setEnabled(true);                
            }else if(command=="恢复(R)"||e.getSource()==redoButton){                 
                if (undomang.canRedo())
                    undomang.redo();
                else
                    redoItem.setEnabled(false);                  
            }else if (command == "剪切(T)"||e.getSource()==cutButton)
                display.cut();      //剪切所选的文本到剪贴板里
            else if (command == "复制(C)"||e.getSource()==copyButton)
                display.copy();     //复制所选的文本放到剪贴板里
            else if (command == "粘贴(P)"||e.getSource()==pasteButton)
                display.paste();    //取得剪贴板里的内容粘贴到display里
            else if (command == "删除(L)"||e.getSource()==deleteButton)   //启动删除            
                display.replaceSelection("");                                     
            else if (command == "查找(F)") {           //启动查找
                new FindDialog(frame,"查找",false);
            }else if (command == "查找下一个(N)") {   //启动查找下一个
                if(selectflag)
                    new FindDialog(frame,"查找",false);
                else if (FindDialog.matcher.find(Note.display.getCaretPosition()))    //使用find()方法查找第一个匹配的对象
                    display.select(FindDialog.matcher.start(), FindDialog.matcher.start()+FindDialog.findText.getText().length());
                else
                    JOptionPane.showMessageDialog(null,"找不到 \""+FindDialog.findText.getText()+"\"","文本编辑器",JOptionPane.WARNING_MESSAGE);
            } else if (command == "替换(E)")              //开启替换对话框
                new ChangeDialog(frame,"替换",false);
            else if (command == "全选(A)")            //全选
                display.selectAll();
            else if (command == "时间/日期(D)") {
                Calendar now = Calendar.getInstance();//取得系统的时间
                int year = now.get(Calendar.YEAR);
                int month = now.get(Calendar.MONTH) + 1;
                int date = now.get(Calendar.DATE);
                int hour = now.get(Calendar.HOUR_OF_DAY);
                int minute = now.get(Calendar.MINUTE);
                display.replaceSelection(hour + ":" + minute + " " + year + "-" + month + "-"+ date);
            }
        }
    }    
    /** 内部类formatAction,用来处理格式菜单事件 **/
    public class formatAction implements ActionListener {
        //重写actionPerformed方法
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if (command == "插入图片(W)") {
                JFileChooser open = new JFileChooser(); // 查找文件
                 open.addChoosableFileFilter(new JpgFileFilter());
                 open.addChoosableFileFilter(new GifFileFilter());
                int state=open.showOpenDialog(frame);
                File file=open.getSelectedFile();
                if(file!=null&&state==JFileChooser.APPROVE_OPTION)
                insertIcon(file); // 插入图片                    
            }else if (command == "字体(F)") {
                xp= frame.getX();
                yp = frame.getY();
                FontDialog font=new FontDialog(frame,"字体",true);
                if(!font.sure)
                    return;
                if(font_type==0){bold=false;italic=false;}
                else if(font_type==1){ bold=true;italic=false;}
                else if(font_type==2){ bold=false;italic=true;} 
                else {bold=true;italic=true;}
                if(display.getSelectedText()==null)
                    display.setFont(font.myfont);
                else{
                    createStyle("Style",styledDoc,font_size,bold,italic,display.getSelectedTextColor(),font_name);
                    insertDoc(styledDoc,display.getSelectedText(),"Style");
                }
            }else if (command == "背景颜色(B)") {
                color = JColorChooser.showDialog(container, "请选择背景颜色", color);
                if (color == null)
                    color = display.getBackground();
                display.setBackground(color);
                
            }else if (command == "字体颜色(F)") {
                color = JColorChooser.showDialog(container, "请选择字体颜色", color);
                if (color == null)
                    color =Color.BLACK;
                if(display.getSelectedText()==null)
                    display.setForeground(color);
                else{
                    createStyle("style",styledDoc,font_size,bold,italic,color,font_name);

⌨️ 快捷键说明

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