Java 程序,用于替换 JTextArea 中的前 10 个字符
若要替换文本区域中的前 10 个字符,请使用 Java 中的 replaceRange() 方法,并将旧文本替换为新文本。
假设在下文中设置了 oude 演示文本,其中使用了 JTextArea −
JTextArea textArea = new JTextArea("This is a text displayed for our example. We have replaced some of the text.");现在,替换范围中的字符 −
int begn = 0;
int end = 10;
// replace
textArea.replaceRange("Replaced! ", begn, end);以下是一个示例,用于替换 JTextArea 中的前 10 个字符 −
示例
package my;
import java.awt.GridLayout;
import javax.swing.*;
public class SwingDemo {
SwingDemo() {
JFrame frame = new JFrame("Demo");
JTextArea textArea = new JTextArea("This is a text displayed for our example. We have replaced some of the text.");
int begn = 0;
int end = 10;
// replace
textArea.replaceRange("Replaced! ", begn, end);
frame.add(textArea);
frame.setSize(550,300);
frame.setLayout(new GridLayout(2, 2));
frame.setVisible(true);
}
public static void main(String args[]) {
new SwingDemo ();
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP