首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请各位帮帮忙看看自己编写的一个计算器的JAVA小程序解决思路

2012-02-05 
请各位帮帮忙看看自己编写的一个计算器的JAVA小程序package whhimport java.awt.*import java.awt.event

请各位帮帮忙看看自己编写的一个计算器的JAVA小程序
package whh;
import java.awt.*;
import java.awt.event.*;

class EventTest extends Frame implements ActionListener,ItemListener
{
Button btn1[]=new Button[16];
Button btn2[]=new Button[16];
TextField text;
String a1="",a2="",b1="",b2="",b3="",x;
CheckboxGroup cbg=new CheckboxGroup();
Checkbox c1,c2,c3;
EventTest()
  {
setTitle("计算器");
setLayout(null);
setSize(500,250);
setLocation(400,200);
this.setResizable(false);
text=new TextField("0",6);
text.setEditable(false);
Panel pnl=new Panel(new GridLayout(4,4,7,7));
Panel pnl2=new Panel(new GridLayout(1,4,7,7));
Panel pnl3=new Panel(new GridLayout(1,4,7,7));
Panel pnl4=new Panel(new GridLayout(1,4,7,7));
Panel pnl5=new Panel(new GridLayout(1,4,7,7));
Panel pnl6=new Panel(new GridLayout(4,4,7,7));
for(int i=0;i<=9;i++)
{
btn1[i]=new Button(String.valueOf(i));
}
 
btn1[10]=new Button("+");
  btn1[10].setForeground(Color.red);
btn1[11]=new Button("-");
btn1[11].setForeground(Color.red);
btn1[12]=new Button("*");
btn1[12].setForeground(Color.red);
btn1[13]=new Button("/");
btn1[13].setForeground(Color.red);
btn1[14]=new Button("C");
btn1[14].setForeground(Color.red);
btn1[15]=new Button("=");
btn1[15].setForeground(Color.red);
btn2[0]=new Button("Sin");
btn2[1]=new Button("And");
btn2[2]=new Button("(");
btn2[3]=new Button(")");
btn2[4]=new Button("Cos");
btn2[5]=new Button("Or");
btn2[6]=new Button("Exp");
btn2[7]=new Button("Ln");
btn2[8]=new Button("Tan");
btn2[9]=new Button("Xo");
btn2[10]=new Button("x*y");
btn2[11]=new Button("Log");
btn2[12]=new Button("e");
btn2[13]=new Button("Not");
btn2[14]=new Button("x*3");
btn2[15]=new Button("n!");
for(int i=0;i<=15;i++)
{
btn2[i].disable();
}

c1=new Checkbox("十进制",cbg,true);
c2=new Checkbox("八进制",cbg,false);
c3=new Checkbox("二进制",cbg,false);
this.setLayout(null);
pnl.setBounds(260,90,240,160);
pnl2.setBounds(0, 70, 240, 20);
pnl3.setBounds(260, 70, 240, 20);
pnl4.setBounds(0, 30, 500, 20);
pnl5.setBounds(50, 50, 400, 20);
pnl6.setBounds(0,90,240,160);
 
for (int i=0;i<=15;i++)
{
pnl.add(btn1[i]);
}

pnl2.add(c1);
pnl2.add(c2);
pnl2.add(c3);
pnl3.add(new Checkbox("Inv"));
pnl3.add(new Checkbox("Hyp"));
pnl5.add(text);
  for(int i=0;i<=15;i++)
  {
  pnl6.add(btn2[i]);
  }

this.add(pnl);
this.add(pnl2);
this.add(pnl3);
this.add(pnl4);
this.add(pnl5);
this.add(pnl6);

for(int i=0;i<=15;i++)
{
btn1[i].addActionListener(this);// 注册监听器
}

c1.addItemListener(this); 
c2.addItemListener(this); 
c3.addItemListener(this); 
 
//关闭窗口
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
  }
public void itemStateChanged(ItemEvent e)
{
if(c1.getState()==true)  
{
for(int i=0;i<=9;i++)


{
btn1[i].enable();
}
}
if(c2.getState()==true)  
{
btn1[8].disable();
btn1[9].disable();
}
if(c3.getState()==true)  
{
for(int i=2;i<=9;i++)
{
btn1[i].disable();
}
}
}
   
