java计算器GUI科学型计算器

在这里插入图片描述

package 曾春淮;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

public class ScientificCaculator extends JFrame implements ActionListener{
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JButton a1=new JButton("1");//按钮
	 JButton a2=new JButton("2");
	 JButton a3=new JButton("3");
	 JButton a4=new JButton("4");   
	 JButton a5=new JButton("5");
	 JButton a6=new JButton("6");  private final JButton a17=new JButton("退格"); 
	 JButton a7=new JButton("7");  private final JButton a18=new JButton("删除"); 
	 JButton a8=new JButton("8"); 
	 JButton a9=new JButton("9");  JButton a14=new JButton("0");
	                               JButton a15=new JButton("."); 
	                               JButton a16=new JButton("=");
	                               JButton a19=new JButton("X²");
     JButton a10=new JButton("+"); JButton a20=new JButton("X³");
	 JButton a11=new JButton("-"); JButton a21=new JButton("1/X");
	                               JButton a22=new JButton("³✔");JButton a23=new JButton("²✔");
	 JButton a12=new JButton("*"); //这里增加按钮
	 JButton a13=new JButton("/");  
	 JButton a24=new JButton("sin");
	 JButton a25=new JButton("cos");
	 JButton a26=new JButton("tan"); 	 StringBuffer str;//输入的字符串
	 JTextField  jfield1=new JTextField("0");
	   
	 
	 //菜单
	 JMenuBar menuBar =new JMenuBar();   JMenuItem[] menuFileItems ={new JMenuItem("科学型"),new JMenuItem("标准型")};
	 JMenu menuFile = new JMenu("菜单"); 
	   
	 boolean state=false;//是否第一次输入

	 static double result=0.0;//结果

	 String yunsuan;//这个是运算的 计算结果"

	
	 ScientificCaculator(){
		 setLayout(null);
		 setTitle("中山大学南方学院科学型计算器设计者曾春淮");
		 setBounds(100,150,375,335);
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 jfield1.setHorizontalAlignment(JTextField.RIGHT);//文本右对齐
		 jfield1.setBounds(10, 30, 340, 30); 
		 jfield1.setEditable(true);
		 jfield1.setText("");            a14.setBounds(10, 180, 50, 40);//按钮位置
		 a1.setBounds(10, 60, 50, 40);   a15.setBounds(60, 180, 50, 40);   //a10+ a11- a12* a13/
         a2.setBounds(60, 60, 50, 40);   a16.setBounds(110, 180, 50, 40);//a14=0 a15 . a16= a17 delete   
		 a3.setBounds(110, 60, 50, 40);  a17.setBounds(10, 220, 170, 40);// a18 删除 a19 平方
		 a4.setBounds(10, 100, 50, 40);   
		 a5.setBounds(60, 100, 50, 40);  a18.setBounds(180, 220, 170, 40);
		 a6.setBounds(110, 100, 50, 40);   
		 a7.setBounds(10, 140, 50, 40);  a19.setBounds(280, 180, 70, 40);  a23.setBounds(210, 60, 70, 40);
		 a8.setBounds(60, 140, 50, 40);//增加按钮的位置 
		 a9.setBounds(110, 140, 50, 40); a20.setBounds(210, 180, 70, 40);  a21.setBounds(210, 140, 70, 40);a22.setBounds(210, 100, 70, 40);
		 a24.setBounds(280, 60, 70, 40); a25.setBounds(280, 100, 70, 40);  a26.setBounds(280, 140, 70, 40);
		 a10.setBounds(160, 60, 50, 40); a11.setBounds(160, 100, 50, 40); 
		 a12.setBounds(160, 140, 50, 40);a13.setBounds(160, 180, 50, 40);  add(a20);add(a21);add(a22);add(a23);
		 add(a24); add(a25);add(a26); add(jfield1);add(a1);add(a2);add(a3);add(a4);add(a5);add(a6);add(a7);add(a8);add(a9);
		 add(a10);add(a11);add(a12); add(a13);add(a14);add(a15);add(a16);add(a17);add(a18);add(a19);
		  //增加按钮的
		 //菜单
		//菜单
		 menuBar.add(menuFile);
		 this.setJMenuBar(menuBar);
		 for (int index = 0;index < menuFileItems.length;index++){
			//为菜单项设置助记符(快捷键)
			 menuFileItems[index].addActionListener(this);
			 menuFile.add(menuFileItems[index]);
			}
		 
		 
		 
		 a1.addActionListener(this); a2.addActionListener(this);
		 a3.addActionListener(this); a4.addActionListener(this);
		 a5.addActionListener(this); a6.addActionListener(this); 
		 a7.addActionListener(this); a8.addActionListener(this);   a26.addActionListener(this);
		 a9.addActionListener(this); a14.addActionListener(this);
		 a15.addActionListener(this); a16.addActionListener(this); a24.addActionListener(this); a25.addActionListener(this);
		 a10.addActionListener(this); a11.addActionListener(this);
		 a12.addActionListener(this); a13.addActionListener(this); a20.addActionListener(this);
		 a21.addActionListener(this); a22.addActionListener(this); a23.addActionListener(this);
		 a17.addActionListener(this); a18.addActionListener(this); a19.addActionListener(this);
		 //添加事件监听器
		 //颜色
	  
		
	   a22.setForeground(Color.green);   a21.setForeground(Color.green); a26.setForeground(Color.blue); 
		 a23.setForeground(Color.green); a25.setForeground(Color.blue);  a24.setForeground(Color.blue);}


	 
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ScientificCaculator D= new ScientificCaculator();
       
