六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 169|回复: 0

简易计算器

[复制链接]

升级  10%

15

主题

15

主题

15

主题

秀才

Rank: 2

积分
65
 楼主| 发表于 2013-2-5 02:17:37 | 显示全部楼层 |阅读模式
package org.tarena.day01;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class CalculatorDemo implements ActionListener {    static boolean append = true;    JTextField jt = new JTextField(21);    //简单计算器    public void makeCalculator() {        JFrame jf = new JFrame("CalculatorDemo");        JPanel one = new JPanel();        JPanel two = new JPanel();        String[] str = { "BackS", "CE", "cir", "+/-", "7", "8", "9", "/", "4",                "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };        // 添加一个JPanel 和一个JLabel        one.setLayout(new FlowLayout());        one.add(jt);        two.setLayout(new GridLayout(5, 4));        for (int i = 0; i < 20; i++) {            JButton jbut = new JButton(str[i]);            jbut.addActionListener(this);            two.add(jbut);        }        jf.add(one, BorderLayout.NORTH);        jf.add(two, BorderLayout.CENTER);        jt.setEditable(false);        jf.setSize(250, 200);        jf.setLocation(450, 500);        jf.setVisible(true);        jf.setResizable(false);        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    String num1 = "0";    String operator = "+";    public void actionPerformed(ActionEvent e) {        String comm = e.getActionCommand();        if ("0123456789".indexOf(comm) != -1) {//也可以用正则验证            if (append) {// 追加                String s = jt.getText();                jt.setText(s + comm);            } else {// 替换                jt.setText(comm);                append = true;            }        } else if ("+-*/".indexOf(comm) != -1) {            num1 = jt.getText();            operator = comm;            append = false;        } else if ("=".equals(comm)) {            String num2 = jt.getText();            double d1 = Double.parseDouble(num1);            double d2 = Double.parseDouble(num2);            if ("+".equals(operator)) {                d1 = d1 + d2;            } else if ("-".equals(operator)) {                d1 = d1 - d2;            } else if ("*".equals(operator)) {                d1 = d1 * d2;            } else if ("/".equals(operator)) {                d1 = d1 / d2;            }            jt.setText(d1 + "");            append = false;        } else if (".".equals(comm)) {//追加小数点;            String temp = jt.getText();            jt.setText(temp + ".");            append = true;        } else if ("+/-".equals(comm)) {            String temp = jt.getText();            if (temp.startsWith("-")) {                jt.setText(temp.substring(1));            } else {                jt.setText("-" + temp);            }        } else if ("CE".equals(comm) || "C".equals(comm)) {            jt.setText("0");            append = false;        } else if ("BackS".equals(comm)) {            String temp = jt.getText();            if (temp.length() > 0) {                jt.setText(temp.substring(0, temp.length() - 1));            }        }    }    public static void main(String[] args) {        CalculatorDemo c = new CalculatorDemo();        c.makeCalculator();    }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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