且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何知道是否在jtextarea中进行了任何更改?

更新时间:2023-12-01 11:14:34

您需要添加文档,该文档支持文本区域.

You need to add a DocumentListener to the Document that backs the text area.

然后在侦听器的回调方法(insertUpdate(),removeUpdate(),changedUpdate())中,只需设置一个已更改的标志并在关闭应用程序之前测试该标志

Then in the callback methods (insertUpdate(), removeUpdate(), changedUpdate()) of the listener, simply set a flag that something has changed and test that flag before closing the application


public class MyPanel
  implements DocumentListener
{
  private boolean changed;

  public MyPanel()
  {
    JTextArea textArea = new JTextArea();
    textArea.getDocument().addDocumentListener(this);
    .....
  }

  .....

  public void insertUpdate(DocumentEvent e)
  {
    changed = true;
  }
  public void removeUpdate(DocumentEvent e)
  {
    changed = true;
  }
  public void changedUpdate(DocumentEvent e)
  {
    changed = true;
  }
}