📄 replace.java
字号:
jTextField_Find.requestFocus(true);
}
else
{
Note.fileArea.select(str_find_index,str_find_index+str_find_long); //将查找到的字符反选
str_down_index=str_find_index+str_find_long; //改变查找的初始位置
}
} else /***不区分大小写查找****说明:先将文本区中的文本和要查找的字符串转换成同一小写字符串,再进行查找操作****/
{
/*将文本区中的字母文本转换为小写字母文本—— 开始*/
char a[]=str_fileArea.toCharArray();
for(int i=0;i<a.length;i++)
{
if(Character.isUpperCase(a[i]))
{
a[i]=Character.toLowerCase(a[i]);
}
}
str_fileArea=new String(a);
/*将文本区中的字母文本转换为小写字母文本—— 结束*/
/*将查找字符串中的字母文本转换为小写字母文本—— 开始*/
char b[]=str_find.toCharArray();
for(int j=0;j<b.length;j++)
{
if(Character.isUpperCase(b[j]))
{
b[j]=Character.toLowerCase(b[j]);
}
}
str_find=new String(b);
/*将查找字符串中的字母文本转换为小写字母文本—— 结束*/
str_down_index=Note.fileArea.getCaretPosition();
str_find_index=str_fileArea.indexOf(str_find,str_down_index); //查找字符串出现的位置,如果没找到则返回-1
if(str_find_index==-1)
{
JOptionPane.showMessageDialog(null,"找不到“ "+str_find+" ”(替换完毕)","记事本",JOptionPane.QUESTION_MESSAGE);
jTextField_Find.selectAll();
jTextField_Find.requestFocus(true);
}
else
{
Note.fileArea.select(str_find_index,str_find_index+str_find_long); //将查找到的字符反选
str_down_index=str_find_index+str_find_long; //改变查找的初始位置
}
}
if(info ==1 &&str_find_index != -1){
StringBuffer strtemp = strbuf_fileArea.delete(str_find_index,str_find_index+str_find_long);
strtemp.insert(str_find_index,str_replace);
Note.fileArea.setText(null);
Note.fileArea.setText(strtemp.toString());
Note.fileArea.setSelectionStart(str_find_index+str_find_long);
Note.fileArea.setSelectionEnd(str_find_index+str_find_long);
}
// System.out.println(strbuf_fileArea.toString());
}
private void jTextField_FindActionPerformed(ActionEvent evt) {
}
private void jButton_Replace_CancelActionPerformed(ActionEvent evt) {
dispose();
}
public void jCheckBox_CapitalActionPerformed(ActionEvent evt) {
}
private void jButton_FindNextActionPerformed(ActionEvent evt) {
find(0);
}
private void jButton_ReplaceActionPerformed(ActionEvent evt) {
find(1);
}
/**这个方法实现的是全部替换 同样采用了 先查找后替换的方式
* 实际上是一个 find 方法的拓展
* 采用的是 在全部替换是先将光标定位在文本内容的首位置
* 依次每个替换 循环至替换完毕.方法比较拙劣
* **/
private void replaceAll(){
String str_find = jTextField_Find.getText().trim();//从查找框中获得内容
int str_find_long = str_find.length();//计算文本内容的长度 ;
String str_fileArea = Note.fileArea.getText();
String str_up_find; //设置为向上查找的字符串
//向下替换的初始值
String str_replace = jTextField_Replace.getText().trim();
int str_replace_long = str_replace.length();
StringBuffer strbuf_fileArea = new StringBuffer(Note.fileArea.getText());
int str_find_index ;
int str_down_index = 0;
boolean temp = true;
while(true){
if(jCheckBox_Capital.isSelected()) { //是否区分大小写. 是就执行
// str_down_index=Note.fileArea.getCaretPosition();
str_find_index=str_fileArea.indexOf(str_find,str_down_index); //查找字符串出现的位置,如果没找到则返回-1
if(str_find_index==-1)
{ temp = false;
JOptionPane.showMessageDialog(null,"找不到“ "+str_find+" ”(替换完毕)","记事本",JOptionPane.QUESTION_MESSAGE);
jTextField_Find.selectAll();
jTextField_Find.requestFocus(true);
break;
}
else
{
Note.fileArea.select(str_find_index,str_find_index+str_find_long); //将查找到的字符反选
str_down_index=str_find_index+str_find_long; //改变查找的初始位置
}
} else /***不区分大小写查找****说明:先将文本区中的文本和要查找的字符串转换成同一小写字符串,再进行查找操作****/
{
/*将文本区中的字母文本转换为小写字母文本—— 开始*/
char a[]=str_fileArea.toCharArray();
for(int i=0;i<a.length;i++)
{
if(Character.isUpperCase(a[i]))
{
a[i]=Character.toLowerCase(a[i]);
}
}
str_fileArea=new String(a);
/*将文本区中的字母文本转换为小写字母文本—— 结束*/
/*将查找字符串中的字母文本转换为小写字母文本—— 开始*/
char b[]=str_find.toCharArray();
for(int j=0;j<b.length;j++)
{
if(Character.isUpperCase(b[j]))
{
b[j]=Character.toLowerCase(b[j]);
}
}
str_find=new String(b);
/*将查找字符串中的字母文本转换为小写字母文本—— 结束*/
str_down_index=Note.fileArea.getCaretPosition();
str_find_index=str_fileArea.indexOf(str_find,str_down_index); //查找字符串出现的位置,如果没找到则返回-1
if(str_find_index==-1)
{
temp = false;
JOptionPane.showMessageDialog(null,"找不到“ "+str_find+" ”","记事本",JOptionPane.QUESTION_MESSAGE);
jTextField_Find.selectAll();
jTextField_Find.requestFocus(true);
break;
}
else
{
Note.fileArea.select(str_find_index,str_find_index+str_find_long); //将查找到的字符反选
str_down_index=str_find_index+str_find_long; //改变查找的初始位置
}
}
if(str_find_index != -1){
StringBuffer strtemp = strbuf_fileArea.delete(str_find_index,str_find_index+str_find_long);
strtemp.insert(str_find_index,str_replace);
Note.fileArea.setText(null);
Note.fileArea.setText(strtemp.toString());
Note.fileArea.setSelectionStart(str_find_index+str_find_long);
Note.fileArea.setSelectionEnd(str_find_index+str_find_long);
}
}
}
private void jButton_ReplaceAllActionPerformed(java.awt.event.ActionEvent evt) {
Note.fileArea.setCaretPosition(0);
replaceAll();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Replace().setVisible(true);
}
});
}
private JButton jButton_Replace;
private JButton jButton_FindNext;
private JButton jButton_ReplaceAll;
private JButton jButton_Replace_Cancel;
private JCheckBox jCheckBox_Capital;
private JLabel jLabel_Replace_findText;
private JLabel jLabel_Replace_replaceText;
private JTextField jTextField_Find;
private JTextField jTextField_Replace;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -