📄 replace.java
字号:
/*
* Replace.java
*
* Created on 2007年12月10日, 下午6:11
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package WordTest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import WordTest.*;
/**
*
* @author Administrator
*/
public class Replace extends JDialog implements ActionListener{
private JTextField RepText=new JTextField(20);
private JTextField RepTo=new JTextField(20);
private JLabel ShowText=new JLabel("替换内容:");
private JLabel ShowText2=new JLabel("替换为:");
private JPanel p1 = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3=new JPanel();
private JButton ReplaceNext=new JButton("替换下一个");
private JButton ReplaceEnter=new JButton("确定");
// private JButton ReplaceCancel=new JButton("取消");
String str,strnext;
int startp,endp,nexttemp,newstartp,newendp;
/** Creates a new instance of Replace */
public Replace(JFrame fra,String str) {
super(fra,str);
this.setLocation(300,200);
p1.add(ShowText);
p1.add(RepText);
p2.add(ShowText2);
p2.add(RepTo);
p3.setLayout(new FlowLayout());
p3.add(ReplaceNext);
p3.add(ReplaceEnter);
// p3.add(ReplaceCancel);
add("North",p1);
add("Center",p2);
add("South",p3);
setSize(400,200);
this.show();
ReplaceNext.addActionListener(this);
ReplaceEnter.addActionListener(this);
// ReplaceCancel.addActionListener(this);
}
// <editor-fold defaultstate="collapsed" desc=" 替换事件区 ">
private void ReplaceEnterEvent()
{ str = MainFrame.TextArea.getText();
startp = str.indexOf(RepText.getText());
endp = startp + RepText.getText().length();
MainFrame.TextArea.replaceRange(RepTo.getText(),startp,endp);
newendp = endp; ///获取这次替换的终点
}
void ReplaceNextEvent()
{nexttemp = newendp; /////获取上次查找的终点做为未查找字符串的起点
String strall = MainFrame.TextArea.getText();
MainFrame.TextArea.select(nexttemp,strall.length()); /////选中所有未查找的字符串
strnext = MainFrame.TextArea.getSelectedText();
newstartp = strnext.indexOf(RepText.getText()) + nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置
newendp = newstartp + RepText.getText().length();
MainFrame.TextArea.select(newstartp,newendp);
NoFind();
MainFrame.TextArea.replaceRange(RepTo.getText(),newstartp,newendp);//替换字符
}
void NoFind()
{
if(!MainFrame.TextArea.getSelectedText().equals(RepText.getText()))
{
MainFrame.TextArea.setCaretPosition(0); //光标返回文件头部
JOptionPane.showMessageDialog(null,"查找不到对应的字符!","查找结果",JOptionPane.ERROR_MESSAGE);
}
}
// </editor-fold>
public void actionPerformed(ActionEvent e) {
if(e.getSource()== ReplaceNext)
{
ReplaceNextEvent();
}
else if(e.getSource()== ReplaceEnter)
{
ReplaceEnterEvent();
}
// else if(e.getSource()== ReplaceCancel)
// { this.hide();
// this.dispose();
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -