几段问题代码,请求仙人指点 本帖最后由 woshiliupingping 于 2010-08-02 14:31:44 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先第一个分析的题是有问题的,你用TextListener是不行的,这样每次你输入一个东西,不管是符号还是数字它都会有反应,这样就起不到你分析的效果。我改了一下,你可以看一下,如下:import java.awt.*;import java.awt.event.*;import java.util.*;public class love { public static void main(String args[]) { WindowFrame s = new WindowFrame("计算的窗口"); }}class WindowFrame extends Frame implements TextListener, ActionListener { float aver, sum = 0; TextArea a, b; Button button1,button2; WindowFrame(String s) { super(s); a = new TextArea(15, 16); b = new TextArea(15, 45); button1 = new Button("退出"); button2 = new Button("分析"); add(button1); add(button2); setLayout(new FlowLayout()); add(a); add(b); button1.addActionListener(this); button2.addActionListener(this); //a.addTextListener(this); setVisible(true); validate(); setBounds(0, 0, 345, 600); } public void actionPerformed(ActionEvent e) { if(e.getSource().equals(button2)){ try { String s; s = a.getText(); sum = 0; StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'"); int n = fenxi.countTokens(); String c[] = new String[n]; for (int i = 0; i <= n - 1; i++) { String temp = fenxi.nextToken(); System.out.println(temp); c[i] = temp; } for (int j = 0; j < n; j++) { sum = sum + Float.parseFloat(c[j]); aver = sum / n; b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver); } } catch (NumberFormatException ee) { } } else if (e.getSource().equals(button1)){ System.exit(0); } } public void textValueChanged(TextEvent e) { try { String s; s = a.getText(); StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'"); int n = fenxi.countTokens(); String c[] = new String[n]; for (int i = 0; i <= n - 1; i++) { String temp = fenxi.nextToken(); System.out.println(temp); c[i] = temp; } for (int j = 0; j < n; j++) { sum = sum + Float.parseFloat(c[j]); aver = sum / n; b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver); } } catch (NumberFormatException ee) { } }} 怎么java编程实现网络邻居的功能 请教一个问题 关于getDelaredMethod()方法的问题;答完结贴;谢谢了; 静态变量的问题!急.... 请问如何获取一个包里的所有类的名称列表? java代码 问题请求解析! java rmi 小问题请教大神 Jbuilder编译时的问题,不明白!!! 关于radio选中的问题(javascript) 学了个很简单的加密,但是解密不会写... weblogic10获取数据库连接池问题 分解String时,split和Scanner的区别,我不是特别明白。
我改了一下,你可以看一下,如下:import java.awt.*;
import java.awt.event.*;
import java.util.*;public class love {
public static void main(String args[]) {
WindowFrame s = new WindowFrame("计算的窗口");
}
}class WindowFrame extends Frame implements TextListener, ActionListener {
float aver, sum = 0;
TextArea a, b;
Button button1,button2; WindowFrame(String s) {
super(s);
a = new TextArea(15, 16);
b = new TextArea(15, 45);
button1 = new Button("退出");
button2 = new Button("分析");
add(button1);
add(button2);
setLayout(new FlowLayout());
add(a);
add(b);
button1.addActionListener(this);
button2.addActionListener(this);
//a.addTextListener(this);
setVisible(true);
validate();
setBounds(0, 0, 345, 600);
} public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button2)){
try {
String s;
s = a.getText();
sum = 0;
StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
int n = fenxi.countTokens();
String c[] = new String[n];
for (int i = 0; i <= n - 1; i++) {
String temp = fenxi.nextToken();
System.out.println(temp);
c[i] = temp;
}
for (int j = 0; j < n; j++) {
sum = sum + Float.parseFloat(c[j]);
aver = sum / n;
b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
}
}
catch (NumberFormatException ee) {
}
}
else if (e.getSource().equals(button1)){
System.exit(0);
}
} public void textValueChanged(TextEvent e) {
try {
String s;
s = a.getText();
StringTokenizer fenxi = new StringTokenizer(s, " ,'\n'");
int n = fenxi.countTokens();
String c[] = new String[n];
for (int i = 0; i <= n - 1; i++) {
String temp = fenxi.nextToken();
System.out.println(temp);
c[i] = temp;
}
for (int j = 0; j < n; j++) {
sum = sum + Float.parseFloat(c[j]);
aver = sum / n;
b.setText("你输入的数的和sum=" + sum + '\n' + "你输入的数的平均数aver=" + aver);
}
} catch (NumberFormatException ee) {
}
}
}