//我曾今写过的,自己看,很容易
private void search() {
final JDialog jd=new JDialog();
final JTextField sjtf1=new JTextField(10);
final JTextField sjtf2=new JTextField(10);
JPanel sjp1=new JPanel();
sjp1.add(sjtf1);sjp1.add(sjtf2);
jd.add(sjp1);
JPanel sjp2=new JPanel();
JButton sjb1=new JButton("查找");
JButton sjb2=new JButton("替换");
sjp2.add(sjb1);sjp2.add(sjb2);
jd.add(sjp2,BorderLayout.SOUTH);
jd.setBounds(400, 250, 300, 100);
jd.setVisible(true);
jd.setResizable(false);
sjb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s=sjtf1.getText();
int x = textEdit.getText().indexOf(s, textEdit.getSelectionEnd());
if (x == -1) {
JOptionPane.showConfirmDialog(null, "没找到 " + s);
} else {
textEdit.setSelectionStart(x);
textEdit.setSelectionEnd(x + s.length());
}
//jd.dispose();
}
});
sjb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s=sjtf1.getText();
String s1=sjtf2.getText();
int x = textEdit.getText().indexOf(s, textEdit.getSelectionEnd());
if (x == -1) {
JOptionPane.showConfirmDialog(null, "没找到 " + s);
} else {
textEdit.setSelectionStart(x);
textEdit.setSelectionEnd(x + s.length());
textEdit.replaceSelection(s1);
textEdit.setSelectionStart(textEdit.getSelectionEnd()-s1.length());
textEdit.setSelectionEnd(textEdit.getSelectionEnd());
}
//jd.dispose();
}
});
}
温馨提示:答案为网友推荐,仅供参考