java记事本的查找功替换能怎么实现,请说的具体点!
3个回答
推荐于2017-12-16
展开全部
//我曾今写过的,自己看,很容易
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();
}
});
}
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();
}
});
}
2013-11-11
展开全部
查找功能String里面不是有个indexOf("")吗?这样找到第一个出现的位置 ,然后再从这个位置开始找下面的第一个出现的位置 知道文章结束。 返回找就应lastIndexOf("");类似的啊
替换的话当然是replace(" "," ");你去试试吧 我也是只给你思路!
替换的话当然是replace(" "," ");你去试试吧 我也是只给你思路!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-11
展开全部
String 类中用此方法
StringBuffer 也用此类方法 上面的的答案也可以
StringBuffer 也用此类方法 上面的的答案也可以
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |