您现在的位置是:首页 > 电脑 > 

如何知道jtextarea是否有任何变化?(How to know whether any changes in the jtextarea have been made or not?)

2025-07-21 04:55:49
如何知道jtextarea是否有任何变化?(How to know whether any changes in the jtextarea have been made or not?) 我创建了一个jtextarea,用户可以在其中修改其内容。 我想知道,如果有任何办法,用户是否在关闭应用程序之前修改了其内容。 请帮忙。 -提前致谢 I've create
如何知道jtextarea是否有任何变化?(How to know whether any changes in the jtextarea have been made or not?)

我创建了一个jtextarea,用户可以在其中修改其内容。 我想知道,如果有任何办法,用户是否在关闭应用程序之前修改了其内容。 请帮忙。 -提前致谢

I've created a jtextarea where a user can modify its content. I want to know,if there is any way, whether the user has modified its content or not before closing the application. Please help. -Thanks in advance

最满意答案

您需要将DocumentListener添加到支持文本区域的Document 。

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

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; } }

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

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; } }

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697387.html

相关标签:无
上传时间: 2023-08-27 09:51:15
留言与评论(共有 12 条评论)
本站网友 灰色按钮加强版
13分钟前 发表
changedUpdate())中
本站网友 美白养颜汤
7分钟前 发表
removeUpdate()
本站网友 浅水湾二手房
10分钟前 发表
if there is any way
本站网友 聂磊
11分钟前 发表
用户可以在其中修改其内容
本站网友 鸡贼是什么意思
18分钟前 发表
removeUpdate()
本站网友 中学生英语
17分钟前 发表
if there is any way
本站网友 以岭药业股份有限公司
15分钟前 发表
用户可以在其中修改其内容
本站网友 行唐二手房出售
0秒前 发表
changedUpdate())中
本站网友 41122
1分钟前 发表
请帮忙
本站网友 假体隆鼻术费用
5分钟前 发表
用户可以在其中修改其内容
本站网友 applewatch
17分钟前 发表
changedUpdate())中