找回密码
 注册
搜索
查看: 2458|回复: 0

计算器java代码,简单的图形界面

[复制链接]
发表于 2010-11-26 00:54:39 | 显示全部楼层 |阅读模式
能实现二进制,八进制,十六进制的转换
  1. import java.awt.*;
  2. import java.awt.event.*;

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

  13. public static void main(String args[]) {
  14.   (new jisuanqi()).frame();
  15. }

  16. public void frame() {
  17.   Frame fm = new Frame("简单计算器");
  18.   for (int i = 0; i <= 16; i++) {
  19.    b[i] = new Button(ss[i]);
  20.   }                              
  21.   for (int i = 0; i <= 15; i++) {
  22.    p2.add(b[i]);
  23.   }                               //创建按钮 并添加到P2
  24.   b[16].setBackground(Color.yellow);
  25.   txt = new TextField(15);
  26.   txt.setEditable(false);
  27.   for (int i = 0; i <= 16; i++) {
  28.    b[i].addActionListener(new buttonlistener());//添加监听器
  29.   }
  30.   b[16].addActionListener(new close());
  31.   fm.addWindowListener(this);
  32.   fm.setBackground(Color.red);
  33.   p1.setLayout(new BorderLayout());
  34.   p1.add(txt, "North");
  35.   p2.setLayout(new GridLayout(4, 4));
  36.   p3.setLayout(new BorderLayout());
  37.   p3.add(b[16]);
  38.   fm.add(p1, "North");
  39.   fm.add(p2, "Center");
  40.   fm.add(p3, "South");
  41.   fm.pack();
  42.   fm.setVisible(true);//都是些窗中设置 添加相关组件和监听器
  43. }

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

  47. class buttonlistener implements ActionListener {//编写监听器事件 通过按键得出给果
  48.   public void actionPerformed(ActionEvent e) {
  49.    Button btn = (Button) e.getSource();
  50.    if (btn.getLabel() == "=") {
  51.     jisuan();
  52.     str = String.valueOf(a);
  53.     txt.setText(str);
  54.     s = "";
  55.    } else if (btn.getLabel() == "+") {
  56.     jisuan();
  57.     txt.setText("");
  58.     s = "+";
  59.    } else if (btn.getLabel() == "-") {
  60.     jisuan();
  61.     txt.setText("");
  62.     s = "-";
  63.    } else if (btn.getLabel() == "/") {
  64.     jisuan();
  65.     txt.setText("");
  66.     s = "/";

  67.    } else if (btn.getLabel() == "*") {
  68.     jisuan();
  69.     txt.setText("");
  70.     s = "*";
  71.    } else {
  72.     txt.setText(txt.getText() + btn.getLabel());

  73.     if (btn.getLabel() == "清空")
  74.      txt.setText("");
  75.    }
  76.   }

  77.   public void jisuan() {//编写具体计算方法
  78.    if (s == "+")
  79.     a += Double.parseDouble(txt.getText());
  80.    else if (s == "-")
  81.     a -= Double.parseDouble(txt.getText());
  82.    else if (s == "*")
  83.     a *= Double.parseDouble(txt.getText());
  84.    else if (s == "/")
  85.     a /= Double.parseDouble(txt.getText());
  86.    else
  87.     a = Double.parseDouble(txt.getText());
  88.   }
  89. }
  90. }

  91. class close implements ActionListener {//退出
  92. public void actionPerformed(ActionEvent e) {
  93.   System.exit(0);
  94. }
  95. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|QQ客服|联系我们|Archiver|手机版|小黑屋|悉远网络 ( 鄂ICP备09013446号 )

GMT+8, 2024-11-21 20:12 , Processed in 0.031367 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表