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

📄 javaeditor.java

📁 java程序设计源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.net.URL;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.lang.*;
import javax.swing.text.*;
import javax.swing.table.*;
public class javaedit extends JFrame 
{   //class 声明
	public class KeyLis extends KeyAdapter
	{public void keyPressed(KeyEvent e)
		{if(e.getKeyCode()==e.VK_F12)
                   (new Act_ji1()).actionPerformed(e_find);     		
		else if(e.isControlDown()&&e.getKeyCode()==e.VK_S)
		 (new Act_ji2()).actionPerformed(e_find);
                else if(e.isAltDown())
                (new Act_ji4()).actionPerformed(e_find);
                 else if(e.getKeyCode()==e.VK_F3)
                 (new Act_ji4_next()).actionPerformed(e_find);   
                  else if(e.getKeyCode()==e.VK_F4)
                 (new Act_ji5()).actionPerformed(e_find);   
              else if(e.isControlDown()&&e.getKeyCode()==e.VK_N)
                 (new Act_ji0()).actionPerformed(e_find);
                 else if(e.isControlDown()&&e.getKeyCode()==e.VK_E)
                 (new Act_ji3()).actionPerformed(e_find);
             else if(e.getKeyCode()==e.VK_F5)
             (new Act_ji7()).actionPerformed(e_find);
              else if(e.getKeyCode()==e.VK_F9)
              (new Act_ji6()).actionPerformed(e_find);
                 else if(e.isControlDown()&&e.getKeyCode()==e.VK_H)
                 (new Act_ji8()).actionPerformed(e_find);
           }
	 }
		
         public class WindowLis extends WindowAdapter
         {public void windowClosing(WindowEvent e)
          {JOptionPane.showMessageDialog(null,"Exit from xuhao's JavaEdit to Windows");
             dispose();
             System.exit(0);
          }
        }

	   class chooseFile extends JFileChooser{
            public chooseFile(){
	     super("/java");
             }
             public void approveSelection()
          {  if(file_control==0)
	      { 
	      	filename=Jfc.getSelectedFile().getName();
              directory[ja_control]=Jfc.getCurrentDirectory().toString();
             ja[ja_control].setText(null);	   
	   try{
	      BufferedReader breader=new BufferedReader(new FileReader(directory[ja_control]+"/"+filename));
	      while(true){
	      str=breader.readLine();
	      if(str==null)
	      break;
           ja[ja_control].append(str+'\n');
              }
             }     
           catch(Exception e_open)
            {
	   JOptionPane.showMessageDialog(Jf.getContentPane(),"读取发生错误");
             }
               }
	   else if(file_control==1)
	    {
	      filename=Jfc.getSelectedFile().getName();
             directory[ja_control]=Jfc.getCurrentDirectory().toString();
              try{
              fwrite=new FileWriter(directory[ja_control]+"/"+filename);
              fwrite.write(ja[ja_control].getText());
              fwrite.close();
                  }
             catch(Exception e_save)
            {
	     JOptionPane.showMessageDialog(Jf.getContentPane(),"读取发生错误");
             }
       
           }
                Jf.dispose();
                root.remove(node[ja_control]);
                node[ja_control]=new DefaultMutableTreeNode(filename);
                root.add(node[ja_control]);
	    	remove(tree);
	    	tree=new JTree(root);
	    	tree.setBackground(new Color(70,80,91));
	    	jsp_line.setRightComponent(tree);
	    	jsp_line.setDividerLocation(120);
	    	tab.setTitleAt(ja_control,filename);
	    	 logic[ja_control]=false;
                  }
          public void cancelSelection()
          {logic[ja_control]=true;
          	 Jf.dispose();
          }
         }
        class Filter extends FileFilter
           {public boolean accept(File file1)
          {
	  return(file1.getName().endsWith(".java")||file1.isDirectory()||file1.getName().endsWith(".html")||file1.getName().endsWith(".txt")||file1.getName().endsWith(".cpp"));
           }
          public String getDescription()
             {
	   return(".java,*.html,*.txt,*.cpp");
           }
           }
	   
