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

📄 replace.java

📁 记事本
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class replace extends searchString implements ActionListener{
	GUI gui;
	JDialog jdg=new JDialog(GUI.jf,"查找");    //对话框
	JTextField jtfsearch=new JTextField();     //被替换文本
	JTextField jtfreplace=new JTextField();    //替换文本
	JButton jbnext=new JButton("查找下一个");   
	JButton jbreplace=new JButton("替换");
	JButton jbreplaceall=new JButton("全部替换");
	JButton jbcancel=new JButton("取消");
	JCheckBox div=new JCheckBox("区分大小写");  //复选框
	//
	public replace(GUI gui)
	{
		this.gui=gui;
		//构造布局
		JPanel p1=new JPanel(new BorderLayout());
		p1.add(new JLabel("查找内容 :"),BorderLayout.WEST);
		p1.add(jtfsearch,BorderLayout.CENTER);
		jtfsearch.setText(searchString.jtf.getText());
		//
		JPanel p2=new JPanel(new BorderLayout());
		p2.add(new JLabel("  替换为   :"),BorderLayout.WEST);
		p2.add(jtfreplace,BorderLayout.CENTER);
		//
		JPanel p10=new JPanel(new GridLayout(3,1));
		p10.add(p1);
		p10.add(p2);
		//
		JPanel p11=new JPanel(new BorderLayout());
		p11.add(p10,BorderLayout.CENTER);
		p11.add(div,BorderLayout.SOUTH);
		//
		JPanel p12=new JPanel(new GridLayout(4,1));
		jbnext.addActionListener(this);
		jbreplace.addActionListener(this);
		jbreplaceall.addActionListener(this);
		jbcancel.addActionListener(this);
		p12.add(jbnext);
		p12.add(jbreplace);
		p12.add(jbreplaceall);
		p12.add(jbcancel);
		//对话框设置
		jdg.setLayout(new BorderLayout());
		jdg.add(p11,BorderLayout.CENTER);
		jdg.add(p12,BorderLayout.EAST);
		jdg.setSize(250, 130);
		jdg.setLocation(GUI.jf.getX()+(GUI.jf.getWidth()-jdg.getWidth())/2, 
				        GUI.jf.getY()+(GUI.jf.getHeight()-jdg.getHeight())/2   );
		jdg.setVisible(true);
	}
	//事件处理
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==jbnext)
		{//按钮 "查找下一个"
			//获取起始查找位置
			int i=GUI.jt.getCaret().getDot()+
				cal10( GUI.jt.getText().substring(0,GUI.jt.getCaret().getDot()) )+1;
			//向后寻找
			findnext(GUI.jt.getText(),i,searchString.jtf.getText(),div.isSelected());
		}
		else if(e.getSource()==jbreplace)
		{//按钮 "替换"
			if(GUI.jt.getSelectionStart()-GUI.jt.getSelectionEnd()!=0)
			{//有选中的字符串
				//替换
				GUI.jt.replaceSelection( jtfreplace.getText() );
			}
			//获取起始查找位置
			int i=GUI.jt.getCaret().getDot()+
				cal10( GUI.jt.getText().substring(0,GUI.jt.getCaret().getDot()) )+1;
			//向后寻找
			findnext(GUI.jt.getText(),i,searchString.jtf.getText(),div.isSelected());
		}
		else if(e.getSource()==jbreplaceall)
		{//按钮 "替换全部"
			GUI.jt.setText(
					GUI.jt.getText().replace(
							this.jtfsearch.getText(), this.jtfreplace.getText())
																					);
		}
		//按钮 "取消"
		else if(e.getSource()==jbcancel)
		{
			jdg.setVisible(false);
			jdg.dispose();
		}
	}
	
}

⌨️ 快捷键说明

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