简单计算器

前几天刚学的GUI,老师就布置了这个作业,我这个只实现了简单的加减乘除四则运算

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

public class GUI extends JFrame
{
	private JButton num1,num2,num3,num4,num5,num6,num7,num8,num0,num9;
	private JButton bt1,bt2,bt3,bt4,bt5,bt6;
	//private JTextField inputFild;
	private JTextArea chatContent1,chatContent2;
	private JPanel inputPanel,inputpanel1; 
	//private JScrollPane shcrollPanel;
	//构造函数
	public GUI()
	{
		/*******创建窗口*******/
		this.setLayout(new GridLayout(2,1));
		this.setTitle("计算器");
		this.setSize(400, 500);
		this.setLocation(300, 200);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui();
	}
	
	public void gui()
	{
		chatContent1 =new JTextArea(12,34);
		chatContent2 =new JTextArea(4,5);
		chatContent1.setEditable(false);
		chatContent2.setEditable(true);
		inputpanel1=new JPanel();
		inputpanel1.setLayout(new GridLayout(2,1));
		inputpanel1.add(chatContent1);
		inputpanel1.add(chatContent2);
		inputPanel =new JPanel();
		inputPanel.setLayout(new GridLayout(4,4));
		num1=new JButton("1");
		num2=new JButton("2");
		num3=new JButton("3");
		num4=new JButton("4");
		num5=new JButton("5");
		num6=new JButton("6");
		num7=new JButton("7");
		num8=new JButton("8");
		num9=new JButton("9");
		num0=new JButton("0");
		bt1 =new JButton("+");
		bt2 =new JButton("-");
		bt3 =new JButton("*");
		bt4 =new JButton("/");
		bt5 =new JButton("=");
		bt6 =new JButton(".");
		inputPanel.add(num7);
		inputPanel.add(num8);
		inputPanel.add(num9);
		inputPanel.add(bt1);
		inputPanel.add(num4);
		inputPanel.add(num5);
		inputPanel.add(num6);
		inputPanel.add(bt2);
		inputPanel.add(num1);
		inputPanel.add(num2);
		inputPanel.add(num3);
		inputPanel.add(bt3);
		inputPanel.add(bt6);
		inputPanel.add(num0);
		inputPanel.add(bt5);	
		inputPanel.add(bt4);
		this.add(inputpanel1);
		this.add(inputPanel);
		color();//  为按钮设置颜色和大小
		Action();//为按钮添加监听器
	
	}
//为按钮添加监听器
	public void Action()
	{
	
		num1.addActionListener(new ActionListener()
			{
	                public void actionPerformed(ActionEvent e)
	                {
	                	if(num1.getText()=="1")
	        			    chatContent2.append(num1.getText());
	                }
			}
	  );
		num2.addActionListener(new ActionListener()
		  {
             public void actionPerformed(ActionEvent e)
             {
            	if(num2.getText()=="2")
    			    chatContent2.append(num2.getText());
              }
		   }
      );
		num3.addActionListener(new ActionListener()
		  {
              public void actionPerformed(ActionEvent e)
              {
          	      if(num3.getText()=="3")
  			         chatContent2.append(num3.getText());
              }
		   }
      );
		num4.addActionListener(new ActionListener()
		  {
              public void actionPerformed(ActionEvent e)
              {
          	      if(num4.getText()=="4")
  			          chatContent2.append(num4.getText());
              }
		   }
      );
		num5.addActionListener(new ActionListener()
		  {
               public void actionPerformed(ActionEvent e)
               {
          	       if(num5.getText()=="5")
  			          chatContent2.append(num5.getText());
               }
		   }
      );
	 	num6.addActionListener(new ActionListener()
		  {
               public void actionPerformed(ActionEvent e)
               {
          	      if(num6.getText()=="6")
  			          chatContent2.append(num6.getText());
               }
		   }
      );
		num7.addActionListener(new ActionListener()
		  {
               public void actionPerformed(ActionEvent e)
               {
          	      if(num7.getText()=="7")
  			          chatContent2.append(num7.getText());
               }
		   }
      );
		num8.addActionListener(new ActionListener()
		  {
                public void actionPerformed(ActionEvent e)
                {
          	       if(num8.getText()=="8")
  			          chatContent2.append(num8.getText());
                }
		   }
     );
		num9.addActionListener(new ActionListener()
		  {
                public void actionPerformed(ActionEvent e)
                {
          	        if(num9.getText()=="9")
  			           chatContent2.append(num9.getText());
               }
		   }
     );
		num0.addActionListener(new ActionListener()
		  {
                 public void actionPerformed(ActionEvent e)
                 {
          	        if(num0.getText()=="0")
  			            chatContent2.append(num0.getText());
                 }
		   }
     );
		bt1.addActionListener(new ActionListener()
		  {
                  public void actionPerformed(ActionEvent e)
                {
          	        if(bt1.getText()=="+")
  			            chatContent2.append(bt1.getText());
                }
		   }
     );
		bt2.addActionListener(new ActionListener()
		  {
                  public void actionPerformed(ActionEvent e)
                 {
        	        if(bt2.getText()=="-")
			           chatContent2.append(bt2.getText());
                 }
		   }
   );
		bt3.addActionListener(new ActionListener()
		  {
                 public void actionPerformed(ActionEvent e)
                 {
        	        if(bt3.getText()=="*")
			            chatContent2.append(bt3.getText());
                 }
		   }
   );
		bt4.addActionListener(new ActionListener()
		  {
                 public void actionPerformed(ActionEvent e)
                 {
        	         if(bt4.getText()=="/")
			             chatContent2.append(bt4.getText());
                 }
		   }
   );	
		bt6.addActionListener(new ActionListener()
		  {
                 public void actionPerformed(ActionEvent e)
                 {
        	         if(bt6.getText()==".")
			             chatContent2.append(bt6.getText());
                 }
		   }
   );
		bt5.addActionListener(new ActionListener()
		  {
             public void actionPerformed(ActionEvent e)
             {
      	     String  str=chatContent2.getText();
      	     if(bt5.getText()=="=")
		         chatContent2.append(bt5.getText());
             double a,b,c;
             int num=0,num1=1;
             String str1,str2;
	         for(int i=0;i<str.length();i++)
      	     {
      	          if(str.charAt(i)>='0'&&str.charAt(i)<='9'||str.charAt(i)=='.')
                      num++;
      	          else
      		          break;
      	      }
	         for(int j=0;j<str.length();j++)
	           if(str.charAt(j)=='.')
	        	 {
	        		 num1=0;
	        		 break;
	        	 }
      	      str1=str.substring(0,num);
      	      str2=str.substring(num+1);
      	      a=Double.parseDouble(str1);
      	      b=Double.parseDouble(str2);
      	      if(str.charAt(num)=='+')
              {
		          c=a+b;
		          if(num1==1)
		          {
		        	  long n;
		        	  n=(long)(a+b);
		        	  chatContent2.append(Long.toString(n));
		          }
		          else
		        	  chatContent2.append(Double.toString(c));  
      		      
               }
      	       if(str.charAt(num)=='-')
               {
      		       c=a-b;
      		     if(num1==1)
		          {
		        	  long n;
		        	  n=(long)(a-b);
		        	  chatContent2.append(Long.toString(n));
		          }
		          else
		        	  chatContent2.append(Double.toString(c)); 
               }
      	       if(str.charAt(num)=='*')
               {
      		       c=a*b;
      		     if(num1==1)
		          {
		        	  long n;
		        	  n=(long)(a*b);
		        	  chatContent2.append(Long.toString(n));
		          }
		          else
		        	  chatContent2.append(Double.toString(c)); 
               }
      	       if(str.charAt(num)=='/')
               {
      		        c = a/b;
		        	  chatContent2.append(Double.toString(c)); 
                }
      	       chatContent1.setText("");
      	       chatContent1.append(chatContent2.getText());
      	       chatContent2.setText("");
           }
       }
 );		
}
//  为按钮设置颜色和大小
	public void color()
	{
		num1.setBackground(Color.white);
		num2.setBackground(Color.white);
		num3.setBackground(Color.white);
		num4.setBackground(Color.white);
		num5.setBackground(Color.white);
		num6.setBackground(Color.white);
		num7.setBackground(Color.white);
		num8.setBackground(Color.white);
		num9.setBackground(Color.white);
		num0.setBackground(Color.white);
		bt1.setBackground(Color.LIGHT_GRAY);
		bt2.setBackground(Color.LIGHT_GRAY);
		bt3.setBackground(Color.LIGHT_GRAY);
		bt4.setBackground(Color.LIGHT_GRAY);
		bt5.setBackground(Color.white);
		bt6.setBackground(Color.white);
		bt5.setFont(new Font("黑体",Font.BOLD,40));
	}
	 public static void main(String[] args) {
		  new GUI();
	}
}


