ユーザーがコンテンツを変更できる jtextarea を作成しました。アプリケーションを閉じる前に、ユーザーがコンテンツを変更したかどうかを知りたいです。助けてください。
-前もって感謝します
7971 次
2 に答える
10
テキスト領域を支えるドキュメントにDocumentListenerを追加する必要があります。
次に、リスナーのコールバックメソッド(insertUpdate()、removeUpdate()、changedUpdate())で、何かが変更されたことを示すフラグを設定し、アプリケーションを閉じる前にそのフラグをテストします。
パブリッククラスMyPanel DocumentListenerを実装します {{ プライベートブール値が変更されました。 public MyPanel() {{ JTextArea textArea = new JTextArea(); textArea.getDocument()。addDocumentListener(this); ....。 } ....。 public void insertUpdate(DocumentEvent e) {{ 変更=true; } public void removeUpdate(DocumentEvent e) {{ 変更=true; } public void changedUpdate(DocumentEvent e) {{ 変更=true; } }
于 2011-01-29T10:29:12.720 に答える
2
jtextarea の値を保存し、アプリケーションを閉じた瞬間にこの値を jtextarea の値と比較します。
ここの疑似コードは、テキスト領域の正確な構文を覚えていません:
String oldText = textarea.getText();
....
// not the exact method, just to point the moment of application exit
public onClose() {
String newText = textArea.getText();
// assuming oldText is not null
if (oldText.equals(newText)) {
// no changes have been done
} else {
// the value changed
}
}
于 2011-01-29T10:08:45.977 に答える