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