真是乱七八糟^_^^_^!首先比较那个事件源触发的ActionEvent,可以使用e.getSource();不过要强制转换成相应的对象,如是Button事件,就用(Button)e.getSource();
使用e.getActionCommand()返回组件上面的title,然后比较可以确定是那个事件源!其次字符串转换为相应的基本类型,如int , float!如下:
Integer.parseInt( s );将字符串s转换为int
Float.parseFloat( s );将字符串转换为float
使用e.getActionCommand()返回组件上面的title,然后比较可以确定是那个事件源!其次字符串转换为相应的基本类型,如int , float!如下:
Integer.parseInt( s );将字符串s转换为int
Float.parseFloat( s );将字符串转换为float
解决方案 »
- 很基础的问题,但不是每个人都能回答出来。
- 对中文分词程序运行错误的疑惑,希望能解决
- 两道Java题,五个问题
- Iterator是如何获得对象类型的
- socket问题?
- 在JCREATOR中编译通过,却不能够调试 ,出错:Exception occurred: java.lang.ClassNotFoundException
- 关于树的代码问题
- JBoss3.0 with Tomcat4.0.3怎么用?
- 为什么转义字符“\n”写到文本文件中是一个黑方块?而不是换行?不解!!!,应怎么写才是换行?
- 请教一个有关weblogic的问题!
- 关于String类的小问题!
- java 连接oracle8i 数据库问题。急~~~~~~~~~~~~~~
e.getSource() == pingjun
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;public class SimpleArithmetic extends Applet implements ActionListener
{
Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
Label output;
Button jia;
Button jian;
Button cheng;
Button chu;
Button zuidazhi;
Button pingjun;
public void init()
{
prompt1 = new Label("请输入一个整数:");
input1 = new TextField(4);
prompt2 = new Label("请输入一个浮点数:");
input2 = new TextField(6);
output = new Label("运算结果 "); //创建output并初始化
jia = new Button("加"); //创建jia并初始化,以下类似
jian = new Button("减");
cheng = new Button("乘");
chu = new Button("除");
zuidazhi = new Button("最大值");;
pingjun = new Button("平均值");;
add(prompt1);
add(input1);
add(prompt2);
add(input2);
add(output); //输出标签添加到Applet,接下来的也是一样
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
zuidazhi.addActionListener(this);
pingjun.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jia) //target改为getSource()
output.setText("相加值为:"+(Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()).floatValue())); else if(e.getSource()==jian)
output.setText("相减值为:"+(Integer.parseInt(input1.getText())-Float.valueOf(input2.getText()).floatValue())); //浮点数还要用floatValue()方法转换,以下类似。并去掉e.toString(),因为没用
else if(e.getSource()==cheng)
output.setText("相乘值为:"+(Integer.parseInt(input1.getText())*Float.valueOf(input2.getText()).floatValue()));
else if(e.getSource()==chu)
output.setText("相除值为:"+(Integer.parseInt(input1.getText())/Float.valueOf(input2.getText()).floatValue()));
else if(e.getSource()==zuidazhi)
output.setText("最大值为:"+Math.max(Integer.parseInt(input1.getText()),Float.valueOf(input2.getText()).floatValue()));
else if(e.getSource()==pingjun)
output.setText("平均值为:"+((Integer.parseInt(input1.getText())+Float.valueOf(input2.getText()).floatValue())/2));
}
}
output = new Label("运算结果 ");
jia = new Button("加");
jian = new Button("减");
cheng = new Button("乘");
chu = new Button("除");
zuidazhi = new Button("最大值");
pingjun = new Button("平均值");
并将其加入到Applet,使用如下语句:
add(output);
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
最后还有在转换浮点数的地方的valueOf()方法后加floatValue()方法,并且将e.toString()去掉,刚才的程序中有两句多了个分号:
zuidazhi = new Button("最大值");;
pingjun = new Button("平均值");;
去掉一个就行了。
import java.awt.*;
import java.awt.event.*;public class SimpleArithmetic extends Applet implements ActionListener
{
Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
Label output;
Button jia;
Button jian;
Button cheng;
Button chu;
Button zuidazhi;
Button pingjun;
public void init()
{
prompt1=new Label("请输入一个整数:");
input1=new TextField(4);
prompt2=new Label("请输入一个浮点数:");
input2=new TextField(6);
jia = new Button("加"); //创建jia并初始化,以下类似
jian = new Button("减");
cheng = new Button("乘");
chu = new Button("除");
zuidazhi = new Button("最大值");;
pingjun = new Button("平均值");;
add(prompt1);
add(input1);
add(prompt2);
add(input2);
add(jia);
add(jian);
add(cheng);
add(chu);
add(zuidazhi);
add(pingjun);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
zuidazhi.addActionListener(this);
pingjun.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jia)
output.setText("相加值为:"+String.valueOf(Integer.parseInt(input1.getText()) +Float.parseFloat(input2.getText()))+e.toString());
...
}
}
谢谢各位
特别是kaidu(Roger)
我参考你的