import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class text extends JFrame {
private JTextField textField1;
private JTextArea field;
private JPanel panel;
private JLabel label;
private String string;
public text()
{
super( "String Sorter" );
Container container = getContentPane();
container.setLayout( null );
label = new JLabel("请输入句子:");
label.setBounds(20,20,200,20);
label.setFont( new Font( "黑体", Font.PLAIN, 15 ) );
container.add(label);
textField1 = new JTextField( 10 );
textField1.setBounds(120,20,170,20);
container.add( textField1 );
textField1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int i = 0,j,a;
StringTokenizer st = new StringTokenizer(textField1.getText());
String e[] = new String[st.countTokens()];
while (st.hasMoreTokens())
{
e[i] = st.nextToken();
i++;
}
i--;
for(a = i;a>0;a--)
{
for(j=0;j<a;j++)
{
if(e[j].compareTo( e[j+1]) > 0)
{
string = e[j+1];
e[j+1] = e[j];
e[j] = string;
}
}
}
for(j=0;j<=i;j++)
{
field.append(e[j]+"\n");
}
}
}
);
panel = new JPanel();
panel.setBounds(15,50,280,180);
panel.setBorder(BorderFactory.createTitledBorder("显示结果"));
panel.setLayout(new GridLayout(1,1));
container.add(panel);
field = new JTextArea( );
field.setLineWrap(true);
field.setEditable(false);
field.setBounds(50,70,50,50);
field.setFont( new Font( "黑体", Font.PLAIN, 15 ) );
panel.add(field);
setBounds(300,150, 325, 280 );
setResizable(false);
setVisible( true );
}
public static void main( String args[] )
{
text application = new text();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
评论5
最新资源