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 Example7_9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
WindowTextArea win = new WindowTextArea();
}
}
http://www.cnblogs.com/upstart/p/6011927.html
求解释 那个看不懂 初学菜鸟 谢谢
!图片说明](https://img-ask.csdn.net/upload/201704/14/1492101144_512756.jpg)
String a[] = s.split("\s+");
知道这句话什么意思吗,输入的字符串按照 一个或多个空格 分割
而你输入的字符串包含逗号 所以分解不了。。。
String a[] = s.split("[\s,]+");
就可以了 和sort无关
可以把这个排序后的数组赋值给一个新定义的数组,再打印出来看看
字符串排序是按照字典序排的
你可以在Arrays.sort之前把String[] s的内容打印一下
然后在之后再打印一下
看看到底有没有排序