  public void actionPerformed(ActionEvent e)
  {
  //取得事件源对象
  Button btn=(Button) e.getSource(); 
   
  if(btn==btn1[0]) // 如果是按下btn1按钮  
  {
  a1=btn1[0].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[1])
  {
  a1=btn1[1].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[2])  
  {
  a1=btn1[2].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[3])  
  {
  a1=btn1[3].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[4])  
  {
  a1=btn1[4].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[5])  
  {
  a1=btn1[5].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[6])  
  {
  a1=btn1[6].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[7])  
  {
  a1=btn1[7].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[8])  
  {
  a1=btn1[8].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[9])  
  {
  a1=btn1[9].getLabel();
  b1=b1+a1;
  text.setText(b1);
   
  }
  if(btn==btn1[10])  
  {
  b3=text.getText();
  b1="";
  x=btn1[10].getLabel();
 
  }
  if(btn==btn1[11])  
  {
  b3=text.getText();
  b1="";
  x=btn1[11].getLabel();
  }
  if(btn==btn1[12])  
  {
  b3=text.getText();
  b1="";
  x=btn1[12].getLabel();
  }
  if(btn==btn1[13])  
  {
  b3=text.getText();
  b1="";
  x=btn1[13].getLabel();
  }
  if(btn==btn1[14])  
  {
  b1="";
  text.setText("0");

  }
  if(btn==btn1[15])  
  {
  b2=text.getText();
  b1="";
  if(x=="+")


  {
  float c = Float.parseFloat(b3)+ Float.parseFloat(b2);
text.setText(String.valueOf(c));
  }
  if(x=="-")
  {
float c = Float.parseFloat(b3)- Float.parseFloat(b2);
text.setText(String.valueOf(c));
  }
  if(x=="*")
  {
float c = Float.parseFloat(b3)* Float.parseFloat(b2);
text.setText(String.valueOf(c));
  }
  if(x=="/")
  {
float c = Float.parseFloat(b3)/Float.parseFloat(b2);
text.setText(String.valueOf(c));
  }
 
  }
 
 
 
  }
   
  public static void main(String args[])
{  
EventTest frm=new EventTest(); 
 
}  
}



代码如上 就是我再按一下“=”这个按钮的时候 的出的结果不对 其他运算也一样只能运算一次不能再一次运算以后 连续按等号
帮帮修改一下 
或者 有什么算法说一下 最好详细一点 
谢谢了 
还有 什么不好的地方请指出 
不胜感激涕零啊

[解决办法]

