首页 新闻 会员 周边

计算器 的代码

0
[待解决问题]

求一个   计算器 的代码   谢谢各位大神

扯-淡的主页 扯-淡 | 菜鸟二级 | 园豆:202
提问于:2015-07-26 13:35
< >
分享
所有回答(2)
0

import java.awt.*;
import java.awt.event.*;

public class Jisuanqi extends WindowAdapter {
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
TextField txt;
private Button[] b = new Button[17];
private String ss[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2",
   "3", "*", "清空", "0", "=", "/", "关闭" };
static double a;
static String s, str;//定义变量 创建对像

public static void main(String args[]) {
  (new Jisuanqi()).frame();
}

public void frame() {
  Frame fm = new Frame("计算器");
  for (int i = 0; i <= 16; i++) {
   b[i] = new Button(ss[i]);
  }                             
  for (int i = 0; i <= 15; i++) {
   p2.add(b[i]);
  }                               //创建按钮 并添加到P2
  b[16].setBackground(Color.WHITE);
  txt = new TextField(15);
  txt.setEditable(false);
  for (int i = 0; i <= 16; i++) {
   b[i].addActionListener(new buttonlistener());//添加监听器
  }
  b[16].addActionListener(new close());
  fm.addWindowListener(this);
  fm.setBackground(Color.red);
  p1.setLayout(new BorderLayout());
  p1.add(txt, "North");
  p2.setLayout(new GridLayout(4,4));
  p3.setLayout(new BorderLayout());
  p3.add(b[16]);
  fm.add(p1, "North");
  fm.add(p2, "Center");
  fm.add(p3, "South");
  fm.pack();
  fm.setVisible(true);//都是些窗中设置 添加相关组件和监听器
}

public void windowClosing(WindowEvent e) {
  System.exit(0);//退出系统
}

class buttonlistener implements ActionListener {//编写监听器事件 通过按键得出给果
  public void actionPerformed(ActionEvent e) {
   Button btn = (Button) e.getSource();
   if (btn.getLabel() == "=") {
    jisuan();
    str = String.valueOf(a);
    txt.setText(str);
    s = "";
   } else if (btn.getLabel() == "+") {
    jisuan();
    txt.setText("");
    s = "+";
   } else if (btn.getLabel() == "-") {
    jisuan();
    txt.setText("");
    s = "-";
   } else if (btn.getLabel() == "/") {
    jisuan();
    txt.setText("");
    s = "/";

   } else if (btn.getLabel() == "*") {
    jisuan();
    txt.setText("");
    s = "*";
   } else {
    txt.setText(txt.getText() + btn.getLabel());

    if (btn.getLabel() == "清空")
     txt.setText("");
   }
  }

  public void jisuan() {//编写具体计算方法
   if (s == "+")
    a += Double.parseDouble(txt.getText());
   else if (s == "-")
    a -= Double.parseDouble(txt.getText());
   else if (s == "*")
    a *= Double.parseDouble(txt.getText());
   else if (s == "/")
    a /= Double.parseDouble(txt.getText());
   else
    a = Double.parseDouble(txt.getText());
  }
}
}

class close implements ActionListener {//退出
public void actionPerformed(ActionEvent e) {
  System.exit(0);
}
}

这个可以吗

滔天 | 园豆:205 (菜鸟二级) | 2015-07-26 15:33
0

ios的要么?还有点小bug,哈哈

AlexLearnsToCode | 园豆:207 (菜鸟二级) | 2015-07-27 08:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册