实在是太乱了,抄的吧,抄都没抄对,t1写成ti,==
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ArrayDemo extends Frame implements
ActionListener {
TextField t1 = new TextField(40);
TextField t2 = new TextField(40);
Button b1 = new Button("排序并求和");
int a[];
public ArrayDemo() {
setLayout(new FlowLayout(FlowLayout.LEFT));
add(t1);
add(t2);
add(b1);
a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = (int) (Math.random() * 100);
if (i == 0) {
t1.setText(" " + a[i]);
} else {
t1.setText(t1.getText() + "," + a[i]);
}
}
b1.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
Graphics s = this.getGraphics();
s.drawString("sum=" + getSum(), 100, 100);
}
public static void main(String args[]) {
ArrayDemo maimFrame = new ArrayDemo();
maimFrame.setSize(400, 300);
maimFrame.setTitle("pang");
maimFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
sort();
t2.setText(" ");
for (int i = 0; i <= a.length; i++) {
if (i == 0) {
t2.setText(" " + a[i]);
} else {
t2.setText(t2.getText() + "," + a[i]);
}
}
}
long getSum() {
long s = 0;
for (int i = 0; i < a.length; i++) {
s = s + a[i];
}
return s;
}
void sort() {
int t;
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
}