没有什么问题啊..不知道是不是我理解错了.
比如2+3 按第一次"=" 时结果为"5",再接着
按"="结果依次为:7,9,11,13,15....
这没有什么不对啊.就是平时用的计算器也是这样的了.
就连微软自带的计算器也是这样.
只是有点不同,如:2+3=5,再按"="时.依次为:5,8,11,14...
交换了一个而矣.
[解决办法]
我给你一个你自己看看好吗?
public class Calculator{ 
private String result = "0"; 
private int op = 0,add = 1,sub = 2,mul = 3,div = 4; 

private double stringToDouble(String x){ 
double y = Double.parseDouble(x); 
return y; 

private void operate(String x){ 
double x1 = stringToDouble(x); 
double y = stringToDouble(result); 
switch (op){ 
case 0: 
result = x; 
break; 
case 1: 
result = String.valueOf(y+x1); 
break; 
case 2: 
result = String.valueOf(y-x1); 
break; 
case 3: 
result = String.valueOf(y*x1); 
break; 
case 4: 
if(x1!=0){ 
result = String.valueOf(y/x1); 
}else{ 
result = "The divisor can't be zero!"; 

break; 



public String opAdd(String x){ 
operate(x); 
op = add; 
return result; 

public String opSubtract(String x){ 
operate(x); 
op = sub; 
return result; 

public String opMultiply(String x){ 
operate(x); 
op = mul; 
return result; 

public String opDivide(String x){ 
operate(x); 
op = div; 
return result; 

public String opEquals(String x){ 
operate(x); 
op = 0; 
return result; 

public void opClean(){ 
op = 0; 
result = "0"; 

}
//////////CalculatorGUI///////////////////////
import java.awt.*; 
import java.awt.event.*; 
import java.util.EventObject; 

public class CalculatorGUI{ 
private Frame f; 
private Panel p1,p2; 
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; 
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal; 
private TextField tf; 
private String s,op; 
private Calculator cal = new Calculator(); 
private boolean ifOp; 

public CalculatorGUI(){ 


f = new Frame("Calculator"); 
p1 = new Panel(); 
p2 = new Panel(); 

b0 = new Button("0"); 
b1 = new Button("1"); 
b2 = new Button("2"); 
b3 = new Button("3"); 
b4 = new Button("4"); 
b5 = new Button("5"); 
b6 = new Button("6"); 
b7 = new Button("7"); 
b8 = new Button("8"); 
b9 = new Button("9"); 
bPoint = new Button("."); 
bAdd = new Button("+"); 
bDec = new Button("-"); 
bMul = new Button("*"); 
bDiv = new Button("/"); 
bCal = new Button("="); 

tf = new TextField(25); 
tf.setEditable(false); 




public void launchFrame(){ 
f.setSize(220,160); 
f.setResizable(false); 
f.addWindowListener(new myWindowListener()); 
p1.setLayout(new FlowLayout(FlowLayout.CENTER)); 
p1.add(tf); 
f.add(p1,BorderLayout.NORTH); 
p2.setLayout(new GridLayout(4,4)); 

b0.addActionListener(new setLabelText_ActionListener()); 
b1.addActionListener(new setLabelText_ActionListener()); 
b2.addActionListener(new setLabelText_ActionListener()); 
b3.addActionListener(new setLabelText_ActionListener()); 
b4.addActionListener(new setLabelText_ActionListener()); 
b5.addActionListener(new setLabelText_ActionListener()); 
b6.addActionListener(new setLabelText_ActionListener()); 
b7.addActionListener(new setLabelText_ActionListener()); 
b8.addActionListener(new setLabelText_ActionListener()); 
b9.addActionListener(new setLabelText_ActionListener()); 
bPoint.addActionListener(new setLabelText_ActionListener()); 
bAdd.addActionListener(new setOperator_ActionListener()); 
bDec.addActionListener(new setOperator_ActionListener()); 
bMul.addActionListener(new setOperator_ActionListener()); 
bDiv.addActionListener(new setOperator_ActionListener()); 
bCal.addActionListener(new setOperator_ActionListener()); 

p2.add(b7); 
p2.add(b8); 
p2.add(b9); 
p2.add(bAdd); 
p2.add(b4); 
p2.add(b5); 
p2.add(b6); 
p2.add(bDec); 
p2.add(b1); 
p2.add(b2); 
p2.add(b3); 
p2.add(bMul); 
p2.add(b0); 
p2.add(bPoint); 
p2.add(bCal); 
p2.add(bDiv); 
f.add(p2,BorderLayout.SOUTH); 
f.setVisible(true); 


public void setTextFieldText_Temp(){ 
if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){ 
tf.setText(tf.getText()+s); 
}else{ 
tf.setText((tf.getText()+s).substring(0,15)); 


public void setTextFieldText(){ 
if(ifOp){ 
ifOp = false; 
tf.setText(""); 
setTextFieldText_Temp(); 
}else{ 
setTextFieldText_Temp(); 



public static void main(String[] args){ 
CalculatorGUI calculator = new CalculatorGUI(); 
calculator.launchFrame(); 


class myWindowListener extends WindowAdapter{ 
public void windowClosing(WindowEvent e){ 
System.exit(0); 



class setLabelText_ActionListener implements ActionListener{ 
public void actionPerformed(ActionEvent e){ 
Button tempB = (Button)e.getSource(); 
s = tempB.getLabel(); 
setTextFieldText(); 



class setOperator_ActionListener implements ActionListener{ 
public void actionPerformed(ActionEvent e){ 
Button tempB = (Button)e.getSource(); 
op = tempB.getLabel(); 
if(op.equals("+")){ 
tf.setText(cal.opAdd(tf.getText())); 
ifOp = true; 
}else if(op.equals("-")){ 
tf.setText(cal.opSubtract(tf.getText())); 
ifOp = true; 
}else if(op.equals("*")){ 
tf.setText(cal.opMultiply(tf.getText())); 
ifOp = true; 
}else if(op.equals("/")){ 
tf.setText(cal.opDivide(tf.getText())); 
ifOp = true; 
}else if(op.equals("=")){ 
tf.setText(cal.opEquals(tf.getText())); 
ifOp = true; 



}
我自己从前搞的,基本上的是O(n)
[解决办法]
对不起,发错了
if(btn==btn1[15])
{

b2=text.getText();
b1="";
if(x=="+")
{
double c = Double.parseDouble(b3)+ Double.parseDouble(b2);
text.setText(String.valueOf(c));
}
if(x=="-")
{
double c = Double.parseDouble(b3)-Double.parseDouble(b2);
text.setText(String.valueOf(c));
}
if(x=="*")
{
double c = Double.parseDouble(b3)*Double.parseDouble(b2);
text.setText(String.valueOf(c));
}
if(x=="/")
{
double c = Double.parseDouble(b3)/Double.parseDouble(b2);
text.setText(String.valueOf(c));
}
if(x=="=")
{
text.setText(b2);
}
x=btn1[15].getLabel();
}

热点排行