其实学到Java这一块很多人会觉得很复杂实际上学会使用方法其实很简单
话不多说直接贴源码,如下:
package cn.sjy.calculator;import javax.swing.*;
import java.awt.*;/*** 简易计算器* @author 石俊熠* 2020.7.13 11:24* 注:仿照某Java大佬的源码改之*/
public class Test {private String str=" ";//输入输出框显示内容private JTextField input;//输出框private JPanel button;//按钮区域private String[] addbutton={"AC","求根","取反","/","7","8","9","*","4","5","6","-","1","2","3","+","+/-","0",".","="};private String showTextFiledNew;//设置主窗体public Test() {//初始化窗体JFrame f = new JFrame("计算器");Container c = f.getContentPane();c.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));//设置排列布局为Y轴排列f.setLocation(200, 300);//f.setSize(500, 600);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);input = new JTextField(30);//设置输入框的长度为30input.setHorizontalAlignment(JTextField.RIGHT);//设置输入框内容的对齐方式,设置为右对齐JPanel jPanel = new JPanel();//建立一个面板jPanel.add(input);c.add(jPanel);GridBagLayout gridBagLayout = new GridBagLayout();GridBagConstraints cs = new GridBagConstraints();button = new JPanel();button.setLayout(gridBagLayout);for (int i = 0; i < addbutton.length; i++) {if ((i + 1) % 4 == 0) {cs.gridwidth = GridBagConstraints.REMAINDER;} else {cs.fill = GridBagConstraints.BOTH;//使组件完全填充其显示区域cs.weightx = 1.0;cs.gridwidth = 1;}JButton b = new JButton(addbutton[i]);gridBagLayout.setConstraints(b, cs);//设置组件的约束b.addActionListener(e -> {String command = e.getActionCommand();setShowTextFiledNew(command);});button.add(b);}c.add(button);f.pack();}/*** 设置显示内容得窗体* @param command 按钮点击命令* 如果按下=执行计算命令* 如果按下运算符 则格式为 空格+运算符+空格* 如果按下数字 则直接拼接* 需要更多资料可以加群:756584822*/public void setShowTextFiledNew(String command) {if(command.equals("=")){str = getResult(str);}else if(command.equals("+")||command.equals("-")||command.equals("*")||command.equals("/")||command.equals("求根")||command.equals("取反")){str = str+" "+command+" ";}else if(command.equals("AC")){str = "";}else{str = str+command;}input.setText(str);//将按钮的信息返回到input中}/*** 计算模块* @param str 需要计算的字符串* 根据空格进行分割成字符串数组* 然后判断是哪种类型进行计算*通过一个result来存放那个结果*/private String getResult(String str) {Double result = 0.0;//用来存放结果String[] need = str.split(" ");//进行字符串分割for (String j : need) {System.out.print(j+" ");}for (int i = 0; i < need.length; i++) {switch (need[i]){case "+":if (result == 0.0 && i==1){result=result+(Double.parseDouble(need[i-1])+Double.parseDouble(need[i+1]));break;}else{result=result+Double.parseDouble(need[i+1]);break;}case "-":if(Double.parseDouble(need[i-1])>=Double.parseDouble(need[i+1])){result = (Double.parseDouble(need[i-1])-Double.parseDouble(need[i+1]));break;}else {result = +(Double.parseDouble(need[i-1])-Double.parseDouble(need[i+1]));break;}case "*":result=result+(Double.parseDouble(need[i-1])*Double.parseDouble(need[i+1]));break;case "/":result=result+(Double.parseDouble(need[i-1])/Double.parseDouble(need[i+1]));break;case "求根":result=result+(Math.sqrt(Double.parseDouble(need[i-1])));break;case "取反":result=result+(-Double.parseDouble(need[i-1]));break;}}return result+"";}//设置主方法,调用Test方法public static void main(String[] args ){new Test();}
}效果如下