检测JTextArea中的内容是否被修改 各位大大。我想要检测JTextArea的文本内容是否被修改了。怎么做。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不太清楚你说的意思,可以对JTextArea内容的改变事件进行真挺,下面是我实现的例子将添加或者修改的内容在控制台回显import java.awt.BorderLayout;import java.awt.Container;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextArea;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;/** * * @author Administrator * */public class MyJTextAreaDemo1 extends JFrame{ private String checkedStr="清华大学"; private JTextArea textArea=null; private JLabel inputLabel=null; public MyJTextAreaDemo1(){} public MyJTextAreaDemo1(String title){ super(title); textArea=new JTextArea(5,10); inputLabel=new JLabel("input"); inputLabel.setLabelFor(textArea); Container container=this.getContentPane(); container.setLayout(new BorderLayout()); container.add(inputLabel,BorderLayout.EAST); container.add(textArea,BorderLayout.CENTER); textArea.getDocument().addDocumentListener(new DocumentListener(){ //不清楚這個方法监听的什么事件 @Override public void changedUpdate(DocumentEvent event) { // TODO Auto-generated method stub System.out.println("changedUpdate"); } @Override public void insertUpdate(DocumentEvent event) { // TODO Auto-generated method stub System.out.println(textArea.getText()); String inputStr=textArea.getText().trim(); if(inputStr.contains(checkedStr)){ JOptionPane.showMessageDialog(MyJTextAreaDemo1.this, "输出"); } } @Override public void removeUpdate(DocumentEvent event) { // TODO Auto-generated method stub System.out.println("removeUpdate"); } }); this.pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args){ new MyJTextAreaDemo1("Demo"); } } 呵呵。我初学Java。我就是做记事本的退出确认。检测JTextArea是否被修改,修改了就提示保存,否则直接退出。 监听Plaint Document对象事件 定义一个状态变量modified,在Document的事件处理中设为 true,文档保存后设为 false。 这个是楼上思路的具体代码实现,保存文件逻辑你自己实现吧,用IO流import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextArea;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;public class MyJTextAreaDemo2 extends JFrame{ private String checkedStr="清华大学"; private boolean modified=false; private JTextArea textArea=null; private JLabel inputLabel=null; public MyJTextAreaDemo2(){} public MyJTextAreaDemo2(String title){ super(title); textArea=new JTextArea(5,10); inputLabel=new JLabel("input"); inputLabel.setLabelFor(textArea); Container container=this.getContentPane(); container.setLayout(new BorderLayout()); container.add(inputLabel,BorderLayout.EAST); container.add(textArea,BorderLayout.CENTER); textArea.getDocument().addDocumentListener(new DocumentListener(){ //不清楚這個方法监听的什么事件 @Override public void changedUpdate(DocumentEvent event) { // TODO Auto-generated method stub modified=true; System.out.println("changedUpdate"); } @Override public void insertUpdate(DocumentEvent event) { // TODO Auto-generated method stub System.out.println(textArea.getText()); String inputStr=textArea.getText().trim(); modified=true;// if(inputStr.contains(checkedStr)){// JOptionPane.showMessageDialog(MyJTextAreaDemo2.this, "输出");// } } @Override public void removeUpdate(DocumentEvent event) { // TODO Auto-generated method stub modified=true; System.out.println("removeUpdate"); } }); this.pack(); this.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { int needSaveOrNot=0; if(modified){ needSaveOrNot=JOptionPane.showConfirmDialog(MyJTextAreaDemo2.this, "内容被修改是否要保存","保存修改",0); if(needSaveOrNot==JOptionPane.YES_OPTION){ //完成业务保存 } if(needSaveOrNot==JOptionPane.NO_OPTION){ //什么都不做 } } System.exit(0); } });// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args){ new MyJTextAreaDemo2("Demo"); }} 保存 使用 DefaultEditorKit 提供的 write方法,自动处理了换行问题。 关于反射的问题 请问TelnetInputStream和ServletOutputStream的父类 jcreator 启动调试器时出错? 只让一个java程序实例运行 做过WebGIS的请进 请赐教... 如何判断a.txt文件的大小 再将这个a.txt文件另存为b.txt文件呀 如何将java.util.Date转化为java.sql.Date?数据库中Date类型对应于java的哪个Date呢 jbuilder开发疑问??? 挺不错的JAVA小题一道! 多层组件内的paintComponent方法内得到的height和width很小 约瑟夫环公式怎么得到滴
import java.awt.BorderLayout;
import java.awt.Container;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;/**
*
* @author Administrator
*
*/
public class MyJTextAreaDemo1 extends JFrame{
private String checkedStr="清华大学";
private JTextArea textArea=null;
private JLabel inputLabel=null;
public MyJTextAreaDemo1(){}
public MyJTextAreaDemo1(String title){
super(title);
textArea=new JTextArea(5,10);
inputLabel=new JLabel("input");
inputLabel.setLabelFor(textArea);
Container container=this.getContentPane();
container.setLayout(new BorderLayout());
container.add(inputLabel,BorderLayout.EAST);
container.add(textArea,BorderLayout.CENTER);
textArea.getDocument().addDocumentListener(new DocumentListener(){
//不清楚這個方法监听的什么事件
@Override
public void changedUpdate(DocumentEvent event) {
// TODO Auto-generated method stub
System.out.println("changedUpdate");
} @Override
public void insertUpdate(DocumentEvent event) {
// TODO Auto-generated method stub
System.out.println(textArea.getText());
String inputStr=textArea.getText().trim();
if(inputStr.contains(checkedStr)){
JOptionPane.showMessageDialog(MyJTextAreaDemo1.this, "输出");
}
} @Override
public void removeUpdate(DocumentEvent event) {
// TODO Auto-generated method stub
System.out.println("removeUpdate");
}
});
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args){
new MyJTextAreaDemo1("Demo");
}
}
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class MyJTextAreaDemo2 extends JFrame{
private String checkedStr="清华大学";
private boolean modified=false;
private JTextArea textArea=null;
private JLabel inputLabel=null; public MyJTextAreaDemo2(){} public MyJTextAreaDemo2(String title){
super(title);
textArea=new JTextArea(5,10);
inputLabel=new JLabel("input");
inputLabel.setLabelFor(textArea); Container container=this.getContentPane();
container.setLayout(new BorderLayout());
container.add(inputLabel,BorderLayout.EAST);
container.add(textArea,BorderLayout.CENTER); textArea.getDocument().addDocumentListener(new DocumentListener(){ //不清楚這個方法监听的什么事件
@Override
public void changedUpdate(DocumentEvent event) {
// TODO Auto-generated method stub
modified=true;
System.out.println("changedUpdate");
} @Override
public void insertUpdate(DocumentEvent event) {
// TODO Auto-generated method stub
System.out.println(textArea.getText());
String inputStr=textArea.getText().trim();
modified=true;
// if(inputStr.contains(checkedStr)){
// JOptionPane.showMessageDialog(MyJTextAreaDemo2.this, "输出");
// }
} @Override
public void removeUpdate(DocumentEvent event) {
// TODO Auto-generated method stub
modified=true;
System.out.println("removeUpdate");
} }); this.pack();
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
int needSaveOrNot=0;
if(modified){
needSaveOrNot=JOptionPane.showConfirmDialog(MyJTextAreaDemo2.this,
"内容被修改是否要保存","保存修改",0);
if(needSaveOrNot==JOptionPane.YES_OPTION){
//完成业务保存
}
if(needSaveOrNot==JOptionPane.NO_OPTION){
//什么都不做
}
}
System.exit(0);
}
});
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true); } public static void main(String[] args){
new MyJTextAreaDemo2("Demo");
}}