package com.softeem.train.calculator; import java.util.Scanner; import java.io.*; public class Calculator { private int m; // 第一个输入的数字 private int n; // 第二个输入的数字 public Calculator() { this.m = 0; this.n = 0; } /* * public void use() { System.out.println("你是否继续使用?继续输入Y,退出输任意键?"); Scanner * in5= new Scanner(System.in); String zz=in5.next(); * if(zz.equals("Y")||zz.equals("y")) this.gui(); else System.exit(0); * * } */ public void graphic() { //3 System.out.println("=========================="); System.out.println("欢迎使用 So easy 计算机简易系统 "); System.out.println(" 请选择计算类型:"); System.out.println(" 1.加法"); System.out.println(" 2.减法"); System.out.println(" 3.乘法"); System.out.println(" 4.除法"); System.out.println(" 5.输入表达式计算"); System.out.println(" 6.退出"); System.out.println("=========================="); Scanner in3 = new Scanner(System.in); int n = in3.nextInt(); switch (n) { //4 case 1: this.add(); //8 break; case 2: this.sub(); break; case 3: this.mul(); break; case 4: this.div(); break; case 5: this.cal(); break; case 6: System.exit(0); default: break; } } public void add() // 实现加法的方法 { //5 System.out.println("欢迎使用加法操作"); this.in(); //7 int sum; sum = this.m + this.n; System.out.println("你输入的数据的和是:" + sum); // this.use(); } public void sub() // 实现减法的方法 { System.out.println("欢迎使用减法操作"); this.in(); int cha; cha = this.m - this.n; System.out.println("你输入的数据的差是:" + cha); // this.use(); } public void mul() // 实现乘法的方法 { System.out.println("欢迎使用乘法操作"); this.in(); int ji; ji = this.m * this.n; System.out.println("你输入的数据的积是:" + ji); // this.use(); } public void div() // 实现除法的方法 { System.out.println("欢迎使用除法操作"); this.in(); if (n == 0) { System.out.println("被除数不能为零!"); this.graphic(); } int shang; shang = this.m / this.n; System.out.println("你输入的数据的和是:" + shang); // this.use(); } public void cal() // 实现表达式运算方法 { String s = ""; // 接收表达式的字符串 String ss1 = ""; // 离数字m的字符串 String ss2 = ""; // 离数字n的字符串 char c1 = ' '; // 存放运算符 int m = 0; // 第一个数字 int n = 0; // 第二个数字 System.out.print("请输入一个表达式:"); try { BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); s = in.readLine(); } catch (IOException e) { } for (int i = 0; i < s.length(); i++) { if ((s.charAt(i) == '+') || (s.charAt(i) == '-') || (s.charAt(i) == '*') || (s.charAt(i) == '/')) { c1 = s.charAt(i); // 第一个数 for (int j = 0; j < i; j++) { ss1 += s.charAt(j); m = Integer.parseInt(ss1); } // 第二个数 for (int j = i + 1; j < s.length(); j++) { ss2 += s.charAt(j); n = Integer.parseInt(ss2); } break; } } if (c1 == '+') System.out.println(m + "+" + n + "=" + (m + n)); if (c1 == '-') System.out.println(m + "-" + n + "=" + (m - n)); if (c1 == '*') System.out.println(m + "*" + n + "=" + (m * n)); if (c1 == '/') System.out.println(m + "/" + n + "=" + (m / n)); } public void in() // 实现接收数据的方法 { //6 System.out.println("请输入一个数:"); Scanner in1 = new Scanner(System.in); this.m = in1.nextInt(); System.out.println("请输入第二个数:"); Scanner in2 = new Scanner(System.in); this.n = in2.nextInt(); } public static void main(String[] args) throws java.io.IOException { //1 String ifquit = ""; // 用于接收是否退出 Calculator c = new Calculator(); do { //2 c.graphic(); // 调用界面方法 //9 System.out.println("你是否继续使用?继续输入Y,退出输任意键?"); Scanner in5 = new Scanner(System.in); ifquit = in5.nextLine(); //10 } while (ifquit.equals("Y") || ifquit.equals("y")); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值