2 szq188 szq188 于 2014.05.11 11:12 提问

关于使用DocumentListener监听报错的问题,请高手指教

我是java新手,自学java。用java写一个JFrame的窗口,能够读取本地电脑上的文件。设置一个JTextArea文本域,用于显示读取的文件的内容。窗口下方有三个按钮(JButton),分别为“打开”、“保存”、“另存为”。打开窗口时,默认“打开”按钮可以点击,其他两个按钮不能点击。当读取文件到JTextArea文本区后,“保存”按钮激活,可以点击。我用下面的代码实现。其中DocumentListener我用两种方式实现:一种是直接在 JFrameTransFile类中实现,在该类内部调用,这种方式是可以修改JButton按钮是否激活属性的;另一种是另外写一个类Swing_OnValueChanged实现DocumentListener接口,在JFrameTransFile类中设置文本区(JTextArea)监听(代码中** **部分),调用Swing_OnValueChanged,此时会报错(报错信息附在代码后面),是在Swing_OnValueChanged类中insertUpdate()方法中修改JFrameTransFile类中的jbutton2的属性是报错的。请高手指教,如果我想在文本区变化时,调用Swing_OnValueChanged类修改JFrameTransFile类中JButton的setEnable属性,该怎么修改代码?在此先谢谢各位大侠了。
以下为部分主要代码:
package demo.java.transfile;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class JFrameTransFile extends WindowAdapter implements ActionListener,TextListener,DocumentListener
{
JFrame jf;//框架变量
JTextArea jtextarea1;//多行文本区
JPanel jp1;
JTextField jtextfield1;//单行文本框
JScrollPane jscrollpane1;
JButton jbutton1,jbutton2,jbutton3;//按钮
FileDialog filedialog1;//文件对话框
File file1=null;//文件对象初始化为空值

public void display()
{//设置界面与监听
    jf=new JFrame("坐标转换");
    jf.setSize(680,400);
    jf.addWindowListener(this);
            .........
            .........       
    //定义监听
    DocumentListener sovc=new Swing_OnValueChanged();
    //设置滚动条
    jscrollpane1=new JScrollPane();
    jf.add(jscrollpane1);
    //添加文本区
    jtextarea1=new JTextArea();
    jtextarea1.setEnabled(false);   
    jscrollpane1.setViewportView(jtextarea1);

     **//添加文本区监听(调用另外的类中实现的DocumentListener)** 
     **jtextarea1.getDocument().addDocumentListener(sovc);**    
     **//添加文本区监听(调用另外的类中实现的DocumentListener)**
    **//jtextarea1.getDocument().addDocumentListener(this);**   

    //面板与布局
    jp1=new JPanel();
    jp1.setLayout(new FlowLayout(FlowLayout.LEFT));     
    jbutton1=new JButton("打开");
    jbutton2=new JButton("保存");
    jbutton3=new JButton("另存为");
    jp1.add(jbutton1);
    jp1.add(jbutton2);
    jp1.add(jbutton3);
    jbutton2.setEnabled(false);
    jbutton3.setEnabled(false);
    jbutton1.addActionListener(this);
    jbutton2.addActionListener(this);
    jbutton3.addActionListener(this);
    jf.add(jp1,"South");
    jf.setVisible(true);
}   
public void textValueChanged(TextEvent e)
{}  
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==jbutton1)
    {
        //创建并打开文件对话框
        filedialog1=new FileDialog(jf,"打开",FileDialog.LOAD);
        filedialog1.setVisible(true);
        if((filedialog1.getDirectory()!=null)&&(filedialog1.getFile()!=null))
        {

jtextfield1.setText(filedialog1.getDirectory()+filedialog1.getFile());
try
{
//为文件对象赋值
file1=new File(filedialog1.getDirectory(),filedialog1.getFile());
//文件读入通道连向文件
FileReader fr=new FileReader(file1);
//定义文件缓冲区
BufferedReader br=new BufferedReader(fr);
String line;
//按行读取文本,每行附加在多行文本区之后
while((line=br.readLine())!=null)
{
jtextarea1.append(line.trim()+"\r\n");
}
fr.close();
br.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
}

}

public void changedUpdate(DocumentEvent e)
{}

public void insertUpdate(DocumentEvent e)
{
jbutton2.setEnabled(true);
}
public void removeUpdate(DocumentEvent e)
{

}
public static void main(String[] args)
{
    new JFrameTransFile().display();
}

}

class Swing_OnValueChanged implements DocumentListener
{
JFrameTransFile jftf=new JFrameTransFile();
public void changedUpdate(DocumentEvent e)
{}
public void insertUpdate(DocumentEvent e)
{
jftf.jbutton2.setEnabled(true);
}
public void removeUpdate(DocumentEvent e)
{}
}
以下部分为点击“打开”按钮时的报错
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at demo.java.transfile.Swing_OnValueChanged.insertUpdate(JFrameTransFile.java:150)
at javax.swing.text.AbstractDocument.fireInsertUpdate(Unknown Source)
at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
at javax.swing.text.AbstractDocument.insertString(Unknown Source)
at javax.swing.text.PlainDocument.insertString(Unknown Source)
at javax.swing.JTextArea.append(Unknown Source)
at demo.java.transfile.JFrameTransFile.actionPerformed(JFrameTransFile.java:107)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!