JAVA制作的简易计算器——傻瓜计算器

用JAVA编写的傻瓜计算器

作用:

1.可以实现加法、减法、乘法、除法简单运算且是单一运算,不可混合使用。

2.CE为清除键

3.没有小数点O(∩_∩)O

思路:

创建JFrame窗口,设置标题,创建JButton,创建文本框JTextField用作显示。

先定义各种按钮类型,用作成员。定义窗口方法对窗口进行设置。定义按钮事件方法,每个按钮的

每次点下在文本框都会进行打印。以运算符为界限对字符串进行分割,并把分割后的字符串放入新

建的数组当中。对字符串数组内容访问转换为整数类型,进行运算。运算的结果为double类型的并

把结果打印到文本框中。

成品:

JAVA制作的简易计算器——傻瓜计算器_第1张图片

 代码实现:

package test1;
import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Window extends JFrame{
    private JButton ADD=new JButton("+");
    private JButton SUB=new JButton("-");
    private JButton SUM=new JButton("x");
    private JButton DIV=new JButton("/");
    private JButton l=new JButton("0");
    private JButton y=new JButton("1");
    private JButton e=new JButton("2");
    private JButton s=new JButton("3");
    private JButton s4=new JButton("4");
    private JButton w=new JButton("5");
    private JButton l6=new JButton("6");
    private JButton q=new JButton("7");
    private JButton b=new JButton("8");
    private JButton j=new JButton("9");
    private JButton dengyu=new JButton("=");
    private JButton qingchu=new JButton("CE");
    private JTextField jt=new JTextField();
    private String shuju;
    public Window(){
        window();
        addJb();
        addjt();
        add();
        this.setVisible(true);
    }
   public void window(){
        //窗体大小
        this.setSize(570,550);
        //窗体标题
        this.setTitle("傻瓜计算器");
        //解除窗体的默认设置
        this.setLayout(null);
        //窗体关闭程序停止
        this.setDefaultCloseOperation(3);
        //窗体位于屏幕中央
       this.setLocationRelativeTo(null);
   }
//按钮大小位置设置
   public void addJb(){
            ADD.setBounds(350,150,90,90);
            ADD.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(ADD);
            SUB.setBounds(450,150,90,90);
            SUB.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(SUB);
            SUM.setBounds(350,250,90,90);
       SUM.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(SUM);
            DIV.setBounds(450,250,90,90);
       DIV.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(DIV);
            y.setBounds(10,150,90,90);
       y.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(y);
            e.setBounds(110,150,90,90);
       e.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(e);
            s.setBounds(210,150,90,90);
       s.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(s);
            s4.setBounds(10,250,90,90);
       s4.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(s4);
            w.setBounds(110,250,90,90);
       w.setFont(new Font("宋体", Font.PLAIN, 50));
           this.add(w);
            l6.setBounds(210,250,90,90);
       l6.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(l6);
            q.setBounds(10,350,90,90);
       q.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(q);
            b.setBounds(110,350,90,90);
       b.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(b);
            j.setBounds(210,350,90,90);
       j.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(j);
            dengyu.setBounds(10,450,530,50);
       dengyu.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(dengyu);
            qingchu.setBounds(450,350,90,90);
       qingchu.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(qingchu);
            l.setBounds(350,350,90,90);
       l.setFont(new Font("宋体", Font.PLAIN, 50));
            this.add(l);
        }
        //文本框设置
        public void addjt(){
        jt.setBounds(10,50,530,80);
        jt.setFont(new Font("宋体", Font.PLAIN, 40));
        this.add(jt);

        }
//添加按钮事件
        public void add(){
        y.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("1");

            }
        });
        e.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("2");

            }
        });
        qingchu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // jt.setText(jt.getText().substring(0, jt.getText().length() - 1));
                jt.setText("");
            }
        });
        s.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("3");
            }
        });
        s4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("4");
            }
        });
        w.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("5");
            }
        });
        l6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("6");
            }
        });
        q.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("7");
            }
        });
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("8");
            }
        });
        j.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("9");
            }
        });
        l.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("0");
            }
        });
        ADD.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("+");
            }
        });
        SUB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("-");
            }
        });
        SUM.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("x");
            }
        });
        DIV.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number("/");
            }
        });
        dengyu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deng();
            }
        });
        }
        //按钮输出数字方法
       public void number(String a){
        jt.setText(jt.getText() + a);
        }
       //等号运算方法
          public  void deng(){
        //读取文本框字符串
         String str=jt.getText();
         //字符串拆成字符
         char [] ch=str.toCharArray();
         String[] ing=new String[ch.length];
         int[] a=new int[ing.length];
         double sum=0;
         for(int i=0;i

PS:

初来乍到的小白一枚,里边运用到了窗口、组件、监视器。初学者可以简单参考一下。顺便加深一下知识点的印象。学习就应该循序渐进,一步一个脚印来。里边多少会有些问题希望在你们手里可以得到改进,同时你也可以学到新的知识。O(∩_∩)O

你可能感兴趣的:(编程,JAVA,学习)