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("清空");
add(text1);
add(text2);
add(buttonClear);
text2.setEditable(false);
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 temp=fenxi.nextToken();
a[i]=temp;
} 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 Example7_9
{ public static void main(String args[])
{ WindowTextArea win=new WindowTextArea();
}
}
黄色的代码不知道是什么意思,帮忙讲解下最好每条都要
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("清空");
add(text1);
add(text2);
add(buttonClear);
text2.setEditable(false);
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 temp=fenxi.nextToken();
a[i]=temp;
} 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 Example7_9
{ public static void main(String args[])
{ WindowTextArea win=new WindowTextArea();
}
}
黄色的代码不知道是什么意思,帮忙讲解下最好每条都要
{ String s=text1.getText();
StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");//用逗号和回车分割字符串s
int n=fenxi.countTokens(); //分割字符串获得得子串个数
String a[]=new String[n];
for(int i=0;i <=n-1;i++) //循环取出来放入数组中
{ String temp=fenxi.nextToken();
a[i]=temp;
} Arrays.sort(a); //排序改数组并在后面放入到此textfield区域中
text2.setText(null);
for(int i=0;i <n;i++)
{ text2.append(a[i]+"\n");
}
}
运行程序在text2中粘如7123,2159\n324343
自己看下效果就知道了 看StringTokenizer得api解释