写了一个Swing版本的,楼主的Applet应该很容易能写了:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*;
class InputPanel extends JPanel { private JTextField inputText; private JTextField outputText; InputPanel() { TextPanel panel = new TextPanel(); add(panel); } private class TextPanel extends JPanel { int num; //The input numbers' total number int data[]; TextPanel() { setLayout(new BorderLayout()); inputText = new JTextField(20); outputText = new JTextField(20); add(inputText, BorderLayout.NORTH); add(outputText, BorderLayout.SOUTH);
inputText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String input = inputText.getText(); //Suppose we use " " to divide the input StringTokenizer token = new StringTokenizer(input, " "); int length = token.countTokens(); data = new int[length]; for(int i = 0; i < length; i++) data[i] = Integer.parseInt(token.nextToken()); sort(data, length); //Let's make the output string StringBuffer output = new StringBuffer(); for(int j = 0; j < length; j++) { output.append(data[j]); output.append(" "); } outputText.setText(output.toString()); } }); }
private void sort(int[] a, int len) { int i, j, temp; for(i = 0; i < len-1; i++) for(j = i+1; j < len; j++) if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } }
class InputFrame extends JFrame { InputFrame() { setTitle("Input Test"); setSize(300, 200);
getContentPane().add(new InputPanel()); } }
public class Test { public static void main(String[] args) { InputFrame frame = new InputFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } }