各位大大。我想要检测JTextArea的文本内容是否被修改了。怎么做。

解决方案 »

  1.   

    不太清楚你说的意思,可以对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");
    }

    }
      

  2.   

    呵呵。我初学Java。我就是做记事本的退出确认。检测JTextArea是否被修改,修改了就提示保存,否则直接退出。
      

  3.   

    监听Plaint Document对象事件
      

  4.   

    定义一个状态变量modified,在Document的事件处理中设为 true,文档保存后设为 false。
      

  5.   

    这个是楼上思路的具体代码实现,保存文件逻辑你自己实现吧,用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");
    }}
      

  6.   

    保存 使用 DefaultEditorKit 提供的 write方法,自动处理了换行问题。