刪除 Java 中 JTextArea 的前 10 個字元
假設我們有以下帶預設文字的 JTextArea -
JTextArea textArea = new JTextArea("The text added here is just for demo. "
+ "
This demonstrates the usage of JTextArea in Java. In this example we have"
+ "deleted some text.");現在若要刪除前 10 個字元,請使用 replaceRange() 方法,並從一端設定為空,到另一端,即刪除範圍中的字元。replaceRaneg() 方法將從指示的起始位置到結束位置將文字替換為指定的新文字,即這裡的 null 將替換前 10 個字元 -
int start = 0; int end = 10; textArea.replaceRange(null, start, 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("The text added here is just for demo. " + "
This demonstrates the usage of JTextArea in Java. In this example we have" + "deleted some text.");
int start = 0;
int end = 10;
textArea.replaceRange(null, start, 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 ();
}
}輸出如下。我們已在此處刪除前 10 個字元 -
輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式語言
C++
C#
MongoDB
MySQL
Javascript
PHP