欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现的计算器功能示例【基于swing组件】

程序员文章站 2023-12-15 15:47:22
本文实例讲述了java实现的计算器功能。分享给大家供大家参考,具体如下: package awtdemo; import java.awt.borderlayo...

本文实例讲述了java实现的计算器功能。分享给大家供大家参考,具体如下:

package awtdemo;
import java.awt.borderlayout;
import java.awt.color;
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;
@suppresswarnings("serial")
public class calculator extends jframe implements actionlistener {
    private final string[] keys = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
            "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
    private final string[] command = { "backspace", "ce", "c" };
    private jbutton keys[] = new jbutton[keys.length];
    private jbutton commands[] = new jbutton[command.length];
    private jtextfield resulttext = new jtextfield("0");
    private boolean firstdigit = true;
    private double resultnum = 0.0;
    private string operator = "=";
    private boolean operatevalidflag = true;
    public calculator() {
        init();
        this.setbackground(color.light_gray);
        this.settitle("www.jb51.net - 计算器");
        this.setlocation(500, 300);
        this.setresizable(false);
        this.pack();
    }
    private void init() {
        resulttext.sethorizontalalignment(jtextfield.right);
        resulttext.seteditable(false);
        resulttext.setbackground(color.white);
        jpanel calckeyspanel = new jpanel();
        calckeyspanel.setlayout(new gridlayout(4, 5, 3, 3));
        for (int i = 0; i < keys.length; i++) {
            keys[i] = new jbutton(keys[i]);
            calckeyspanel.add(keys[i]);
            keys[i].setforeground(color.blue);
        }
        keys[3].setforeground(color.red);
        keys[8].setforeground(color.red);
        keys[13].setforeground(color.red);
        keys[18].setforeground(color.red);
        keys[19].setforeground(color.red);
        jpanel commandspanel = new jpanel();
        commandspanel.setlayout(new gridlayout(1, 3, 3, 3));
        for (int i = 0; i < command.length; i++) {
            commands[i] = new jbutton(command[i]);
            commandspanel.add(commands[i]);
            commands[i].setforeground(color.red);
        }
        jpanel panel1 = new jpanel();
        panel1.setlayout(new borderlayout(3, 3));
        panel1.add("north", commandspanel);
        panel1.add("west", calckeyspanel);
        jpanel top = new jpanel();
        top.setlayout(new borderlayout());
        top.add("center", resulttext);
        getcontentpane().setlayout(new borderlayout(3, 5));
        getcontentpane().add("north", top);
        getcontentpane().add("center", panel1);
        for (int i = 0; i < keys.length; i++) {
            keys[i].addactionlistener(this);
        }
        for (int i = 0; i < command.length; i++) {
            commands[i].addactionlistener(this);
        }
    }
    public void actionperformed(actionevent e) {
        string label = e.getactioncommand();
        if (label.equals(command[0])) {
            handlebackspace();
        } else if (label.equals(command[1])) {
            resulttext.settext("0");
        } else if (label.equals(command[2])) {
            handlec();
        } else if ("0123456789.".indexof(label) >= 0) {
            handlenumber(label);
        } else {
            handleoperator(label);
        }
    }
    private void handlebackspace() {
        string text = resulttext.gettext();
        int i = text.length();
        if (i > 0) {
            text = text.substring(0, i - 1);
            if (text.length() == 0) {
                resulttext.settext("0");
                firstdigit = true;
                operator = "=";
            } else {
                resulttext.settext(text);
            }
        }
    }
    private void handlenumber(string key) {
        if (firstdigit) {
            resulttext.settext(key);
        } else if ((key.equals(".")) && (resulttext.gettext().indexof(".") < 0)) {
            resulttext.settext(resulttext.gettext() + ".");
        } else if (!key.equals(".")) {
            resulttext.settext(resulttext.gettext() + key);
        }
        firstdigit = false;
    }
    private void handlec() {
        resulttext.settext("0");
        firstdigit = true;
        operator = "=";
    }
    private void handleoperator(string key) {
        if (operator.equals("/")) {
            if (getnumberfromtext() == 0.0) {
                operatevalidflag = false;
                resulttext.settext("除数不能为零");
            } else {
                resultnum /= getnumberfromtext();
            }
        } else if (operator.equals("1/x")) {
            if (resultnum == 0.0) {
                operatevalidflag = false;
                resulttext.settext("零没有倒数");
            } else {
                resultnum = 1 / resultnum;
            }
        } else if (operator.equals("+")) {
            resultnum += getnumberfromtext();
        } else if (operator.equals("-")) {
            resultnum -= getnumberfromtext();
        } else if (operator.equals("*")) {
            resultnum *= getnumberfromtext();
        } else if (operator.equals("sqrt")) {
            resultnum = math.sqrt(resultnum);
        } else if (operator.equals("%")) {
            resultnum = resultnum / 100;
        } else if (operator.equals("+/-")) {
            resultnum = resultnum * (-1);
        } else if (operator.equals("=")) {
            resultnum = getnumberfromtext();
        }
        if (operatevalidflag) {
            long t1;
            double t2;
            t1 = (long) resultnum;
            t2 = resultnum - t1;
            if (t2 == 0) {
                resulttext.settext(string.valueof(t1));
            } else {
                resulttext.settext(string.valueof(resultnum));
            }
        }
        operator = key;
        firstdigit = true;
        operatevalidflag = true;
    }
    private double getnumberfromtext() {
        double result = 0;
        try {
            result = double.valueof(resulttext.gettext()).doublevalue();
        } catch (numberformatexception e) {
        }
        return result;
    }
    public static void main(string args[]) {
        calculator calculator1 = new calculator();
        calculator1.setvisible(true);
        calculator1.setdefaultcloseoperation(jframe.exit_on_close);
    }
}

运行效果:

java实现的计算器功能示例【基于swing组件】

ps:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线一元函数(方程)求解计算工具:

科学计算器在线使用_高级计算器在线计算:

在线计算器_标准计算器:

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

上一篇:

下一篇: