自己写个组件吧,java只提供替换单个字符的方法,我贴一个:
package basic;
public class Replace
{
public static String strReplace(String mainString,String one,String two)
{
String strReturn="";
int start=0;
while((start=mainString.indexOf(one))!=-1)
{
String strFirst=mainString.substring(0,start);
strReturn+=strFirst+two;
mainString=mainString.substring(start+one.length());
}
return strReturn+mainString;
}
public static void main(String args[])
{
System.out.println(new Replace().strReplace(args[0],args[1],args[2])); }
}
package basic;
public class Replace
{
public static String strReplace(String mainString,String one,String two)
{
String strReturn="";
int start=0;
while((start=mainString.indexOf(one))!=-1)
{
String strFirst=mainString.substring(0,start);
strReturn+=strFirst+two;
mainString=mainString.substring(start+one.length());
}
return strReturn+mainString;
}
public static void main(String args[])
{
System.out.println(new Replace().strReplace(args[0],args[1],args[2])); }
}
/**
* @version 1.20 27 Jul 1998
* @author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TextEditFrame extends JFrame
{ public TextEditFrame()
{ setTitle("TextEditTest");
setSize(500, 500);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane = getContentPane(); JPanel panel = new JPanel();
JButton replaceButton = new JButton("Replace");
panel.add(replaceButton);
replaceButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ String f = from.getText();
int n = textArea.getText().indexOf(f);
if (n >= 0 && f.length() > 0)
textArea.replaceRange(to.getText(), n,
n + f.length());
}
}); from = new JTextField(8);
panel.add(from); panel.add(new JLabel("with")); to = new JTextField(8);
panel.add(to);
textArea = new JTextArea(8, 40);
scrollPane = new JScrollPane(textArea); contentPane.add(panel, "South");
contentPane.add(scrollPane, "Center");
}
private JScrollPane scrollPane;
private JTextArea textArea;
private JTextField from, to;
}public class TextEditTest {
public static void main(String[] args)
{ JFrame f = new TextEditFrame();
f.show();
}
}