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

📄 textediter.java

📁 文本编辑器Java语言实现,很好的一个Java源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     }
  if(e.getSource()==quitf)
        {int ms;
        if(issaved==false&&tf.getText().length()!=0)
          {ms=JOptionPane.showConfirmDialog(f,"文件"+file.getAbsolutePath()+"\n已经改变,想保存文件吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);                  
           if(ms==0)
           {
            if(isnewf==true)
           {FileDialog fdag=new FileDialog(f,"保存",1);
   	        fdag.setSize(300,250);
   	        fdag.setVisible(true);
   	        if(fdag.getFile()!=null)
   	        {
   	         String st=fdag.getDirectory()+fdag.getFile();
             try
             { file=new File(st);
               FileOutputStream fout=new FileOutputStream(file);
               DataOutputStream out=new DataOutputStream(fout);
               out.writeBytes(tf.getText());//tf.setText("");f.setTitle("无标题 - 记事本");
               JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getAbsolutePath(),"保存信息",JOptionPane.PLAIN_MESSAGE);
               f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
               isnewf=false;issaved=true;
             }
             catch(Exception E)
             {System.out.println("I/O错误!");}
            }	
           }
   	       else
   	         {try
   	          {FileOutputStream fout=new FileOutputStream(file);
               DataOutputStream out=new DataOutputStream(fout);
               out.writeBytes(tf.getText());
               out.close();
               JOptionPane.showMessageDialog(f,"文件"+file.getAbsolutePath()+"\n已成功保存!","保存信息",JOptionPane.PLAIN_MESSAGE);
               isnewf=false;issaved=true;
              }
              catch(Exception E)
              {System.out.println("I/O错误!");}
             }
           }
            System.exit(0);
           }
        }
 
  if(e.getSource()==autorc)
          {if(tf.getLineWrap()==true){tf.setLineWrap(false);autorc.setLabel("   自动换行(W)");}
                else {tf.setLineWrap(true);autorc.setLabel("√自动换行(W)");}
          }
   if(e.getSource()==cancel){buffer=new String(tf.getText());
                              if(iskey==true)
                              {tf.setText(buffer.substring(0,buffer.length()-1));
                               iskey=false;cace=1;}
                              if(cace==1){tf.setText(temptext);cace=0;}
                            }
  if(e.getSource()==cut){temptext=new String(tf.getText());tf.cut();issaved=false;iskey=false;cace=1;}//paste.setEnabled(true);
  if(e.getSource()==copy){tf.copy();iskey=false;cace=1;}//paste.setEnabled(true);
  if(e.getSource()==paste){temptext=new String(tf.getText());tf.paste();issaved=false;iskey=false;cace=1;}//paste.setEnabled(false);
  if(e.getSource()==del){temptext=new String(tf.getText());tf.replaceSelection("");issaved=false;iskey=false;cace=1;}
  if(e.getSource()==search)
           {search_replace();
            lb2.setEnabled(false);
            btn4.setEnabled(false);
            fd2.setEnabled(false);
            btn5.setEnabled(false);
            
           }
  if(e.getSource()==replace){search_replace();}
  if(e.getSource()==btn3)
         {
          if(isbtn3==0)serp=tf.getText().substring(0,tf.getText().length());
           else serp=tf.getText().substring(tf.getSelectionStart(),tf.getText().length());
          int loca=serp.indexOf(fd1.getText(),0);
            if(loca!=-1)
             {tf.select(loca,loca+fd1.getText().length());isbtn3=1;
             }
             else JOptionPane.showMessageDialog(f,"找不到 '"+fd1.getText()+"'","记事本",JOptionPane.WARNING_MESSAGE);
          
          }
  if(e.getSource()==btn4){tf.replaceSelection(fd2.getText());issaved=false;}
  if(e.getSource()==btn5){}
  if(e.getSource()==btn6){dialog.setVisible(false);}
  if(e.getSource()==selectall){tf.selectAll();iskey=false;}
  if(e.getSource()==tmda)
       {Date dt=new Date();
        String hour=String.valueOf(dt.getHours());
        String minute=String.valueOf(dt.getMinutes());
        String year=String.valueOf(dt.getYear()+1900);
        String month=String.valueOf(dt.getMonth()+1);
        String date=String.valueOf(dt.getDate());
        switch(dt.getDay())
        {case 0:tf.insert("星期日 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
         case 1:tf.insert("星期一 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
         case 2:tf.insert("星期二 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
         case 3:tf.insert("星期三 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
         case 4:tf.insert("星期四 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
         case 5:tf.insert("星期五 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
         case 6:tf.insert("星期六 "+hour+":"+minute+" "+year+"-"+month+"-"+date,tf.getCaretPosition());break;
        }
        //tf.setText(hour+":"+minute+" "+year+"-"+month+"-"+date);
        issaved=false;
       }
  if(e.getSource()==gesi){setfont();  }
  if(e.getSource()==btn1)
   {//
    cbxstr1=(String)cb1.getSelectedItem();//tf.setText(cbxstr1);
    cbxint3=Integer.parseInt((String)cb3.getSelectedItem());
   if(((String)cb2.getSelectedItem()).compareTo("正常体")==0)	tf.setFont(new Font(cbxstr1,Font.PLAIN,cbxint3));
   if(((String)cb2.getSelectedItem()).compareTo("粗体")==0)	tf.setFont(new Font(cbxstr1,Font.BOLD,cbxint3));
   if(((String)cb2.getSelectedItem()).compareTo("斜体")==0)	tf.setFont(new Font(cbxstr1,Font.ITALIC,cbxint3));
   //tf.setText(String.valueOf(cbxint3));
  	//tf.setFont(cbxstr1,cbxint2,cbxint3);
  	issaved=false;
  	dialog.show(false);
  	}
   if(e.getSource()==btn2){dialog.show(false);}
   if(e.getSource()==cancel1){buffer=new String(tf.getText());
                              if(iskey==true)
                              {tf.setText(buffer.substring(0,tf.getText().length()-1));
                               
                               iskey=false;cace=1;}
                              if(cace==1){tf.setText(temptext);cace=0;}
                            }
   if(e.getSource()==copy1){tf.copy();iskey=false;cace=1;}
   if(e.getSource()==cut1){temptext=new String(tf.getText());tf.cut();issaved=false;iskey=false;cace=1;}
   if(e.getSource()==paste1){temptext=new String(tf.getText());tf.paste();issaved=false;iskey=false;cace=1;}
   if(e.getSource()==del1){temptext=new String(tf.getText());tf.replaceSelection("");issaved=false;iskey=false;cace=1;}
   if(e.getSource()==selall){tf.selectAll();iskey=false;}
   
 }
 
 class WinLis extends WindowAdapter
 {public void windowClosing(WindowEvent e)
  {int ms;
   if(tf.getText().length()!=0&&issaved==false)
   {ms=JOptionPane.showConfirmDialog(f,"文件"+file.getAbsolutePath()+"\n已经改变,想保存文件吗?","记事本",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);                  
    if(ms==0)
    {if(isnewf==true)
     {FileDialog fdag=new FileDialog(f,"保存",1);
   	  fdag.setSize(300,250);
   	  fdag.setVisible(true);
   	  if(fdag.getFile()!=null)
   	  {String st=fdag.getDirectory()+fdag.getFile();
       try
       { file=new File(st);
        FileOutputStream fout=new FileOutputStream(file);
        DataOutputStream out=new DataOutputStream(fout);
        out.writeBytes(tf.getText());//tf.setText("");f.setTitle("无标题 - 记事本");
        JOptionPane.showMessageDialog(f,"文件已成功保存在:\n"+file.getAbsolutePath(),"保存信息",JOptionPane.PLAIN_MESSAGE);
        f.setTitle(file.getName().substring(0,file.getName().indexOf('.'))+" - 记事本");
        isnewf=false;issaved=true;
       }
       catch(Exception E)
       {System.out.println("I/O错误!");}
      }	
     }
   	 else
   	  {try
   	   {FileOutputStream fout=new FileOutputStream(file);
        DataOutputStream out=new DataOutputStream(fout);
        out.writeBytes(tf.getText());
        out.close();
        JOptionPane.showMessageDialog(f,"文件"+file.getAbsolutePath()+"\n已成功保存!","保存信息",JOptionPane.PLAIN_MESSAGE);
        isnewf=false;issaved=true;
       }
       catch(Exception E)
       {System.out.println("I/O错误!");}
      }System.exit(0);
     }
     if(ms==-1||ms==2){}
        
   }
  }
 }
class MyMouseAdapter extends MouseAdapter
{public void mousePressed(MouseEvent e)
 {if((e.getModifiers() & InputEvent.BUTTON3_MASK)==InputEvent.BUTTON3_MASK)
    pmenu.show(tf,e.getX(),e.getY());
    
 }
}
class MyKeyAdapter extends KeyAdapter
{public void keyPressed(KeyEvent e)
 {issaved=false;
  if((e.getModifiers() & KeyEvent.VK_BACK_SPACE)!=KeyEvent.VK_BACK_SPACE)
  temptext=tf.getText();//.substring(0,tf.getText().length()-1);
  iskey=true;//cace=1;
 }
} 
 public static void main(String arg[])
 {new Textediter();
  
  //f.setTitle("无标题 - 记事本");
 }
  public void setfont()
 { dialog=new JDialog(f,"字体设置",true);
  //dialog=new JFrame("字体设置");
  diacp=dialog.getContentPane();
  diacp.setLayout(new FlowLayout());
  jp=new JPanel(new GridLayout(4,2,4,4));
  lb1=new JLabel("字体");
  lb2=new JLabel("字形");
  lb3=new JLabel("大小");
  String s1[]={"宋体","黑体","楷体","隶书","华文行楷","Arial","Courier"};
  cb1=new JComboBox(s1);
  String s2[]={"正常体","粗体","斜体"};
  cb2=new JComboBox(s2);
  //int s3[]={10,15,20,25,30,35,40,45,50};
 String s3[]={"10","15","20","25","30","35","40","45","50","60"};
  //int s3[]={Font.PLAIN,Font.BOLD,Font.ITALIC};
  cb3=new JComboBox(s3);
  btn1=new JButton("确定");
  btn2=new JButton("取消");
  jp.add(lb1);jp.add(cb1);
  jp.add(lb2);jp.add(cb2);
  jp.add(lb3);jp.add(cb3);
  btn1.addActionListener(this);
  jp.add(btn1);
  btn2.addActionListener(this);
  jp.add(btn2);
  diacp.add(jp);
  Font ft=tf.getFont();
  cb1.setSelectedItem(ft.getName());
  if(ft.getStyle()==0)cb2.setSelectedItem("正常体");
  if(ft.getStyle()==1)cb2.setSelectedItem("粗体");
  if(ft.getStyle()==2)cb2.setSelectedItem("斜体");
  cb3.setSelectedItem(String.valueOf(ft.getSize()));
  //dialog.add(diacp);
  dialog.setBounds(250,250,250,180);
  dialog.setResizable(false);
  dialog.setVisible(true);
 }
 public void search_replace()
 {dialog=new JDialog(f,"查找/替换",true);
  diacp=dialog.getContentPane();
  diacp.setLayout(new FlowLayout());
  jp=new JPanel(new GridLayout(4,3,4,4));
  lb1=new JLabel("查找内容:");
  lb2=new JLabel("替换为:");
  btn3=new JButton("查找下一个");
  btn4=new JButton("替换");
  btn5=new JButton("全部替换");
  btn6=new JButton("取消");
  fd1.setSize(80,25);
  fd2.setSize(80,25);
  jp.add(lb1);
  jp.add(fd1);
  jp.add(btn3);btn3.addActionListener(this);
  jp.add(lb2);
  jp.add(fd2);
  jp.add(btn4);btn4.addActionListener(this);
  jp.add(new JLabel());
   jp.add(new JLabel());
  jp.add(btn5);btn5.addActionListener(this);
   jp.add(new JLabel());
    jp.add(new JLabel());
  jp.add(btn6);btn6.addActionListener(this);
  diacp.add(jp);
  dialog.setBounds(250,250,330,180);
  dialog.setResizable(false);
  dialog.setVisible(true);
 }
}
/*class filter1 extends java.io.FilenameFilter
{public boolean accept(File file)
 {String extension="";
  if(file.getPath().lastIndexOf('.')>0)
   extension=file.getPath().substring(file.getPath().lastIndexOf('.')+1);
   extension=extension.toLowerCase();
  if(extension!="")
   return extension.equals("txt");
  else return file.isDirectory();
 }
 public String getDescription()
 {return "*.txt";
 }
} */

⌨️ 快捷键说明

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