Java实现计算器

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//计算机
public class Study extends JFrame implements ActionListener//存放计算结果的文本框
{
    private TextField a=new TextField(30);
    private JButton b1=new JButton("1");
    private JButton b2=new JButton("2");
    private JButton b3=new JButton("3");
    private JButton b4=new JButton("4");
    private JButton b5=new JButton("5");
    private JButton b6=new JButton("6");
    private JButton b7=new JButton("7");
    private JButton b8=new JButton("8");
    private JButton b9=new JButton("9");
    private JButton b10=new JButton("0");//表示数值的按钮
    private JButton b11=new JButton(".");
    private JButton b12=new JButton("=");
    private JButton c1=new JButton("+");
    private JButton c2=new JButton("-");
    private JButton c3=new JButton("*");
    private JButton c4=new JButton("/");//运算符按钮
    private JButton reset=new JButton("C");//清零按钮
    private JPanel p1=new JPanel();//放置文本框等的面板
    private JPanel p2=new JPanel();//放置按钮的面板
    private boolean end,add,sub,mul,div;//表示相应的运算符
    private String s;
    private double num1,num2;//运算数字
   Study()
    {
        super("计算器");
        this.setSize(300, 300);//定义容器
        Container c=this.getContentPane();//获取框架的内容
        c.add(p1,BorderLayout.NORTH);
        c.add(p2, BorderLayout.CENTER);
        p1.setLayout(new FlowLayout(FlowLayout.TRAILING));
        p1.add(a);
        p1.add(reset);
        p2.setLayout(new GridLayout(4,4 ));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(c1);
        p2.add(b4);
        p2.add(b5);
        p2.add(b6);
        p2.add(c2);
        p2.add(b7);
        p2.add(b8);
        p2.add(b9);
        p2.add(c3);
        p2.add(b10);
        p2.add(b11);
        p2.add(b12);
        p2.add(c4);
        //给组件添加监听器
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b10.addActionListener(this);
        b11.addActionListener(this);
        b12.addActionListener(this);
        c1.addActionListener(this);
        c1.addActionListener(this);
        c2.addActionListener(this);
        c3.addActionListener(this);
        c4.addActionListener(this);
        reset.addActionListener(this);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void num(int i)//在文本框中显示数值
    {
        String s1=null;
        s1=String.valueOf(i);
        if(end)//如果输入数字结束,则将文本框置零,重新输入
        {
            a.setText("0");
            end=false;
        }
        if((a.getText()).equals("0"))
        //如果文本框的内容为零,则覆盖文本框的内容
        {
            a.setText(s1);
        }
        else
        {
            //如果文本框的内容不为零,则在内容后面添加数字
            s=a.getText()+s1;
            a.setText(s);
        }
    }
    public void sign(int s)
    {
        //确定运算符
        if(s==1)
        {
            //加法
            add=true;
            sub=false;
            mul=false;
            div=false;
        }
        else if(s==2)
        {
            //减法
            add=false;
            sub=true;
            mul=false;
            div=false;
        }
        else if(s==3)
        {
            //乘法
            add=false;
            sub=false;
            mul=true;
            div=false;
        }
        else if(s==4)
        {
            //除法
            add=false;
            sub=false;
            mul=false;
            div=true;
        }
        num1=Double.parseDouble(a.getText());
        //确定第一个运算数字
        end=true;
    }
    public void actionPerformed(ActionEvent e)
    {
        //相应按钮事件
        if(e.getSource()==b1)
        {
            num(1);
        }
        else if(e.getSource()==b2)
        {
            num(2);
        }
        else if(e.getSource()==b3)
        {
            num(3);
        }
        else if(e.getSource()==b4)
        {
            num(4);
        }
        else if(e.getSource()==b5)
        {
            num(5);
        }
        else if(e.getSource()==b6)
        {
            num(6);
        }
        else if(e.getSource()==b7)
        {
            num(7);
        }
        else if(e.getSource()==b8)
        {
            num(8);
        }
        else if(e.getSource()==b9)
        {
             num(9);
        }
        else if(e.getSource()==b10)
        {
            num(0);
        }
        else if(e.getSource()==b11) {
            //按字符判断输入的数值有没有小数点,如果其中有一个是“.",就不再输入
            boolean j = true;
            for (int i = 0; i < a.getText().length(); i++) {
                if (a.getText().charAt(i) == '.') {
                    j = false;
                    break;
                }
            }

            if (j) {
                a.setText(a.getText() + '.');
            }
        }
            else if (e.getSource() == c1)
            {
                sign(1);
            }
            else if (e.getSource() == c2)
            {
                sign(2);
            }
            else if (e.getSource() == c3)
            {
                sign(3);
            }
            else if (e.getSource() == c4) {
                sign(4);
            }
            else if (e.getSource() == b12)
            {
                num2 = Double.parseDouble(a.getText());//获取第二个运算数值
                //计算运算结果
                if (add)
                {
                    num1 = num1 + num2;
                }
                else if (sub)
                {
                    num1 = num1 - num2;
                }
                else if (mul)
                {
                    num1 = num1 * num2;
                }
                else if (div)
                {
                    num1 = num1 / num2;
                }
                a.setText(String.valueOf(num1));//显示运算结果
                end = true;
            }
            else if (e.getSource() == reset) {
                //把文本框设为0,把要算的值归为0
                a.setText(String.valueOf(""));
                num1 = num2 = 0;
            }
    }

    public static void main(String[] args)
    {
        Study st=new Study();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值