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

📄 notebook.java

📁 java写的一个编辑工具,类似文本编辑器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        SaveF savefile=new SaveF();                        savefile.actionPerformed(e);                    }                    else                    {                        Updat update=new Updat();                        update.actionPerformed(e);                    }                    timer.stop();                    System.exit(0);                 }                if (result == JOptionPane.NO_OPTION)                {                    timer.stop();                    System.exit(0);                 }                if(result==JOptionPane.CANCEL_OPTION){}            }            else            {                timer.stop();                System.exit(0);             }                    }    }    //复制    class CopyT implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            String selection = jta.getSelectedText();             StringSelection clipString = new StringSelection(selection);             clipbd.setContents(clipString, clipString);         }    }    //剪切    class CutT implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            String selection = jta.getSelectedText();             StringSelection clipString = new StringSelection(selection);             clipbd.setContents(clipString, clipString);             jta.replaceSelection("");                    }    }    //粘贴    class PasteT implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            Transferable clipData = clipbd.getContents(NoteBook.this);             try             {                 String clipString = (String)clipData.getTransferData( DataFlavor.stringFlavor);                 jta.replaceSelection(clipString);                           }             catch(Exception ex)             {                 System.out.println("Exception:"+ex.getMessage());            }         }    }    //删除    class DeleteT implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            String selection = jta.getSelectedText();             jta.replaceSelection("");                    }    }    //查找    class Find implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            try            {                String find=JOptionPane.showInputDialog("查找");                String text=jta.getText();                                int n=0;                int start=text.indexOf(find);                for(int i=0;i<start;i++)                {                     if(text.charAt(i)=='\n')n++;                                    }                jta.setSelectionStart(start-n);                int end=start-n+find.length();                jta.setSelectionEnd(end);                enternum=0;            }            catch(Exception e1)            {                System.err.println("Exception"+e1.getMessage());            }        }    }    //查找下一个     class FindNext implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            try            {                String find=jta.getSelectedText();                int NewIndex=jta.getSelectionStart();//回车为一个字符                String text=jta.getText();                int Nstart=text.indexOf(find,NewIndex+1+enternum);//回车为两个字符                if(Nstart==-1)                {                    JOptionPane.showMessageDialog(null,"文档搜索完毕");                }                else                {                    int n=0;                    for(int i=0;i<=Nstart;i++)                    {                        if(text.charAt(i)=='\n')n++;                    }                    jta.setSelectionStart(Nstart-n);                    int Nend=Nstart-n+find.length();                    jta.setSelectionEnd(Nend);                    enternum=n;                }            }            catch(Exception e1)            {                System.err.println("Exception"+e1.getMessage());            }        }    }    //替换    class Replace implements ActionListener    {        private JDialog jd;        private JLabel label1,label2;        private TextField tf1,tf2;        private JButton b1,b2;        private Container paneC;        public void actionPerformed(ActionEvent e)        {            jd=new JDialog();                        jd.setTitle("替换");            jd.setSize(400,200);            label1=new JLabel("替换内容:  ");            label2=new JLabel("替换为:  ");            tf1=new TextField("",20);            tf2=new TextField("",20);            b1=new JButton("确定");            b1.addActionListener(new Ent());            b2=new JButton("取消");            b2.addActionListener(new Canc());            paneC=jd.getContentPane();            paneC.setLayout(new FlowLayout());            paneC.add(label1);            paneC.add(tf1);            paneC.add(label2);            paneC.add(tf2);            paneC.add(b1);            paneC.add(b2);            jd.pack();            jd.setVisible(true);                   }            //内部替换确定类        class Ent implements ActionListener        {            public void actionPerformed(ActionEvent e)            {               String st1,st2,text;               st1=tf1.getText();               st2=tf2.getText();               text=jta.getText();               int start=text.indexOf(st1);               if(start==-1)               {                   JOptionPane.showMessageDialog(null,"文档搜索完毕");               }               else               {                    int n=0;                    for(int i=0;i<=start;i++)                    {                        if(text.charAt(i)=='\n')n++;                    }                    jta.setSelectionStart(start-n);                    int end=start-n+st1.length();                    jta.setSelectionEnd(end);                    jta.replaceSelection(st2);                                  }            }        }        // 内部替换取消类        class Canc implements ActionListener        {            public void actionPerformed(ActionEvent e)            {                jd.dispose();            }        }    }    //全选    class SelAll implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            jta.selectAll();        }    }    //设置颜色    class SetColor implements ActionListener    {        JColorChooser chooser=new JColorChooser();        JDialog dialog;                 public void actionPerformed(ActionEvent e)        {                                   dialog=JColorChooser.createDialog(null, "颜色选择", false, chooser, new ColorOk(),new ColorCancel());            dialog.setVisible(true);        }        //确定颜色        class ColorOk implements ActionListener        {                        public void actionPerformed(ActionEvent e)            {                                                           StyleConstants.setForeground(normalAttr, chooser.getColor());                jta.setCharacterAttributes(normalAttr,true);                            }        }        //取消颜色        class ColorCancel implements ActionListener        {            public void actionPerformed(ActionEvent e)            {                dialog.dispose();            }        }    }       //设置字体    class SetBold implements ActionListener    {        int flag=0;        public void actionPerformed(ActionEvent e)        {                       if(flag==0)            {                StyleConstants.setBold(normalAttr,true);                jta.setCharacterAttributes(normalAttr,true);                flag=1;            }            else            {                StyleConstants.setBold(normalAttr,false);                jta.setCharacterAttributes(normalAttr,true);                                flag=0;            }                  }    }    //帮助    class Help implements ActionListener    {        public void actionPerformed(ActionEvent e)        {            JOptionPane.showMessageDialog(null,"程序设计大作业");        }    }    //定时更新    class Updat implements ActionListener    {                public void actionPerformed(ActionEvent e)        {              try            {                Writer backUpOut=new FileWriter("C:\\backup.txt");                jta.write(backUpOut);            }            catch(IOException ex)            {                System.out.println("IOException"+ex.getMessage());            }            System.out.println("saveFile="+saveFile);            if(saveFile==null)            {                return;            }            try            {                Writer out=new FileWriter(saveFile);                jta.write(out);            }            catch(Exception ex)            {                System.out.println("Exception:"+ex.getMessage());            }                    }    }          //键盘输入监听  class KeyAction extends KeyAdapter {    private String s=null;    private JTextPane editPane;            private   MutableAttributeSet inputAttributes = new RTFEditorKit().getInputAttributes();    public KeyAction(JTextPane editPane)    {        this.editPane=editPane;        m_context = new StyleContext();        m_doc = new DefaultStyledDocument(m_context);        editPane.setDocument(m_doc);            }    public void keyReleased(KeyEvent kev)    {         changeflag=1;         syntaxParse(s);    }    public void syntaxParse(String data)    {        try         {            Element root = m_doc.getDefaultRootElement();           //取光标当前行            int cursorPos = editPane.getCaretPosition();         //当前光标的位置                                    int line = root.getElementIndex(cursorPos);          //取当前行                       Element para = root.getElement(line);            int start = para.getStartOffset();                       int end = para.getEndOffset() - 1;                   //删除\r字符                      data = m_doc.getText(start, end - start).toLowerCase();            int i = 0;            int xStart = 0;            //分析关键字---            m_doc.setCharacterAttributes(start, data.length(),normalAttr, true);            KeyStringTokenizer st = new KeyStringTokenizer(data);            while( st.hasMoreTokens())            {                data = st.nextToken();                              if ( data == null) return;                for (i = 0; i < keyWord.length; i++ )                {                    if (data.equals(keyWord[i]))                    {                        break;                    }                }                if ( i >= keyWord.length ) continue;                xStart = st.getCurrPosition();                                 //设置关键字显示属性                m_doc.setCharacterAttributes(start+xStart, data.length(),keyAttr,false);             }            inputAttributes.addAttributes(normalAttr);        }        catch (Exception ex)        {            ex.printStackTrace();        }    }}        class KeyStringTokenizer extends StringTokenizer    {        private String oldStr,str;        int m_currPosition = 0,m_beginPosition=0;		public KeyStringTokenizer(String str){        super(str,"\t,\n,\r,, ,;,(,),[,],{,}");        this.oldStr = str;        this.str = str;    }    public String nextToken()    {           try            {               String s = super.nextToken();                                        int pos = -1;                              if (oldStr.equals(s))                {                   return s;               }               pos = str.indexOf(s);               if ( pos == -1)                {                   pos = str.indexOf( s);                   if ( pos == -1)                       return null;                   else pos += 1;               }                              int xBegin = pos + s.length();               str = str.substring(xBegin);               m_currPosition =m_beginPosition+pos;               m_beginPosition = m_beginPosition + xBegin;                return s;           }            catch (java.util.NoSuchElementException ex)           {               ex.printStackTrace();               return null;           }    }    //返回token在字符串中的位置       public int getCurrPosition()        {           return m_currPosition;       }  }  //主函数    public static void main(String args[])    {        JFrame frame=new JFrame();        NoteBook nb=new NoteBook();        frame.setTitle("玩具盒子 记事本");                frame.getContentPane().add(nb,BorderLayout.CENTER);                frame.addWindowListener(new Close());        frame.setSize(450,600);        frame.setVisible(true);            }    }

⌨️ 快捷键说明

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