zoukankan      html  css  js  c++  java
  • Java实现简单的计算器

    此文转载自:https://blog.csdn.net/qq_49529322/article/details/111792238#commentBox

    要求: 编写一个Java应用程序,采用GridLayout实现如下计算机器的布局及做相应的事件处理

    代码实现(本需求完全有个人实现):

    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")
    class MyComputerWin extends JFrame implements ActionListener {
    	JTextField expressText;  //创建一个单行文本框引用
    	JPanel numPanel, operPanel;// 创建两个面板引用
    	JPanel panel; //创建一个面板用于存放文本框与"="按钮。
    	//创建按钮数组用于存放按钮,并定义数组的长度
    	JButton[] numBtn = new JButton[12];
    	JButton[] operBtn = new JButton[6];
    	String express = "";
    
    	public void init() {
    		expressText = new JTextField(1); //实例化单行文本框对象
    		//实例化面板对象
    		numPanel = new JPanel(); 
    		operPanel = new JPanel();
    		panel = new JPanel();
    		//设置面板为网格布局(GridLayout),  注意: 默认是流动布局(FlowLayout)
    		panel.setLayout(new BorderLayout());
    		numPanel.setLayout(new GridLayout(4, 3));
    		operPanel.setLayout(new GridLayout(5, 1));
    		//将数字按钮添加到数字面板上
    		for (int i = 0; i < numBtn.length; i++) {
    			//创建数字按钮
    			if(i>=0&&i<=2) {
    				numBtn[i] = new JButton(""+(7+i));
    			}else if(i>=3&&i<=5) {
    				numBtn[i] = new JButton(""+(i+1));
    			}else if(i>=6&&i<=8) {
    				numBtn[i] = new JButton(""+(i-5));
    			}else {
    				numBtn[9] = new JButton(""+"00");
    				numBtn[10] = new JButton(""+"0");
    				numBtn[11] = new JButton(""+"^");
    			}
    			numPanel.add(numBtn[i]);
    			numBtn[i].addActionListener(this);
    			
    		}
    		//设置符号按钮
    		operBtn[0] = new JButton("%");
    		operBtn[1] = new JButton("/");
    		operBtn[2] = new JButton("*");
    		operBtn[3] = new JButton("-");
    		operBtn[4] = new JButton("+");
    		operBtn[5] = new JButton("=");
    		//将符号按钮添加到符号面板上
    		for (int i = 0; i < operBtn.length-1; i++) {
    			operPanel.add(operBtn[i]);
    			operBtn[i].addActionListener(this);
    		}
    		panel.add(expressText);
    		panel.add(operBtn[5],BorderLayout.EAST);
    		operBtn[5].addActionListener(this);
    		
    
    		//将文本框添加到窗口上,并设置为边界布局
    		this.add(panel, BorderLayout.NORTH);
    		//将面板添加到窗口上
    		this.add(numPanel, BorderLayout.CENTER);
    		this.add(operPanel, BorderLayout.EAST);
    	}
    
    	public MyComputerWin(String title) {
    		super(title);
    		init();
    		this.setVisible(true);
    		this.setSize(300, 300);
    		this.setLocationRelativeTo(null);
    		this.expressText.setBackground(Color.YELLOW);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		String s = e.getActionCommand();  //获得发生该事件的相关命令,在这里就是获得按钮事件上的数字。
    		express += s; //将获得的命令保存起来
    		expressText.setText(express); //将获得的命令放入文本框中。
    		//因为s代表的是单个按钮事件上的命令,所以这里判断s获得的命令是不是"="。
    		if ("=".equals(s)) {
    			String[] nums = express.split("\p{Punct}");// 5+6= 正则表达式 \p{Punct}代表的是标点符号,这里就是通过标点符号(+,-,*,/.....)来进行分割,然后得到我们想要计算的数字。
    		    String[] opers =express.split("\d+"); //利用正则表达式中的多位数字来进行分解。然后得到我们想要的运算符号
    			String oper=opers[1]; //定义一个String类型的变量存储符号
    			double num1=Integer.parseInt(nums[0]);
    			double num2=Integer.parseInt(nums[1]);
    		    double result=1;
    		    switch(oper) {
    			    case "+":express+=num1+num2;break;
    			    case "-":express+=num1-num2;break;
    			    case "*":express+=num1*num2;break;
    			    case "/":if(num2!=0) {
    			    	express+=num1/num2;
    			    	}else {
    			    		express+="分母为0,不能计算!!";
    			    	} break;
    			    case "%":express+=num1/100*num2;break;
    			    case "^":for(int i=0;i<num2;i++) {
    			    			result*=num1;
    			    		} 
    			    	express+=result;break;
    		    }
    		    //express+=result;
    		    expressText.setText(express);
    		    express="";  //计算完之后将数据清空
    		}
    	}
    
    }
    
    public class MyComputer {
    
    	public static void main(String[] args) {
    		new MyComputerWin("我的计算器");
    	}
    }
    
       

    更多内容详见微信公众号:Python测试和开发

    Python测试和开发

  • 相关阅读:
    求最大子数组和
    第四周学习进度
    四则运算3
    实用工具箱app开发日记5
    实用工具箱app开发日记4
    实用工具箱app开发日记3
    实用工具箱app开发日记2
    实用工具箱app开发日记1
    《软件需求与分析》阅读笔记
    软件需求分析--阅读笔记3
  • 原文地址:https://www.cnblogs.com/phyger/p/14204954.html
Copyright © 2011-2022 走看看