Java API 分類導覽 - java.awt.TextArea replaceRange()

TextArea 類別 (class) 的 replaceRange() 方法 (method) 用來在文字輸入框內置換新的文字。



修飾子與參數
public void replaceRange(String str, int start, int end)


replaceRange() 沒有回傳值 (return value) ,需要三個參數 (paramenter) ,第一個為 String 型態 (type) 的字串物件 (object) ,也就是要插入的文字,其餘兩個為 int 型態的整數常數, start 為起始索引值, end 為結束索引值。



舉例如下
import java.awt.*;
import java.awt.event.*;

public class TextAreaDemo implements TextListener, ActionListener {
    Frame frame;
    TextArea text;
    
    public static void main(String[] args) {
        new TextAreaDemo();
    }
    
    public TextAreaDemo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setLayout(new BorderLayout());
        frame.setSize(640, 480);
         
        text = new TextArea();
        text.addTextListener(this);
        text.setColumns(40);
        text.setRows(30);
        text.insert("There is no spoon.", 0);
        
        Button button = new Button("Change");
        button.addActionListener(this);

        frame.add(text, BorderLayout.CENTER);
        frame.add(button, BorderLayout.SOUTH);
        
        frame.setVisible(true);
        
        System.out.println("Columns: " + text.getColumns());
        System.out.println("Rows: " + text.getRows());
        System.out.println("Scrollbar visibility: " + text.getScrollbarVisibility());
    }
    
    public void textValueChanged(TextEvent e) {
        System.out.print("!");
    }

    public void actionPerformed(ActionEvent e) {
        text.replaceRange("xxx", 0, 2);
        text.append("(new)");
    }
}

class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:TextAreaDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例的 TextAreaDemo 實作 TextListener 及 ActionListener 介面
public class TextAreaDemo implements TextListener, ActionListener {


建立 TextArea 物件後,呼叫 TextComponent 的 addTextListener() 處理文字輸入框內容改變的事件,並以 this 當參數,其後依序呼叫 setColumns() 設定可輸入字元行數,也就是文字輸入框的寬度, setRows() 設定可輸入列數,也就是文字輸入框的高度,最後 insert() 在文字輸入框起始處插入字串
text = new TextArea();
text.addTextListener(this);
text.setColumns(40);
text.setRows(30);
text.insert("There is no spoon.", 0);


另外設置了一個 Change 按鈕
Button button = new Button("Change");
button.addActionListener(this);


建構子最後工作是在命令列印出相關訊息,依序呼叫 TextArea 的 getColumns() 印出文字輸入框的寬度, getRows() 印出文字輸入框的高度, getScrollbarVisibility() 印出文字輸入框的捲軸是否可見
System.out.println("Columns: " + text.getColumns());
System.out.println("Rows: " + text.getRows());
System.out.println("Scrollbar visibility: " + text.getScrollbarVisibility());


由於 TextAreaDemo 實作 TextListener 介面,因此要寫出 textValueChanged()
public void textValueChanged(TextEvent e) {
    System.out.print("!");
}


這裡,只要文字輸入框的內容有任何變動,命令列就會印出一個驚嘆號 "!" 。


也由於 TextAreaDemo 實作 ActionListener 介面,因此要寫出 actionPerformed()
public void actionPerformed(ActionEvent e) {
    text.replaceRange("xxx", 0, 2);
    text.append("(new)");
}


利用滑鼠點擊 Change 時,就會呼叫 TextArea 的 replaceRange() 更換索引值 0 到 2 的子字串,然後呼叫 append() 在輸入字串的結尾加上 "(new)" 。


編譯後執行,結果如下



中英文術語對照
類別class
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
AWT 元件


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/TextArea.html

沒有留言: