日期:2014-05-20  浏览次数:21021 次

java文本区监听器的使用
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class WindowTextArea extends Frame implements TextListener,ActionListener
{ TextArea text1,text2;
  Button buttonClear; 
  WindowTextArea()
  { setLayout(new FlowLayout());
  text1=new TextArea(6,15);
 
text2=new TextArea(6,15); 

buttonClear=new Button("确定");
text2.setEditable(false); 
  add(text1);
  add(text2);
  add(buttonClear);
  text1.addTextListener(this); 
  buttonClear.addActionListener(this);
   
  setBounds(100,100,350,160);
  setVisible(true);
  validate();
  } 
  public void textValueChanged(TextEvent e) 
  { String s=text1.getText();
StringTokenizer fenxi=new StringTokenizer(s,",'\n'");
int n=fenxi.countTokens();
String a[]=new String[n];
for(int i=0;i<n-1;i++)
{ String teap=fenxi.nextToken();
a[i]=teap;  
 
}  
  Arrays.sort(a);
  text2.setText(null);
  for(int i=0;i<n;i++)
  { text2.append(a[i]+"\n");
   
  }  
  }
   
  public void actionPerformed(ActionEvent e)
  { text1.setText(null);
 
  } 


}
public class TextAreaExa {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WindowTextArea win=new WindowTextArea();
}

}



代码实现的功能是:1当向一个文本区输入若干个单词时,另一个文本区不但更新单词的顺序,安字典顺序排列。2当按“确定”安钮,两个文本区都被清空。第二个功能实现了,但是第一个功能不能实现,当向一个文本区输完一个单词时按空格键 输另一个单词,程序开始报错,下面上所报错误:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.lang.String.compareTo(Unknown Source)
at java.lang.String.compareTo(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at WindowTextArea.textValueChanged(TextAreaExa.java:35)
at java.awt.TextComponent.processTextEvent(Unknown Source)
at java.awt.TextComponent.processEvent(Unknown Source)
at java.awt.Component.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)

这个问题该怎么解决??

------解决方案--------------------
Java code
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class WindowTextArea extends Frame implements TextListener,ActionListener
{
    TextArea text1,text2;
    Button buttonClear;
    WindowTextArea()
    {
        setLayout(new FlowLayout());

        text1 = new TextArea(6,15);
        text2 = new TextArea(6,15);
        text2.setEditable(false);
        buttonClear = new Button("确定");

        add(text1);
        add(text2);
        add(buttonClear);

        text1.addTextListener(this);
        buttonClear.addActionListener(this);

        setBounds(100,100,350,160);
        setVisible(true);
        validate();

        addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
    }
    public void textValueChanged(TextEvent e)
    {
        String s = text1.getText();
        String[] a = s.split("\\s+");
        Arrays.sort(a);
        text2.setText("");
        for(int i = 0; i < a.length; i++)
            {
                text2.append(a[i]+"\n");
            }
    }

    public void actionPerformed(ActionEvent e)
    {
        text1.setText("");
    }
}

public class TextAreaExa {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WindowTextArea win = new WindowTextArea();
    }
}