import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;public class Calculator extends Applet implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
double x, y = 0;
int f = 0;
String s= "";
JTextField tf1,tf2;
JButton b1,b2,b3,b4,b5,b6;
JButton b[] = new JButton[11]; public void init(){
setLayout(null);
p1.setBounds(1,2,500,100);
p2.setBounds(1,102,500,375);
p1.setLayout(new GridLayout(2,1));
p2.setLayout(new GridLayout(4,5));
tf1 = new JTextField(20);
tf2 = new JTextField(20);
tf1.setEditable(false);
tf2.setEditable(false);
add(p1);
add(p2);
p1.add(tf1);
p1.add(tf2);
for(int i=0; i<10; i++){
String s1=""+i;
b[i] = new JButton(s1);
p2.add(b[i]);
b[i].addActionListener(this);
}
b[10] = new JButton(".");
p2.add(b[10]);
b[10].addActionListener(this);
b1 = new JButton("+");
b2 = new JButton("-");
b3 = new JButton("*");
b4 = new JButton("/");
b5 = new JButton("=");
b6 = new JButton("C");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
s = s+ e.getActionCommand();
for(int i=0; i<11; i++){
if(e.getSource() == b[i]){
tf1.setText(tf1.getText() + e.getActionCommand());
}
if(e.getSource() == b6){
tf1.setText("");
tf2.setText("");
x=0;y=0;f=0;s="";
}
if(e.getSource() == b1){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
tf2.setText(""+(y+x));
y=y+x;f=1;
}
if(e.getSource() == b2){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y-x));y=y-x;}
f = 2;
}
if(e.getSource() == b3){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y*x));y=y*x;}
f = 3;
}
if(e.getSource() == b4){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y/x));y=y/x;}
f = 4;
}
if(e.getSource() == b5){
x=Double.parseDouble(tf1.getText());
if(f == 1) tf1.setText(""+(y + x));
if(f == 2) tf1.setText(""+(y - x));
if(f == 3) tf1.setText(""+(y * x));
if(f == 4) tf1.setText(""+(y / x));
y=0;
tf2.setText(s + tf1.getText());
}
}
}
}
运行结果只能加减,乘的话结果都是0.0,除的话结果是NaN,请问怎么回事
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;public class Calculator extends Applet implements ActionListener{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
double x, y = 0;
int f = 0;
String s= "";
JTextField tf1,tf2;
JButton b1,b2,b3,b4,b5,b6;
JButton b[] = new JButton[11]; public void init(){
setLayout(null);
p1.setBounds(1,2,500,100);
p2.setBounds(1,102,500,375);
p1.setLayout(new GridLayout(2,1));
p2.setLayout(new GridLayout(4,5));
tf1 = new JTextField(20);
tf2 = new JTextField(20);
tf1.setEditable(false);
tf2.setEditable(false);
add(p1);
add(p2);
p1.add(tf1);
p1.add(tf2);
for(int i=0; i<10; i++){
String s1=""+i;
b[i] = new JButton(s1);
p2.add(b[i]);
b[i].addActionListener(this);
}
b[10] = new JButton(".");
p2.add(b[10]);
b[10].addActionListener(this);
b1 = new JButton("+");
b2 = new JButton("-");
b3 = new JButton("*");
b4 = new JButton("/");
b5 = new JButton("=");
b6 = new JButton("C");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
s = s+ e.getActionCommand();
for(int i=0; i<11; i++){
if(e.getSource() == b[i]){
tf1.setText(tf1.getText() + e.getActionCommand());
}
if(e.getSource() == b6){
tf1.setText("");
tf2.setText("");
x=0;y=0;f=0;s="";
}
if(e.getSource() == b1){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
tf2.setText(""+(y+x));
y=y+x;f=1;
}
if(e.getSource() == b2){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y-x));y=y-x;}
f = 2;
}
if(e.getSource() == b3){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y*x));y=y*x;}
f = 3;
}
if(e.getSource() == b4){
x=Double.parseDouble(tf1.getText());
tf1.setText("");
if(y==0){tf2.setText(""+x);y=x;}
else{tf2.setText(""+(y/x));y=y/x;}
f = 4;
}
if(e.getSource() == b5){
x=Double.parseDouble(tf1.getText());
if(f == 1) tf1.setText(""+(y + x));
if(f == 2) tf1.setText(""+(y - x));
if(f == 3) tf1.setText(""+(y * x));
if(f == 4) tf1.setText(""+(y / x));
y=0;
tf2.setText(s + tf1.getText());
}
}
}
}
运行结果只能加减,乘的话结果都是0.0,除的话结果是NaN,请问怎么回事
解决方案 »
- tomcat启动后不停的报An attempt was made to authenticate the locked user "manager"
- 我在SSH2配置了两个applicationContext.xml连接两个数据库
- JDBC连接mysql问题
- 怎么做一个小的应用程序
- 关于struts2.1.6的模型驱动问题,满意后立即给分
- jsp+servlet+javabean(注册新用户)怎么写?
- JSP初学者 疑问,搞郁闷了!
- jsp中操作xml运行不了??
- 用javaMail库发不出去邮件! 高人帮忙看一下,急!
- JDBC连接远程SQL出错,请大哥大姐们帮忙啊!!
- 高手指点在aop处理类中为什么MessageContext对象为空
- 在线等,各位高手请帮忙,JavaEE web工程读取线程进度,并显示进度条
if(e.getSource() == b5){
x=Double.parseDouble(tf1.getText());
if(f == 1) tf1.setText(""+(y + x));
if(f == 2) tf1.setText(""+(y - x));
if(f == 3) tf1.setText(""+(y * x));
if(f == 4) tf1.setText(""+(y / x));
y=0;
tf2.setText(s + tf1.getText());中的y=0注释掉就能实现乘除运算,可是加减出现错误,参数设置的有问题