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

一个简单的四则运算器(Java swing 基础入门)

程序员文章站 2022-06-28 18:23:00
package pageDesign;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.*;public class calPage { public void getInfo(String infoAll,JLabel jl){ for (int i =0;i<...
package pageDesign;

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

public class calPage {
    public void getInfo(String infoAll,JLabel jl){
        for (int i =0;i<count;i++) {
            infoAll = infoAll + click.get(i);
        }
        jl.setText(infoAll);
    }//获取数组信息并显示在JLabel中
    public void getValue(JButton jb,JLabel jl){
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String infoAll = "";
                ArrayList<Double> intNone = new ArrayList<>();//空Double数组,方便置零
                ArrayList<String> none = new ArrayList<>();//空String数组,方便置零
                String input = jb.getText();//获取点击按钮的文本
                if (input == "CE") {
                    click = none;
                    infoAll = "";
                    jl.setText(infoAll);
                }//按到清空键后的函数
                else if(input == "Del") {
                    if(count>0)//当数组内有数据
                    {
                        infoAll="";
                        click.remove(--count);
                        getInfo(infoAll,jl);
                    }
                    else {//当数组内无数据
                        jl.setText("目前输入为空");
                    }
                }//按到删除键后的函数
                else if(input != "Del"&&input!="CE"&&input!="+"&&input!="-"&&input!="*"&&input!="/"&&input!="="){
                    click.add(input);
                    count++;
                     getInfo(infoAll,jl);
                }//正常识别数字按钮数值
                else if(input=="+"||input=="-"||input=="*"||input=="/"){
                click.add(input);
                double s = 0;
                String extra="";
                for(int i = count-1;i>=0;i--)
                {
                    extra = extra + click.get(i);
                }//获取前一段数字
                    s = Double.parseDouble(extra);
                cal.add(s);
                count1 = count;
                count++;
                getInfo(infoAll,jl);
            }//加减乘除四则运算
                else if(input == "="){
                    double s = 0;
                    String extra="";
                    for(int i = count1+1;i<count;i++)
                    {
                        extra = extra + click.get(i);
                    }//获取后一段数字
                    s = Double.parseDouble(extra);
                    cal.add(s);
                    switch (click.get(count1))
                    {
                        case "+":s = cal.get(0)+cal.get(1);break;
                        case "-":s = cal.get(0)-cal.get(1);break;
                        case "*":s = cal.get(0)*cal.get(1);break;
                        case "/":s = cal.get(0)/cal.get(1);break;
                    }//switch判断加减乘除符号
                    infoAll = Double.toString(s);
                    jl.setText(infoAll);
                    cal=intNone;
                    click=none;
                    count = 0;
                    count1 = 0;//全部置零
                }//等于键得数值
            }//添加按钮监听内容
        });
    }//获取按钮数值并进行进一步的计算

    int count = 0;
    int count1 = 0;
    static public ArrayList<Double> cal = new ArrayList<Double>();//需要计算的数值
    private JFrame calAll = new JFrame("计算器 作者:大W的幻想");//主容器
    private JPanel numberScr = new JPanel();//显示数字屏
    private JPanel buttonScr = new JPanel();//显示按钮屏
    private JLabel screen = new JLabel();//显示计算公式
    static public ArrayList<String> click = new ArrayList<String>();//存储点击后的数据
    static public String[] sign = {"7","8","9","+","Del","4","5","6","-","CE","1","2","3","*","(","0",".","=","/",")"};//按钮对应数值
    static private JButton buList = new JButton();//按钮样式
    public calPage(){
        calAll.setBounds(300,200,350,300);
        buttonScr.setLayout(new GridLayout(4,5,5,5));
        for(String info:sign){
            buList = new JButton(info);
            buttonScr.add(buList);
            getValue(buList,screen);
        }//构造所有按钮
        numberScr.add(screen);
        calAll.add(numberScr,BorderLayout.NORTH);
        calAll.add(buttonScr, BorderLayout.SOUTH);
        calAll.setVisible(true);
        calAll.setResizable(false);
        calAll.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }//构造

}

差点忘了,这是主函数的内容

package com.company;
import pageDesign.calPage;
public class Main {
    public static void main(String[] args) {
       new calPage();
    }
}

因为我两个函数分开来写的
一个简单的四则运算器(Java swing 基础入门)
代码如上,一些新功能的添加会放在后面的博客中。这个计算器还有()的功能未加入,不过我会适时的修改以达到正常的使用

本文地址:https://blog.csdn.net/qq_35424155/article/details/111039443