	   class CaretLis_line implements CaretListener
	   {   public void caretUpdate(CaretEvent e) {
	   	
	   	try{
	   	line.setText("          Cursor at the "+(ja[ja_control].getLineOfOffset(ja[ja_control].getCaretPosition())+1)+" line in the file of "+tab.getTitleAt(ja_control));
	}
	      catch(BadLocationException eB)
	      { System.out.println("Io Wrong");
	      }      	
	   	}
	   }
	class CaretLis_err extends MouseAdapter
	   {   
             public void mouseClicked(MouseEvent e) 
            {  int off_err=err_ja.getCaretPosition();
	     int i=0;
            try{
             int index=-1;      
             int line_err=err_ja.getLineOfOffset(off_err-1);
             int start_err=err_ja.getLineStartOffset(line_err);
             String err_str=err_ja.getText(start_err,off_err-start_err);
             err_ja.select(start_err,off_err-1);
              for(i=0;i<1000;i++)
              { index=err_str.indexOf(":"+i+":");
               if(index>0)
                break;
              }
             if(index!=-1)
             {ja[ja_control].requestFocus();
              ja[ja_control].select(ja[ja_control].getLineStartOffset(i-1),ja[ja_control].getLineEndOffset(i-1));
             }
             }    
                                   

            catch(BadLocationException eB)
	      { System.out.println("Io Wrong");
	       }      	
               }
              }        
                
	   
	   class Changetab implements ChangeListener
	   {
	   	public void stateChanged(ChangeEvent e)
	   	{
                 ja_control=tab.getSelectedIndex();
                }
           }
             class Act_ji0 implements ActionListener
	    {public void actionPerformed(ActionEvent e_ji0)
	    {   root.add(node[tb]);
	    	remove(tree);
	    	tree=new JTree(root);
	    	tree.setBackground(new Color(70,80,91));  
                jsp_line.setRightComponent(tree);
	    	jsp_line.setDividerLocation(120);
	    	tab.addTab("File"+(tb+1),js[tb]);
	    	tab.setSelectedIndex(tb);
                 tb++;
            }
            }
         class Act_ji1 implements ActionListener
         {public void actionPerformed(ActionEvent e_ji1)
         {logic[ja_control]=false;
          file_control=0;
          Jfc.setApproveButtonText("OPEN");
          Jfc.addChoosableFileFilter(filter);
          Jf.getContentPane().add(Jfc);
          Jf.setSize(550,350);
          Jf.setTitle("Please Choose The File Your Want To Open!"); 
          Jf.setVisible(true);
          ja[ja_control].setCaretPosition(0);
          
          }
        }
        class Act_ji2 implements ActionListener
         {public void actionPerformed(ActionEvent e_ji2)
         { if(logic[ja_control])
         {	file_control=1;
         Jfc.setApproveButtonText("SAVE");
         Jfc.addChoosableFileFilter(filter);
          Jf.getContentPane().add(Jfc);
          Jf.setTitle("Please Input Your File's Name!"); 
          Jf.setSize(550,350);
          Jf.setVisible(true);
        
        
        }
        else
          {try{
              fwrite=new FileWriter(directory[ja_control]+"/"+tab.getTitleAt(ja_control));
              fwrite.write(ja[ja_control].getText());
              fwrite.close();
                  }
             catch(Exception e_save)
            {
	     JOptionPane.showMessageDialog(null,"读取发生错误");
             }
          }
                    
          }
        }
          class Act_ji3 implements ActionListener
         {public void actionPerformed(ActionEvent e_ji3)
         {         JOptionPane.showMessageDialog(null,"Exit from xuhao's JavaEdit to Windows");
              dispose();
             System.exit(0);
         
         
                }
        }
           class Act_ji4 implements ActionListener
       {public void actionPerformed(ActionEvent e_ji4)
          {    
          	find_word=JOptionPane.showInputDialog("Input the word your want to find!");
              if(find_word==null)
                    JOptionPane.showMessageDialog(null,"Can't find the word");
              else
              {  f_length=find_word.length(); 
               ja_text=ja[ja_control].getText();
               index=ja_text.indexOf(find_word);
               if(index<0)
               JOptionPane.showMessageDialog(null,"   The word is not in the file  ");
               else
               {ja[ja_control].requestFocus();
               ja[ja_control].select(index,index+f_length);}
            }
           }
        }
                   class Act_ji4_next implements ActionListener
       {
       	public void actionPerformed(ActionEvent e_ji4_next)
         { ja_text=ja[ja_control].getText();
              index=ja_text.indexOf(find_word,index+1);
           if(index<0)
               JOptionPane.showMessageDialog(null," Seeking has reached the end of the file ");
             else
              ja[ja_control].select(index,index+f_length);
         	 
           }
          }
          class Act_ji5 implements ActionListener
       {
       	public void actionPerformed(ActionEvent e_ji5)
        {   Object[] endButton1={"Replace","Cancel"};
             String message1="Are you sure to replace the word";
             ja_text=ja[ja_control].getText();
             String seek=JOptionPane.showInputDialog("Please input the word you want to seek");
             String replace=JOptionPane.showInputDialog("Please input the word you want to replace");
             if(seek!=null)
               { rp_length=seek.length(); 
                while(true){
                ja_text=ja[ja_control].getText();
                index=ja_text.indexOf(seek,index+rp_length);
                if(index<0)
                  {JOptionPane.showMessageDialog(null,"Seeking has reached the end of the file ");
                  break;}
                 else
              { ja[ja_control].requestFocus();
                ja[ja_control].select(index,index+rp_length);
                 JOptionPane end1=new JOptionPane(message1,JOptionPane.WARNING_MESSAGE,JOptionPane.DEFAULT_OPTION,null,endButton1);
                 JDialog endD1=end1.createDialog(end1,"请选择");
                 endD1.setVisible(true);
                 Object push1=end1.getValue();
                 if(push1==endButton1[0])
                  ja[ja_control].replaceSelection(replace);
              }          
               }
              }
               }     }                               


