import java.awt.*;
import java.awt.event.*;public class Calculator{
public static void main(String[] args){
new MCFrame("Calculator");
}
}class MCFrame extends Frame{
MCFrame(String s){
super(s);
Panel p1 = new Panel();
Panel p2 = new Panel();
TextField tf = new TextField(30);
Button b0 = new Button("0");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button bPoint = new Button(".");
Button bAdd = new Button("+");
Button bDec = new Button("0");
Button bMul = new Button("*");
Button bDiv = new Button("/");
Button bCal = new Button("=");
setSize(150,200);
setResizable(false);
//setLayout(new GridLayout(2,1));
setVisible(true);
//addWindowListener(new myWindowListener());
p1.add(tf,BorderLayout.CENTER);
p2.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
p2.add(b7,15,10);
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);
add(p1,BorderLayout.NORTH);
add(p2); b0.addActionListener(new setLaelText_ActionListener());
b1.addActionListener(new setLaelText_ActionListener());
b2.addActionListener(new setLaelText_ActionListener());
b3.addActionListener(new setLaelText_ActionListener());
b4.addActionListener(new setLaelText_ActionListener());
b5.addActionListener(new setLaelText_ActionListener());
b6.addActionListener(new setLaelText_ActionListener());
b7.addActionListener(new setLaelText_ActionListener());
b8.addActionListener(new setLaelText_ActionListener());
b9.addActionListener(new setLaelText_ActionListener());
bPonit.addActionListener(new setLaelText_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());
}
}class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int n1 = Integer.parseInt(tf.num1.getLabel());
int n2 = Integer.parseInt(tf.num2.getLabel());
}
}

class setOperator_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
TextField tf = new TextField();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.add(tf.getText()));
ifOp = true;
}
else if(op.equals("-")){
tf.setText(cal.subtract(tf.getText()));
ifOp = true;
}
else if(op.equals("*")){
tf.setText(cal.multiply(tf.getText()));
ifOp = true;
}
else if(op.equals("/")){
tf.setText(cal.divide(tf.getText()));
ifOp = true;
}
else if(op.equals("=")){
tf.setText(cal.Equals(tf.getText()));
ifOp = true;
}
}

private void operate(String x){
double x1 = stringToDouble(x);
double y = stringToDouble(output);
switch(op){
case 0:
output = x;
break;
case 1:
output = String.valueOf(y + x1);
break;
case 2:
output = String.valueOf(y - x1);
break;
case 3:
output = String.valueOf(y * x1);
break;
case 4:
if(x1 != 0){
output = String.valueOf(y / x1);
}
else{
output = "被除数不能为0";
}
break;
}
}

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

public String subtract(String x){
operate(x);
op = sub;
return output;
}
public String multiply(String x){
operate(x);
op = mul;
return output;
}
public String divide(String x){
operate(x);
op = div;
return outpur;
}
public String Equals(String x){
operate(x);
op = 0;
return output;
}
public String opClean(){
op = 0;
output = "0";
}
}这是我自己写的一个计算器的小程序,框架的部分我已经调试通过了,现在的困难就在于怎么完成计算的实现部分。我初步的想法是把得到按钮的标签,根据标签的不同内容,实现不同的功能 ,但是具体的实现不知道怎么写了。。
麻烦知道的高手指点下。。
程序应该不难看懂~~~

解决方案 »

  1.   

    Button tempB = (Button)e.getSource();
    TextField tf = new TextField();
    ===========
    TextField tf = new TextField();
    这个做什么用?其他的感觉你的思路已经出来了,没什么问题样的,你的结果是怎样的?
      

  2.   

    在写个 计算类 return a * b ;return a/ b ;return a + b ;return a - b ;
    public方法 调用上面这些get  大概就怎么些吧 我也才学java三个月 GUI 这块可以说几乎没学 0.0
      

  3.   

    我建议你把两个listener的类作为内部类,这样操作起来会方便点
      

  4.   

    一个属性result,存储计算的值,
    一个属性method,存储输入的符号,
    一个布尔值flag,记录是否要清空每次输入数字,先判断flag,如果是要清空就清空再输入,然后把flag设为false,否则接后面输入,
    第一次点击符号的时候,result记录当前text并把反馈,method记录当前符号,flag记录为true再输入文本,然后第二次输入符号,根据前面一个符号计算当前text和result得出新的result并反馈在text上。一直这样.....