Java API 分類導覽 - java.awt.TextComponent getSelectionStart()

TextComponent 類別 (class) 的 getSelectionStart() 方法 (method) 用來取得選取文字的起始點。



修飾子與參數
public int getSelectionStart()


getSelectionStart() 的回傳值 (return value) 為 int 型態 (type) 的常數,非物件 (object) ,不需要參數 (paramenter) 。


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

public class TextComponentDemo implements TextListener, ActionListener {
    Frame frame;
    TextField text;
    
    public static void main(String[] args) {
        new TextComponentDemo();
    }
    
    public TextComponentDemo() {
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setLayout(new FlowLayout());
         
        text = new TextField(20);
        text.addTextListener(this);
        text.addActionListener(this);
        text.setBackground(Color.white);
        text.setEditable(true);
        text.setText("There is no spoon.");
        text.selectAll();

        frame.add(text);
        
        frame.pack();
        frame.setVisible(true);
        
        System.out.println("Background: " + text.getBackground());
        System.out.println("CaretPosition: " + text.getCaretPosition());
        System.out.println("Editable: " + text.isEditable());
        System.out.println("Text: " + text.getText());
    }
    
    public void textValueChanged(TextEvent e) {
        if (text.getText().length() > 20) {
            text.select(5, 15);
            System.out.println("SelectedText: " + text.getSelectedText());
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        text.setSelectionStart(12);
        text.setSelectionEnd(16);
        System.out.println("SelectedText: " + text.getSelectedText());
        text.setCaretPosition(9);
    }
}

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

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


此例的 TextComponentDemo 直接實作 TextListener 與 ActionListener 介面
public class TextComponentDemo implements TextListener, ActionListener {


先建立 TextComponent 的子類別 TextField 物件,然後呼叫 TextComponent 的 addTextListener() 處理輸入字改變的事件,並以 this 當參數,後面呼叫 TextField 的 addActionListener() 處理按下 Enter 的事件,同樣以 this 當參數,其後依序呼叫 TextComponent 的 setBackground() 設定背景顏色, setEditable() 設定是否可編輯, setText() 設定文字, selectAll() 選取設定好的所有文字
text = new TextField(20);
text.addTextListener(this);
text.addActionListener(this);
text.setBackground(Color.white);
text.setEditable(true);
text.setText("There is no spoon.");
text.selectAll();


建構子最後工作是在命令列印出相關訊息,依序呼叫 TextComponent 的 getBackground() 印出背景顏色, getCaretPosition() 印出文字輸入的定位點, isEditable() 印出是否可被編輯, getText() 印出已設定的文字
System.out.println("Background: " + text.getBackground());
System.out.println("CaretPosition: " + text.getCaretPosition());
System.out.println("Editable: " + text.isEditable());
System.out.println("Text: " + text.getText());


由於 TextComponentDemo 實作 TextListener 介面,因此要寫出 textValueChanged()
public void textValueChanged(TextEvent e) {
    if (text.getText().length() > 20) {
        text.select(5, 15);
        System.out.println("SelectedText: " + text.getSelectedText());
    }
}


當文字輸入框的內容有所變動時,便會呼叫 TextComponent 的 getText() 取得文字的字串物件,然後測試長度是否超過 20 ,如果字串長度超過 20 ,就呼叫 TextComponent 的 select() 選取其中一段子字串,然後在命令列印出來。


TextComponentDemo 另有實作 ActionListener ,所以要寫出 actionPerformed()
public void actionPerformed(ActionEvent e) {
    text.setSelectionStart(12);
    text.setSelectionEnd(16);
    System.out.println("SelectedText: " + text.getSelectedText());
    text.setCaretPosition(9);
}


按下 Enter 後,程式便會執行 actionPerformed() 。這裡先呼叫 TextComponent 的 setSelectionStart() 設定選取的起始索引值,然後呼叫 setSelectionEnd() 設定選取的結束索引值,然後在命令列中印出選取的文字內容,最後呼叫 setCaretPosition() 設定文字輸入的定位點。


編譯後執行,結果如下



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


您可以繼續參考
AWT 元件


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


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

沒有留言: