首页 > 代码库 > Java计算器

Java计算器

代码

  1 import javax.swing.*;
  2 import java.awt.*;
  3 import java.awt.event.*;
  4 
  5 public class Calculator extends JFrame{
  6     private String[] labels = {
  7             "%","√","x2","1/x",
  8             "CE","C","←","÷",
  9             "7","8","9","×",
 10             "4","5","6","-",
 11             "1","2","3","+",
 12             "±","0",".","="};
 13     private String a="",b="";
 14     private int op=-1;
 15     JTextField comp;
 16     
 17     public Calculator(){    
 18         setLayout(new BorderLayout());    
 19         
 20         JPanel panel=new JPanel();
 21         panel.setLayout(null);
 22         comp=new JTextField("0",18);
 23         comp.setHorizontalAlignment(JTextField.RIGHT);
 24         comp.setBounds(10, 8, 271, 50);
 25         panel.add(comp);     
 26         
 27         for(int i=0;i<24;i++) {
 28             JButton button=new JButton(labels[i]);
 29             if(labels[i].length()==1 && labels[i].charAt(0)>=‘0‘ && labels[i].charAt(0)<=‘9‘) 
 30                 button.addActionListener(new InsertAction());
 31             else if(labels[i]==".") 
 32                 button.addActionListener(new InsertAction());
 33             else 
 34                 button.addActionListener(new CommandAction());
 35             
 36             int x=i%4*70+10,y=i/4*48+70;
 37             button.setLayout(null);
 38             button.setBounds(x,y,60,40);
 39             panel.add(button);
 40         }      
 41         add(panel,BorderLayout.CENTER);
 42     }
 43     
 44     private class InsertAction implements ActionListener{
 45         public void actionPerformed(ActionEvent e){
 46             String s=e.getActionCommand();
 47             if(s!="." || b.indexOf(".")==-1) b+=s;
 48             comp.setText(b);
 49         }
 50     }
 51        
 52     private class CommandAction implements ActionListener{
 53         public void actionPerformed(ActionEvent e){
 54             String s=e.getActionCommand();
 55             if(s=="+"||s=="-"||s=="×"||s=="÷"){
 56                 if(op!=-1) b=a;
 57                 a=b;
 58                 b="";
 59                 comp.setText(s);
 60                 switch(s){
 61                 case "+":op=1;break;
 62                 case "-":op=2;break;
 63                 case "×":op=3;break;
 64                 case "÷":op=4;break;
 65                 }
 66                 return;
 67             }
 68             else if(s=="="){
 69                 double A=Double.parseDouble(a);
 70                 double B=Double.parseDouble(b);
 71                 switch(op){
 72                 case 1:A+=B;break;
 73                 case 2:A-=B;break;
 74                 case 3:A*=B;break;
 75                 case 4:A/=B;break;
 76                 }
 77                 a=A+"";
 78                 comp.setText(a);
 79             }
 80             else if(s=="±"||s=="√"||s=="x2"||s=="1/x"){
 81                 double B=Double.parseDouble(b);
 82                 switch(s){
 83                 case "±":B=-B;break;
 84                 case "√":B=Math.sqrt(B);break;
 85                 case "x2":B*=B;break;
 86                 case "1/x":B=1/B;break;               
 87                 }
 88                 b=B+"";
 89                 comp.setText(b);
 90             }
 91             else if(s=="←"){
 92                 if(b.length()!=0){
 93                     b=b.substring(0, b.length()-1);
 94                     comp.setText(b);
 95                 }
 96             }
 97             else if(s.charAt(0)==‘C‘){
 98                 a=b="";            
 99                 op=-1;
100                 comp.setText("");
101             }
102         }
103     }
104     
105     public static void main(String[] args) {
106         EventQueue.invokeLater(()-> {
107             JFrame frame = new Calculator();
108             frame.setTitle("Calculator");
109             frame.setResizable(false);
110             frame.setLocationRelativeTo(null);
111             frame.setSize(297, 390);
112             frame.setVisible(true);
113             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
114         });
115     }
116 }

效果图

技术分享

按键顺序参照Win10计算器:

技术分享

ps:除了%不知道有啥用,还有CE和C的功能写的一样之外,其他都实现了

但是不停的更换操作指令的时候,有时候的结果不符合逻辑,不过两个数或者一个数的运算都是没有问题的

 

Java计算器