import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class WindowTextArea extends JFrame implements ActionListener
{
JButton button;
JMenuBar menubar;
JMenu menu;
JMenuItem item1,item2;
JTextArea text1,text2;
boolean boo=true;
JPanel p;
WindowTextArea(String s)
{
super(s);
button=new JButton("排列");
item1=new JMenuItem("升序");
item2=new JMenuItem("降序");
menu=new JMenu("排列顺序");
menu.add(item1);
menu.add(item2);
menubar=new JMenuBar();
menubar.add(menu);
setJMenuBar(menubar);
p=new JPanel();
p.add(button);
add(p,BorderLayout.NORTH);
item1.addActionListener(this);
item2.addActionListener(this);
button.addActionListener(this);
setLayout(new FlowLayout());
text1=new JTextArea(6,15);
text2=new JTextArea(6,15);
add(new JScrollPane(text1));
add(new JScrollPane(text2));
text1.setLineWrap(true);
text1.setWrapStyleWord(true);
text2.setEditable(false);
setBounds(100,100,250,350);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==item1)
{
boo=true;
text1.setText(null);
text2.setText(null);
}
else if(e.getSource()==item2)
{
boo=false;
text1.setText(null);
text2.setText(null);
}
else
{
String s=text1.getText();
StringTokenizer fenxi=new StringTokenizer(s,",");
int n=fenxi.countTokens();
int []a=new int[n];
for(int i=0;i<=n-1;i++)
{
try{
a[i]=Integer.parseInt(fenxi.nextToken());
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(this,"请输入数字字符","警告",JOptionPane.WARNING_MESSAGE);
text1.setText(null);
}
}
Arrays.sort(a); //按字典从小到大排序
text2.setText(null);
if(boo)
{
for(int i=0;i<n;i++)
{
text2.append(a[i]+"\n");
}
}
else
{
for(int i=n-1;i>=0;i--)
{
text2.append(a[i]+"\n");
}
}
}
}
}
class lianxi
{
public static void main(String[] args)
{
new WindowTextArea("编辑练习");
}
}
我试了的可以 数字行 ,前面那个没仔细测试有点问题