      D.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		 

        String mingling=e.getActionCommand(); //label 是你触发按钮后的执行的命令 就是操作的意思

        if(e.getActionCommand().equalsIgnoreCase("科学型")){
             new ScientificCaculator().setVisible(true);//使视图可视化	
         
        new ScientificCaculator();
		  new BasicCaculator().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        		if(e.getActionCommand().equalsIgnoreCase("标准型")) {
        			new BasicCaculator().setVisible(true);//使视图可视化	
        			new BasicCaculator();
        			
        			 new ScientificCaculator().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		};
        switch(mingling){

                case "0":jfield1.setText(jfield1.getText()+"0");state=true;break;
                case "1":jfield1.setText(jfield1.getText()+"1");state=true;break;
                case "2":jfield1.setText(jfield1.getText()+"2");state=true;break;
                case "3":jfield1.setText(jfield1.getText()+"3");state=true;break;
                case "4":jfield1.setText(jfield1.getText()+"4");state=true;break;
                case "5":jfield1.setText(jfield1.getText()+"5");state=true;break;
                case "6":jfield1.setText(jfield1.getText()+"6");state=true;break;
                case "7":jfield1.setText(jfield1.getText()+"7");state=true;break;
                case "8":jfield1.setText(jfield1.getText()+"8");state=true;break;
                case "9":jfield1.setText(jfield1.getText()+"9");state=true;break;
                case "X²":jfield1.setText(jfield1.getText()+"");state=true;break; 
                case "X³":jfield1.setText(jfield1.getText()+"");state=true;break;
                case ".":jfield1.setText(jfield1.getText()+".");state=true;break;
                case "²✔":jfield1.setText(jfield1.getText()+"");state=true;break;
                case "³✔":jfield1.setText(jfield1.getText()+"");state=true;break;
                case "1/X":jfield1.setText(jfield1.getText()+"");state=true;break;
                //
                //可以增加操作   case "X^2" 这个是判断按钮:jfield1.setText(jfield1.getText()+"x^2" "?" 这个是输入平方的意思 运算器);state=true;break;
                //下面if 语句还有写 这些符号的功能!!! 极其重要!!
                //没有下面的语句  按钮后都不知道按钮的 计算方法
                default:break;

            }

        if("+".equals(mingling)){//判断语句 如果命令输入是加法“/”就执行以下的操作 以下都是同理

            result+=Double.valueOf(jfield1.getText());

            System.out.println(result);

            jfield1.setText("");

            yunsuan=mingling;

            state=false;

        }else if("-".equals(mingling)){//判断语句 如果命令输入是减法“/”就执行以下的操作 以下都是同理

            result-=Double.valueOf(jfield1.getText());

            jfield1.setText("");

            yunsuan=mingling;

            state=false;

        }else if("²✔".equals(mingling)){//判断语句 如果命令输入是平方立方法“/”就执行以下的操作 以下都是同理
            
     result=java.lang.Math.sqrt(Double.valueOf(jfield1.getText()));

     jfield1.setText("²✔");//开平方立方事件 是比较难 
  //   

     yunsuan=mingling;

     state=false;

 }else if("³✔".equals(mingling)){//判断语句 如果命令输入是开立方就执行以下的操作 以下都是同理
     
    result=Math.cbrt(Double.valueOf(jfield1.getText()));  //开立方

    jfield1.setText("³✔");
    yunsuan=mingling;
    state=false;

}
 else if("sin".equals(mingling)){//判断语句 如果命令输入是sin就执行以下的操作 以下都是同理
     
	 result=Math.sin(Math.PI/Double.valueOf(jfield1.getText()));  //
	 jfield1.setText("sin");
	 yunsuan=mingling;
	 state=false;

	 }
 else if("cos".equals(mingling)){//判断语句 如果命令输入是cos就执行以下的操作 以下都是同理
     
	 result=Math.cos(Math.PI/Double.valueOf(jfield1.getText()));  
	 jfield1.setText("cos");//平方立方事件 是比较难 
	 yunsuan=mingling;
	 state=false;
	 }
 else if("tan".equals(mingling)){//判断语句 如果命令输入是tan就执行以下的操作 以下都是同理
     
	 result=Math.tan(Math.PI/Double.valueOf(jfield1.getText()));  
	 jfield1.setText("tan");//平方立方事件 是比较难 
	 yunsuan=mingling;
	 state=false;
	 }
 
 else if("1/X".equals(mingling)){//判断语句 如果命令输入是1/x就执行以下的操作 以下都是同理
    
    result=1/(Double.valueOf(jfield1.getText()));  // 分数 真分数
    jfield1.setText("1/X");
    yunsuan=mingling;
    state=false;

}
 else if("X²".equals(mingling)){//判断语句 如果命令输入X²是就执行以下的操作 以下都是同理
                   
            result=Double.valueOf(jfield1.getText())*Double.valueOf(jfield1.getText());
            jfield1.setText("X²");
            yunsuan=mingling;
            state=false;
            
        }else if("X³".equals(mingling)){//判断语句 如果命令输入是X³就执行以下的操作 以下都是同理
            
     result=Double.valueOf(jfield1.getText())*Double.valueOf(jfield1.getText())*Double.valueOf(jfield1.getText());
     jfield1.setText("X³");
     yunsuan=mingling;
     state=false;

 }else if("=".equals(mingling)){//判断语句 如果命令输入是等号“=”就执行以下的操作 以下都是同理

            switch(yunsuan){
                case "+":result+=Double.valueOf(jfield1.getText());break;
                case "-":result-=Double.valueOf(jfield1.getText());break;
                case "/":result/=Double.valueOf(jfield1.getText());break;
                case "*":result*=Double.valueOf(jfield1.getText());break;       
            }

            jfield1.setText(String.valueOf(result));

        }else if("/".equals(mingling)){  // 判断语句 如果命令输入是除法“/”就执行以下的操作 以下都是同理

            result=1;
            result=Double.valueOf(jfield1.getText())/result;
            jfield1.setText("");
            yunsuan=mingling;

        }else if("*".equals(mingling)){//判断语句 如果命令输入是乘法“*”就执行以下的操作 以下都是同理

            result=1;
            result*=Double.valueOf(jfield1.getText());
            jfield1.setText("");
            yunsuan=mingling;

        }else if("删除".equals(mingling)){

        	jfield1.setText("");
            result=0.0;
            yunsuan="";

        }else if("退格".equals(mingling)){
        	
           if(jfield1.getText().length()>1){                   //文本框能输入 但不能退一 怎么退一找不到法子 
        	  jfield1.setText(jfield1.getText().substring(0, jfield1.getText().length()-1));//这个操作比较难
           }                                                   //  文本框不是数字  借鉴后 只能才文本长度里面入手 文本内容减一实现操作
           
           else if(jfield1.getText().length()==1){
        	   jfield1.setText("0");
           } 
           yunsuan="";
        }


	}
	
	
}

