像windows自带那样的,以前C#的多好写,将替换的文本赋值给RichTextBox的选中文本,求代码例子

解决方案 »

  1.   

    http://hi.baidu.com/freish/blog/item/21a604f7c0271b28730eec61.html
      

  2.   

    public ReplaceDialog(JTextArea text, Frame parent, String title,
    boolean modal) {
    super(parent, title, modal);
    this.txtOwner = text;
    JPanel pBottom = new JPanel();
    btnSearch = new JButton("查找");
    btnSearch.addActionListener(this);
    btnReplace = new JButton("替换");
    btnReplace.addActionListener(this);
    btnCancel = new JButton("取消");
    btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    dispose();
    } });
    pBottom.add(btnSearch);
    pBottom.add(btnReplace);
    pBottom.add(btnCancel);
    this.add(pBottom, "South"); JPanel pTop = new JPanel();
    pTop.add(new JLabel("查找的内容:"));
    txtSearch = new JTextField(15);
    pTop.add(txtSearch);
    this.add(pTop, "North"); JPanel pCenter = new JPanel();
    pCenter.add(new JLabel("替换为:"));
    txtReplace = new JTextField(15);
    pCenter.add(txtReplace);
    this.add(pCenter); this.pack();
    this.setLocationRelativeTo(parent);
    this.setResizable(false);
    this.setVisible(true); } public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if (e.getSource() == btnSearch) { String source = txtOwner.getText();
    String content = txtSearch.getText();
    int start = txtOwner.getSelectionStart();
    index = source.indexOf(content, start);
    if (index != -1) {
    txtOwner.select(index, content.length());//找到的话选中文本框的文字 } else {//找不到弹出对话框
    InfoDialog info = new InfoDialog(this.txtSearch.getText(),
    this, "提示!!!", false);
    }
    }
    if(e.getSource()==btnReplace){
    String replaced=txtReplace.getText();
    String source=txtOwner.getText();
    String content = txtSearch.getText();
    int start = txtOwner.getSelectionStart();
    index = source.indexOf(content, start);
    if (index != -1) {
      txtOwner.select(index, content.length());//找到的话选中文本框的文字 }
    }
    }
    这是我写的一部分,谁完成下替换