<< 返回
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Replace extends JDialog {
private Replace myself = null;
private Notepad notepad = null;
private JTextField textFrom = null;
private JLabel labelFrom = null;
private JButton findNext = null;
private JTextField textTo = null;
private JLabel labelTo = null;
private JButton cancel = null;
private JCheckBox care = null;
private JPanel panel_Care = null;
private JButton replace = null;
private JButton replaceAll = null;
private boolean isCare = false;
public Replace(Notepad notepad) {
this.myself = this;
this.notepad = notepad;
this.setLayout(null);
this.getReady();
this.addEvent();
this.fillText();
this.setVisible(true);
}
private void getReady() {
this.labelFrom = new JLabel("查找内容:");
this.labelFrom.setSize(100, 25);
this.labelFrom.setLocation(10, 5);
this.labelFrom.setFont(new Font("宋体", 0, 12));
this.labelTo = new JLabel("替换为:");
this.labelTo.setSize(100, 25);
this.labelTo.setLocation(10, 35);
this.labelTo.setFont(new Font("宋体", 0, 12));
this.textFrom = new JTextField();
this.textFrom.setSize(170, 25);
this.textFrom.setLocation(100, 5);
this.textFrom.setFont(new Font("宋体", 0, 12));
this.textTo = new JTextField();
this.textTo.setSize(170, 25);
this.textTo.setLocation(100, 35);
this.textTo.setFont(new Font("宋体", 0, 12));
this.findNext = new JButton("查找下一个");
this.findNext.setSize(100, 25);
this.findNext.setLocation(280, 5);
this.findNext.setEnabled(false);
this.replace = new JButton("替换");
this.replace.setSize(100, 25);
this.replace.setLocation(280, 35);
this.replace.setEnabled(false);
this.replaceAll = new JButton("全部替换");
this.replaceAll.setSize(100, 25);
this.replaceAll.setLocation(280, 65);
this.replaceAll.setEnabled(false);
this.cancel = new JButton("取消");
this.cancel.setSize(100, 25);
this.cancel.setLocation(280, 95);
this.care = new JCheckBox("区分大小写");
this.care.setSelected(false);
this.panel_Care = new JPanel();
this.panel_Care.add(this.care);
this.panel_Care.setSize(80, 50);
this.panel_Care.setLocation(10, 95);
this.setSize(400, 160);
this.setLocation(400, 300);
this.setResizable(false);
this.setTitle("替换");
this.add(this.labelFrom);
this.add(this.labelTo);
this.add(this.textTo);
this.add(this.textFrom);
this.add(this.findNext);
this.add(this.replace);
this.add(this.replaceAll);
this.add(this.cancel);
this.add(this.panel_Care);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
private void addEvent() {
this.textFrom.addKeyListener(new TextKeyListener(this));
this.textFrom.addMouseListener(new TextMouseListener(this));
this.care.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
myself.isCare = care.isSelected();
}
});
this.findNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
notepad.setFindStr(textFrom.getText());
}
});
this.replace.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (notepad.getTextArea().getSelectedText() == null || "".equals(notepad.getTextArea().getSelectedText())) {
findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
return;
} else {
if (!notepad.getTextArea().getSelectedText().equals(textFrom.getText())) {
// 如果当前选中的和textFrom中的内容不一样,就不能把它替换掉,要当前位置重新查找
findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
return;
}
}
if (textTo.getText() == null) textTo.setText(""); // 防止空指向异常
notepad.getTextArea().replaceSelection(textTo.getText()); // 替换完成
findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
notepad.setFindStr(textFrom.getText());
notepad.setReplaceStr(textTo.getText());
}
});
this.replaceAll.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (textTo.getText() == null) textTo.setText(""); // 防止空指向异常
notepad.getTextArea().setCaretPosition(0);
findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
while (true) {
if (!notepad.getTextArea().getSelectedText().equals(textFrom.getText())) return;
// 如果没有这句,会造成死循环,我没时间去看为什么,直接规避吧,嗯……
System.out.println("选中了:[" + notepad.getTextArea().getSelectedText() + "]");
if (notepad.getTextArea().getSelectedText() == null || "".equals(notepad.getTextArea().getSelectedText())) return;
notepad.getTextArea().replaceSelection(textTo.getText()); // 替换完成
findNext(textFrom.getText(), notepad.getTextArea().getCaretPosition(), isCare);
if (notepad.getTextArea().getCaretPosition() >= notepad.getTextArea().getText().length()) {
System.out.println("退出循环");
return;
}
}
}
});
this.cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
myself.dispose();
}
});
}
private void findNext(String str, int currentIndex, boolean isCare) {
if (str == null || "".equals(str) || currentIndex < 0 || currentIndex > notepad.getTextArea().getText().length()) {
System.err.println("异常!\nFind.java: findNext()");
return;
}
if (isCare) { // 如果区分大小写
notepad.getTextArea().setSelectionStart(notepad.getTextArea().getText().indexOf(str, currentIndex));
notepad.getTextArea().setSelectionEnd(notepad.getTextArea().getText().indexOf(str, currentIndex) + str.length());
} else { // 如果不区分大小写
notepad.getTextArea().setSelectionStart(notepad.getTextArea().getText().toUpperCase().indexOf(str.toUpperCase(), currentIndex));
notepad.getTextArea().setSelectionEnd(notepad.getTextArea().getText().toUpperCase().indexOf(str.toUpperCase(), currentIndex) + str.length());
}
}
private void fillText() {
if (this.notepad.getTextArea().getSelectedText() != null
&& !("".equals(this.notepad.getTextArea().getSelectedText()))) {
this.textFrom.setText(this.notepad.getTextArea().getSelectedText());
} else if (this.notepad.getFindStr() != null
&& !("".equals(this.notepad.getFindStr()))) {
this.textFrom.setText(this.notepad.getFindStr());
}
}
public JTextField getTextField() {
return this.textFrom;
}
public JButton getButton(int num) {
if (num == 1) return this.findNext;
if (num == 2) return this.replace;
if (num == 3) return this.replaceAll;
return null;
}
}
<< 返回