package java实践;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Replace extends JDialog implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -4598195336296280059L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
int next;
private JCheckBox checkBox;
private JTextArea textArea;
private JButton button;
private JButton button_1;
private JButton button_2;
private JButton button_3;
private JCheckBox checkBox_1;
/**
* Create the frame.
*/
public Replace(JTextArea textArea) {
this.textArea=textArea;
init();
}
public void init() {
setVisible(true);
setBounds(100, 100, 847, 414);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(2, 0, 0, 0));
JPanel panel = new JPanel();
contentPane.add(panel);
panel.setLayout(null);
JLabel label = new JLabel("查找内容");
label.setBounds(43, 41, 72, 18);
panel.add(label);
JLabel label_1 = new JLabel("替换为:");
label_1.setBounds(43, 93, 72, 18);
panel.add(label_1);
textField = new JTextField();
textField.setBounds(117, 38, 373, 24);
panel.add(textField);
textField.setColumns(10);
//查找
textField.addActionListener(this);
textField_1 = new JTextField();
textField_1.setBounds(117, 90, 373, 24);
panel.add(textField_1);
textField_1.setColumns(10);
button = new JButton("查找下一个");
button.setBounds(643, 37, 113, 27);
panel.add(button);
//查找下一处
button.addActionListener(this);
button_1 = new JButton("替换");
button_1.setBounds(643, 89, 113, 27);
panel.add(button_1);
//替换
button_1.addActionListener(this);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1);
panel_1.setLayout(null);
JPanel panel_2 = new JPanel();
panel_2.setBorder(new TitledBorder(null, "\u9009\u9879", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_2.setBounds(0, 0, 570, 178);
panel_1.add(panel_2);
panel_2.setLayout(null);
checkBox = new JCheckBox("区分大小写");
checkBox.setBounds(134, 92, 133, 27);
panel_2.add(checkBox);
checkBox_1 = new JCheckBox("正则表达式");
checkBox_1.setBounds(304, 92, 133, 27);
panel_2.add(checkBox_1);
button_2 = new JButton("全部替换");
button_2.addActionListener(this);
button_2.setBounds(646, 36, 113, 27);
panel_1.add(button_2);
button_3 = new JButton("取消");
button_3.addActionListener(this);
button_3.setBounds(646, 101, 113, 27);
panel_1.add(button_3);
}
//处理事件
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
//查找
if (e.getSource()==textField) {
String string=textArea.getText();
String string1=textField.getText();
Pattern pattern=Pattern.compile(string1);
Matcher matcher=pattern.matcher(string);
if(matcher.find()) {
int start=matcher.start();
int end=matcher.end();
textArea.select(start, end);
textArea.setSelectedTextColor(Color.blue);
}
}
//查找下一处
else if (e.getSource()==button) {
int k=0,m=0;
final String str1,str2,str3,str4,strA,strB;
str1=textArea.getText();
str2=textField.getText();
str3=str1.toUpperCase();
str4=str2.toUpperCase();
if(checkBox.isSelected()){
strA=str1;
strB=str2;
}
else{ //不区分大小写,此时把所选内容全部化成大写(或小写),以便于查找
strA=str3;
strB=str4;
}
if(textArea.getSelectedText()==null) {
k=strA.indexOf(strB,textArea.getCaretPosition()+1);
}else {
k=strA.indexOf(strB, textArea.getCaretPosition()-textField.getText().length()+1);
}if(k>-1){
textArea.setCaretPosition(k);
textArea.select(k,k+strB.length());
}else{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
//替换
else if (e.getSource()==button_1) {
if (textField_1.getText().length() == 0 && textArea.getSelectedText() != null)
textArea.replaceSelection("");
if (textField_1.getText().length() > 0 && textArea.getSelectedText() != null)
textArea.replaceSelection(textField_1.getText());
}
//全部替换
else if(e.getSource()==button_2){
String string=textArea.getText();
String string1=textField.getText();
String string2=textField_1.getText();
Pattern pattern=Pattern.compile(string1);
Matcher matcher=pattern.matcher(string);
if(matcher.find()) {
String s=matcher.replaceAll(string2);
textArea.setText(s);
}
}
//退出
else if (e.getSource()==button_3) {
setVisible(false);
}
}
}