Cuối cùng nó cũng làm như vậy.
Chuyển đến mã nguồn của JTextArea
here, nơi bạn có thể thấy rằng nó đang làm điều tương tự. tôi đã sao chép phương pháp ở đây cũng là nơi bạn có thể thấy rằng nó đang làm
d.remove(pos, word1.length());
d.insertString(pos, word2.toUpperCase(), null);
trong trường hợp gọi:
t.replaceRange(word2.toUpperCase(), pos, pos+ word1.length());
phương pháp.
Source code của phương pháp của lớp dưới
public void replaceRange(String str, int start, int end) {
490 if (end < start) {
491 throw new IllegalArgumentException ("end before start");
492 }
493 Document doc = getDocument();
494 if (doc != null) {
495 try {
496 if (doc instanceof AbstractDocument) {
497 ((AbstractDocument)doc).replace(start, end - start, str,
498 null);
499 }
500 else {
501 doc.remove(start, end - start);
502 doc.insertString(start, str, null);
503 }
504 } catch (BadLocationException e) {
505 throw new IllegalArgumentException (e.getMessage());
506 }
507 }
508 }
Nguồn
2012-05-12 08:23:35
tò mò: tại sao tiền thưởng lại? bạn không hài lòng với câu trả lời của riêng bạn? – kleopatra