e968. retrieving text from a jtextcomponent.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 24 行

TXT
24
字号
This example works for all types of text components. 
    // Create the text component
    JTextComponent tc = new JTextArea("Initial Text");
    
    // Get length
    int docLength = tc.getDocument().getLength();
    
    // Get all text
    String text = tc.getText();
    
    try {
        // Get the first 3 characters
        int offset = 0;
        int len = 3;
        text = tc.getText(offset, len);
    
        // Get the last 3 characters
        offset = docLength-3;
        len = 3;
        text = tc.getText(offset, len);
    } catch (BadLocationException e) {
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?