         class Act_ji6 implements ActionListener
       {
       	public void actionPerformed(ActionEvent e_ji6)
             {err_ja.setText(null);
           try
        {  
           int count;
            byte input[]=new byte[256];
            String InputString;
            String[] command = {"javac",directory[ja_control]+"/"+tab.getTitleAt(ja_control)};
            Process p = Runtime.getRuntime().exec(command);
            BufferedInputStream bufin=new BufferedInputStream(p.getErrorStream());
            bufin.mark(256);
            count=bufin.read(input);
             if(count<=0)
             err_ja.append("Compile to "+tab.getTitleAt(ja_control)+" Success");
           else 
            {InputString=new String(input,0,count); 
            err_ja.append("Compile to "+tab.getTitleAt(ja_control)+" Fail\n"+InputString);}
                }
        catch (IOException e)
        {
            System.err.println("IO error: " + e);
        }         
           
        }
        } 
        class Act_ji7 implements ActionListener
        {
        public void actionPerformed(ActionEvent e_ji7)
             {
             	err_ja.setText(null);
            if((tab.getTitleAt(ja_control)).indexOf(".java")>-1)
             {
               try
           {        int count;
            byte input[]=new byte[256];
            String InputString;
            String class_name;
            int length=(tab.getTitleAt(ja_control)).length();
            class_name=(tab.getTitleAt(ja_control)).substring(0,length-5);
             String[] command = {"java","-classpath",directory[ja_control],class_name};
            Process p = Runtime.getRuntime().exec(command);
            BufferedInputStream bufin=new BufferedInputStream(p.getErrorStream());

⌨️ 快捷键说明

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