开发心得
按钮触发的数字显示在文本框中 并与 下一个事件按钮的相加或相减相乘相除 连接不上
使用 gettext ()
连接上去 并制作新的显示框 缺点是 不可以在文本框上连续输入 像 9+9 =
除法结构是最难的
因为分母不能为零 当下一步按零后 会出现出错
网上借鉴为 返回空白文本框
平方立方方法 单单是结果相乘会出错
可以用输入内容乘以输入内容实现 平方立方的方法

1、 个人总结
2、 else if("/".equals(mingling)){ // 判断语句 如果命令输入是除法“/”就执行以下的操作 以下都是同理

2文本框能输入 但不能退一 怎么退一找不到法子
文本框不是数字 借鉴后 只能才文本长度里面入手 文本内容减一实现操作
3 switch 选择语句比较简单 在老师的课本中有提及 但选择按钮的方法缺乏详细操作
可以上网借鉴模仿 实现的语句
4添加按钮事件比较容易 纯粹是设置位置就好 和Add 按钮 比较困难的是 计算的方法 监视器后 能实现和执行没有出错才是 核心步骤
没有 上面的核心步骤算法 制造出来的计算器只是一个空白文框
5平方立方事件 是比较难
无意中发现 只要是输入文本内容乘以输入文本内容或者应用math 类平方立方的使用 也可以实现 平方立方 但是前提必须是数字
6.很奇怪是啦 关于怎么实现开方立方 这个可讲知识了
这要引用到Math 类的 sqrt是开方 cbrt 是开立方 把输入的内容输入括号了 便实现了开方立方
7.颜色按钮是 引用color类 在指定的按钮后面setForeground(Color.orange) 便可实现颜色的使用啦
8关于实现sin tan cos 也是使用Math 类 但是 使用后就是弧度制吧
Math.PI 就是π这个意思
Sin30度 就是 math.sin【math.pi / 输入6】 就是度数了 是不是很简单啦
哈哈哈
9 log 这个函数就比较 难理解吧 网上有参考公式 可以
例如要10为底 就是log输入/log10 就实现了

10通过学习理解就觉得简单了
其实大多数方法都是在网上学习理解得来的 书本上教的有非常不错的地方 关于文件导入的方法就比较简略 所以要多方面学习才是 提升自己的方法 最重要的还是自己的动手能力和理解方法就好

白嫖了之后记得点赞转发一键三连支持一下 关注我 呗
此项目乃本人原创 转载请务必声明 侵犯本人创作权 违法必究

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java计算器GUI一个基于图形用户界面的计算器应用程序,可以用来执行各种数学运算,包括加、减、乘、除、求平方根、求倒数等。 以下是一个简单的Java计算器GUI的示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalculatorGUI extends JFrame implements ActionListener { private JTextField tfResult; private JButton[] btnNumbers; private JButton[] btnOps; private JButton btnEquals; private JButton btnClear; private String op = ""; private double num1 = 0; private double num2 = 0; public CalculatorGUI() { setTitle("Calculator"); setSize(300, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 4, 5, 5)); tfResult = new JTextField(); tfResult.setEditable(false); panel.add(tfResult); btnNumbers = new JButton[10]; for (int i = 0; i < btnNumbers.length; i++) { btnNumbers[i] = new JButton("" + i); btnNumbers[i].addActionListener(this); panel.add(btnNumbers[i]); } btnOps = new JButton[4]; btnOps[0] = new JButton("+"); btnOps[1] = new JButton("-"); btnOps[2] = new JButton("*"); btnOps[3] = new JButton("/"); for (int i = 0; i < btnOps.length; i++) { btnOps[i].addActionListener(this); panel.add(btnOps[i]); } btnEquals = new JButton("="); btnEquals.addActionListener(this); panel.add(btnEquals); btnClear = new JButton("C"); btnClear.addActionListener(this); panel.add(btnClear); add(panel); setVisible(true); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("+") || command.equals("-") || command.equals("*") || command.equals("/")) { op = command; num1 = Double.parseDouble(tfResult.getText()); tfResult.setText(""); } else if (command.equals("=")) { num2 = Double.parseDouble(tfResult.getText()); if (op.equals("+")) { tfResult.setText(String.valueOf(num1 + num2)); } else if (op.equals("-")) { tfResult.setText(String.valueOf(num1 - num2)); } else if (op.equals("*")) { tfResult.setText(String.valueOf(num1 * num2)); } else if (op.equals("/")) { tfResult.setText(String.valueOf(num1 / num2)); } } else if (command.equals("C")) { op = ""; num1 = 0; num2 = 0; tfResult.setText(""); } else { tfResult.setText(tfResult.getText() + command); } } public static void main(String[] args) { new CalculatorGUI(); } } ``` 这个计算器GUI应用程序使用了Java的Swing库来构建用户界面,包括文本框、数字按钮、运算符按钮、等号按钮和清除按钮。当用户点击数字按钮时,它们的值将显示在文本框中。当用户点击运算符按钮时,程序将记录运算符和第一个操作数的值。当用户点击等号按钮时,程序计算第二个操作数的值,并将结果显示在文本框中。当用户点击清除按钮时,程序将清除所有值并重新开始。 该程序使用了基本的数学运算符:加、减、乘、除。如果需要更多的数学运算符或功能,可以添加更多的按钮和相应的操作代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值