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

📄 replaceframe.java

📁 用java编写的一个记事本程序,具有和windows Notepad相似的功能
💻 JAVA
字号:
import java.awt.datatransfer.*;
import java.io.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
class ReplaceFrame extends Frame implements ActionListener,ItemListener
{

	Panel r,r_1,r_2,v_1,v_2,r_3;
	Button findButton,cancel,replace,replaceall;
	Box baseBox,H1,H2,V1,H3,V2;
	Label content,replaceas;
	TextField text;
	TextField replacetext;
	Checkbox type,up,down;

	Image im;
	static int end=0;
	static int tempstart=0;
	static int start=0;
	String tempstring="";
	static int flagsend=0;
	ReplaceFrame()
	{
	   super("替换对话框");
	   Color c=new Color(236,233,216);
	   this.setBackground(c);
	   im=Toolkit.getDefaultToolkit().getImage("d:\\1.1.jpg");
	   this.setIconImage(im);
       H1=Box.createHorizontalBox();
       content=new Label("查找内容");
       text=new TextField(11);
       H1.add(content);
       H1.add(Box.createHorizontalStrut(1));
       H1.add(text);
      
       replacetext=new TextField(10);
       replaceas=new Label("替换为 ");
       H2=Box.createHorizontalBox();
       H2.add(replaceas);
       H2.add(Box.createHorizontalStrut(1));
       H2.add(replacetext);
       
        H3=Box.createHorizontalBox();
       findButton=new Button("查找下一个");
       replace=new Button("替换");
       replace.addActionListener(this);
       replaceall=new Button("替换所有");
       replaceall.addActionListener(this);
       cancel=new Button("取消");
       findButton.addActionListener(this);
       cancel.addActionListener(this);

       r_3=new Panel();
       r_3.setLayout(new GridLayout(4,1));
       r_3.add(findButton);
       r_3.add(replace);
       r_3.add(replaceall);
       r_3.add(cancel);
       
       V1=Box.createVerticalBox();
       V1.add(H1);
       V1.add(H2);
       type=new Checkbox("区分大小写");
       type.addItemListener(this);
       V1.add(type);
       
       r=new Panel();
       r.setLayout(new BorderLayout());
       r.add(V1,BorderLayout.CENTER);
       r.add(r_3,BorderLayout.EAST);
       

       add(r,BorderLayout.CENTER);

 
       this.setVisible(true);
       this.setBounds(300,300,330,120);
       validate();
 
	   addWindowListener(new WindowAdapter()
 	  {
 		public void windowClosing(WindowEvent e)
 		{
 			dispose();
 		}
 	});
		
	}
	
public void actionPerformed(ActionEvent e1)
{
 String s=text.getText();
 String string=MyFrame.text.getText();
	if(e1.getSource()==findButton)
	{
		System.out.println("运算前的start:"+start);
	    System.out.println("运算前的end:"+end);  
		if(text.getText()!=null  )
		{
              if(end<=string.length()-1)
            {
            if(type.getState()==true)
            {
			 start=tempstring.indexOf(s,tempstart);
			}
			else
			{
			 start=string.indexOf(s,tempstart);
			}
			 end=start+s.length();
			 System.out.println("运算后的start:"+start);
			 System.out.println("运算后的end:"+end);  
			MyFrame.text.setSelectionStart(start);
			MyFrame.text.setSelectionEnd(end);
			if((end)<=string.length()-1)
			{
			
			tempstart=end;
			}


          System.out.println("运算后的start:"+start);
			 System.out.println("tempstart:"+tempstart); 
			}    

		 
		}

		
	}
	if(e1.getSource()==replace)
	{
		if(text.getText()!=null && replacetext.getText()!=null)
		{
             if(end<string.length())
             {
           
				int start=string.indexOf(s,tempstart);
			 end=start+s.length();
			 System.out.println("运算前的start:"+start);
			 System.out.println("运算前的end:"+end);   
			MyFrame.text.setSelectionStart(start);
			MyFrame.text.setSelectionEnd(end);
			MyFrame.text.replaceRange(replacetext.getText(),start,end);
			if(end<=string.length()-1)
			{
			
			tempstart=end;
			}
			if(end>string.length()-1)
			{
			 tempstart=end;
			}

			System.out.println("运算后的start:"+start);
		    }

		}

		 		
	}

	if(e1.getSource()==replaceall)
	{
		try
		{
	
		if(text.getText()!=null && replacetext.getText()!=null)
		{

             while(end<string.length())
             {
            
			int start=string.indexOf(s,tempstart);
			 end=start+s.length();
			    
			MyFrame.text.setSelectionStart(start);
			MyFrame.text.setSelectionEnd(end);
			MyFrame.text.replaceRange(replacetext.getText(),start,end);
			if((end)<=string.length()-1)
			{
			
			tempstart=end;
			}

		    }
		 if(end>string.length()-1)
		 {
		 	tempstart=0;
		 	end=0;
		 }
		 flagsend=1;
		}
       	}
       	catch(Exception e)
       	{
       		
       	}		
	}	
	if(e1.getSource()==cancel)
	{
		dispose();
	}
}	
public void itemStateChanged(ItemEvent ie)
{
	  String s=text.getText();
      String string=MyFrame.text.getText();
      String ups=s.toUpperCase();
      String lows=s.toLowerCase();   
	if(ie.getItemSelectable()==type)
	{
       if(s.equals(ups))
       {
       	 tempstring=string.toUpperCase();
       }
       if(s.equals(lows))
       {
       	 tempstring=string.toLowerCase();
       }
		
	}
}
}
	

⌨️ 快捷键说明

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