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)
这个问题该怎么解决??
6 个解决方案
#1
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();
}
}
#2
++1
#3
楼上的对正则表达式很熟吧,String[] a = s.split("\\s+");
split(String regex)方法中"\\s+”中改为\\s或\\s=效果完全不同,在正则表达式也没看到\\s后带+或=的,能否解释一下?还有能识别空格和逗号\\s,不行啊?楼上的能否一并改一下?
split(String regex)方法中"\\s+”中改为\\s或\\s=效果完全不同,在正则表达式也没看到\\s后带+或=的,能否解释一下?还有能识别空格和逗号\\s,不行啊?楼上的能否一并改一下?
#4
s.split("[\\p{Space}\\p{Punct}]+");
不支持中文标点
+ 意思是至少有一个
不支持中文标点
+ 意思是至少有一个
#5
多谢楼上的,问题一下给解决了
#6
#1
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();
}
}
#2
++1
#3
楼上的对正则表达式很熟吧,String[] a = s.split("\\s+");
split(String regex)方法中"\\s+”中改为\\s或\\s=效果完全不同,在正则表达式也没看到\\s后带+或=的,能否解释一下?还有能识别空格和逗号\\s,不行啊?楼上的能否一并改一下?
split(String regex)方法中"\\s+”中改为\\s或\\s=效果完全不同,在正则表达式也没看到\\s后带+或=的,能否解释一下?还有能识别空格和逗号\\s,不行啊?楼上的能否一并改一下?
#4
s.split("[\\p{Space}\\p{Punct}]+");
不支持中文标点
+ 意思是至少有一个
不支持中文标点
+ 意思是至少有一个
#5
多谢楼上的,问题一下给解决了