题目:
编写有两个文本区的小应用程序,当我们在一个文本区中输入若非个数度叶,另一个文本区同时对对你输入的数进行求和运算并救出平均值,也就是说随你输入的变化,加一个文本区不断地更新求和用平均值!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Zuoye extends JFrame implements ActionListener{
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JLabel lbl1;
private JLabel lbl2;
private JLabel lbl3;
private JButton btn1;
private JButton btn2;
private Container cp;
private double d=0;//存放结果
private int n=0;//记录次数
public Zuoye(){
super("zuoye");
setSize(200,200);
// setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text1=new JTextField(5);
text2=new JTextField(5);
text3=new JTextField(5);
lbl1=new JLabel("Data");
lbl2=new JLabel("Total");
lbl3=new JLabel("Average");
btn1=new JButton("add");
btn2=new JButton("exit");
cp=getContentPane();
cp.setLayout(new GridLayout(4,2));
cp.add(lbl1);
cp.add(text1);
cp.add(lbl2);
cp.add(text2);
cp.add(lbl3);
cp.add(text3);
cp.add(btn1);
cp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
double a=0;
if(e.getSource()==btn1){
//
//int n=0;
a=Double.parseDouble(text1.getText());
d+=a;
n++;
text2.setText(String.valueOf(d));
if(n!=0)
text3.setText(String.valueOf(d/n));
}
/*if(e.getSource()==btn2)
{n=0;
//dispose();
}*/
else dispose();
}
public static void main(String[] args){
Zuoye zuoye=new Zuoye();
zuoye.setVisible(true);
}
}
我写了,不过还是有问题呀,得不到结果呀!
各位大虾看看,帮一下!
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
public class TextExample extends Applet implements TextListener
{
TextArea text1,text2;
public void init()
{
text1=new TextArea(6,15);
text2=new TextArea(6,15);
add(text1);
add(text2);
text2.setEditable(false);
text1.addTextListener(this);
}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==text1)
{
int sum=0,avg=0;
String num=text1.getText();
StringTokenizer getNum=new StringTokenizer(num,",'\n'");
int n=getNum.countTokens();
for(int i=0;i<n-1;i++)
{
String temp=getNum.nextToken();
sum+=Integer.parseInt(temp);
text2.setText(null);
text2.append("sum="+sum);
}
}
}
}
这是我运行得结果的程序,不过还有好多的问题!
如我只想让用户输入的内容只能为数字,输入其他字符就提示输入错误!那应该怎样改进?
望大虾指点一二,谢谢了!
源码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;
public class TextExample extends Applet implements TextListener
{
TextArea text1,text2;
public void init()
{
text1=new TextArea(10,20);
text2=new TextArea(10,20);
add(text1);
add(text2);
text2.setEditable(false);
text1.addTextListener(this);
}
public void textValueChanged(TextEvent e)
{
if(e.getSource()==text1)
{
double sum=0.0,avg=0.0;
String num=text1.getText();
StringTokenizer getNum=new StringTokenizer(num," ,'\n'");
int n=getNum.countTokens();
for(int i=0;i<=n-1;i++)
{
String temp=getNum.nextToken();
sum+=Double.parseDouble(temp);
}
avg=sum/n;
text2.setText(null);
text2.append("您输入的数值总和为:"+sum+"\n平均值为:"+avg);
}
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;public class TextDemo extends JPanel implements KeyListener {
private JTextField inputText,outputText;
TextDemo() {
inputText = new JTextField(10);
outputText = new JTextField(20);
outputText.setEditable(false);
add(inputText);
add(outputText);
inputText.addKeyListener(this);
}/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame(\"TextDemo\");
f.getContentPane().add(new TextDemo());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,200);
f.setVisible(true);
}public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
Integer index = 0;
Integer totelNumber = 0;
double averageNumer = 0.0;
try {
String[] allNumberText = inputText.getText().split(\",\");
for(String s:allNumberText) {
Integer number = Integer.valueOf(s);
if(number >= 10) {
totelNumber += number;
index++;
}
}
if(index > 0)
averageNumer = totelNumber / index;
outputText.setText(\"总合为: \" + totelNumber + \"平均值为: \" + averageNumer);
}
catch(NumberFormatException e) {
outputText.setText(\"请输入数字并以','分隔\");
}
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextDemo extends JPanel implements KeyListener {
private JTextField inputText,outputText;
int i=0;
TextDemo() {
inputText = new JTextField(10);
outputText = new JTextField(20);
outputText.setEditable(false);
add(inputText);
add(outputText);
inputText.addKeyListener(this);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame f = new JFrame("TextDemo");
f.getContentPane().add(new TextDemo());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400,200);
f.setVisible(true);
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
int index = 0;
int totelNumber = 0;
double averageNumer = 0.0;
try {
String[] s = inputText.getText().split(",");
for(i=0;i<s.length;i++) {
int number = Integer.parseInt(s[i]);
totelNumber += number;
index++;
}
if(index > 0)
averageNumer = totelNumber / index;
outputText.setText("总合为: " + totelNumber + "平均值为: " + averageNumer);
}
catch(NumberFormatException e) {
outputText.setText("请输入数字并以','分隔");
}
}
}
8楼的有问题 我修改了以下