java计算器小程序

本文介绍了一个使用Java Swing构建的简易计算器应用程序。该计算器具备基本的数学运算功能,并实现了个性化界面风格选择和帮助信息展示。文章详细展示了计算器的实现过程,包括按钮事件处理、运算逻辑实现等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package java自练习;




 
import javax.swing.text.AttributeSet;  
import javax.swing.text.BadLocationException;  
import javax.swing.text.PlainDocument; 


import java.awt.Color;  
import java.awt.Container;  
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.JLabel;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JMenuItem;  
import javax.swing.JOptionPane;  
import javax.swing.JPanel;  
import javax.swing.JTextField;  
 
   class MyDocument extends PlainDocument{  
    int maxLength =20;  
     public MyDocument(int newMaxLength){  
         super();  
         maxLength = newMaxLength;  
     }  
      public MyDocument(){  
         this(20);  
      }   
      public void insertString(int offset,String str,AttributeSet a)throws BadLocationException{  
           if(getLength()+str.length()>maxLength){  
                return;  
           }  
           else{   
               super.insertString(offset,str,a);  
 
           }      
         }   
}  


 
 

 
 class CounterFrame extends JFrame implements ActionListener {  
 
   private static final long serialVersionUID = 1L;  
   //获得内容面板  
   private Container con=getContentPane();  
   //创建菜单栏,菜单及菜单项  
   private JMenuBar menuBar=new JMenuBar();  
   private JMenu viewmenu=new JMenu("查看(V)");  
   private JMenu helpmenu=new JMenu("帮助(H)");  
   private JMenuItem general =new JMenuItem("标准型(T)",'T');  
   private JMenuItem color =new JMenuItem("个性型(C)",'C');  
   private JMenuItem about=new JMenuItem("关于(A)",'A');  
   //创建一个容量为30个字符的文本框  
   private JTextField textField=new JTextField(30);  
   //创建JLabel  
   private JLabel label=new JLabel();  
   //创建计算器的各种按钮  
   private JButton[] button=new JButton[27];  
   private String[] buttonstr={"退格", "CE", "C", "MC", "7", "8", "9", "/", "sqrt",   
           "MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x",   
           "M+", "0", "+/-", ".", "+", "=" };  
   //创建各种面板  
   private JPanel panel=new JPanel();  
   private JPanel childPanel=new JPanel();  
   private JPanel childPanel1=new JPanel();  
   private JPanel childPanel2=new JPanel();  
   private JPanel childPanel3=new JPanel();  
   private JPanel childPanel4=new JPanel();  
   private JPanel childPanel5=new JPanel();  
   private JPanel childPanel6=new JPanel();  
   //创建布局管理器  
   private GridLayout gridLayout=new GridLayout(6,1,6,6);  
   private GridLayout gridLayout1=new GridLayout(1,3,6,6);  
   private GridLayout gridLayout2=new GridLayout(1,6,6,6);  
     
   boolean tem=true; //标识0  
   double  cache=0; //存储器  
   boolean temd=true; //标识小数点  
   boolean temm=false;//标识M+  
   boolean plus=false;  //标识加  
   boolean subtract=false; //标识减  
   boolean mul=false; //标识乘  
   boolean div=false; //标识除  
   boolean rem=false; //百分号  
     
     
   public CounterFrame(){  
       viewmenu.setMnemonic('V');  
       viewmenu.add(general);  
       general.addActionListener(new ActionListener(){  
 
           @Override  
           public void actionPerformed(ActionEvent e) {  
               childPanel.setBackground(Color.red);  
               childPanel1.setBackground(Color.red);  
               childPanel2.setBackground(Color.red);  
               childPanel3.setBackground(Color.red);  
               childPanel4.setBackground(Color.red);  
               childPanel5.setBackground(Color.red);  
               childPanel6.setBackground(Color.red);  
               panel.setBackground(Color.red);  
           }  
             
       });  
       viewmenu.add(color);  
       color.addActionListener(new ActionListener(){  
 
           @Override  
           public void actionPerformed(ActionEvent e) {  
               childPanel.setBackground(Color.blue);  
               childPanel1.setBackground(Color.cyan);  
               childPanel2.setBackground(Color.black);  
               childPanel3.setBackground(Color.darkGray);  
               childPanel4.setBackground(Color.green);  
               childPanel5.setBackground(Color.lightGray);  
               childPanel6.setBackground(Color.orange);  
               panel.setBackground(Color.red);  
                 
                 
                 
           }  
             
       });  
       helpmenu.setMnemonic('H');  
       helpmenu.add(about);  
       about.addActionListener(new ActionListener(){  
 
           @Override  
           public void actionPerformed(ActionEvent e) {  
                JOptionPane.showMessageDialog(CounterFrame.this,"计算器1.0 作者:wustrive_2008",  
                           " ",JOptionPane.INFORMATION_MESSAGE);  
           }  
             
       });  
         
       menuBar.add(viewmenu);  
       menuBar.add(helpmenu);  
       this.setJMenuBar(menuBar);  
         
       for(int i=0;i<27;i++){  
           button[i]=new JButton(buttonstr[i]);  
           button[i].addActionListener(this);  
       }  
         
       panel.setLayout(gridLayout);  
       panel.add(childPanel1);  
       childPanel1.add(label);  
       childPanel1.add(textField);  
       label.setBounds(10, 10, 10, 10);  
       textField.setEditable(false);  
       textField.setDocument(new MyDocument());  
       textField.setText("0");  
       textField.setBackground(Color.WHITE);  
       textField.setHorizontalAlignment(JTextField.RIGHT);//设置文字从右边开始显示  
         
       panel.add(childPanel2);  
       childPanel2.setLayout(gridLayout1);  
       childPanel2.add(button[0]);  
       childPanel2.add(button[1]);  
       childPanel2.add(button[2]);  
         
       panel.add(childPanel3);  
       childPanel3.setLayout(gridLayout2);  
       childPanel3.add(button[3]);  
       childPanel3.add(button[4]);  
       childPanel3.add(button[5]);  
       childPanel3.add(button[6]);  
       childPanel3.add(button[7]);  
       childPanel3.add(button[8]);  
         
       panel.add(childPanel4);  
       childPanel4.setLayout(gridLayout2);  
       childPanel4.add(button[9]);  
       childPanel4.add(button[10]);  
       childPanel4.add(button[11]);  
       childPanel4.add(button[12]);  
       childPanel4.add(button[13]);  
       childPanel4.add(button[14]);  
         
       panel.add(childPanel5);  
       childPanel5.setLayout(gridLayout2);  
       childPanel5.add(button[15]);  
       childPanel5.add(button[16]);  
       childPanel5.add(button[17]);  
       childPanel5.add(button[18]);  
       childPanel5.add(button[19]);  
       childPanel5.add(button[20]);  
         
       panel.add(childPanel6);  
       childPanel6.setLayout(gridLayout2);  
       childPanel6.add(button[21]);  
       childPanel6.add(button[22]);  
       childPanel6.add(button[23]);  
       childPanel6.add(button[24]);  
       childPanel6.add(button[25]);  
       childPanel6.add(button[26]);  
         
         
       childPanel.add(panel);  
       con.add(childPanel);  
         
         
       this.setResizable(false);  
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       this.setBounds(400, 300, 400, 300);  
       this.setTitle("计算器");  
       this.setVisible(true);  
         
   }  
         
   //按钮事件处理  
   @Override  
   public void actionPerformed(ActionEvent e) {  
       if(e.getSource()==button[4]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[4]);  
           }  
           else{  
               textField.setText(buttonstr[4]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[5]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[5]);  
           }  
           else{  
               textField.setText(buttonstr[5]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[6]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[6]);  
           }  
           else{  
               textField.setText(buttonstr[6]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[10]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[10]);  
           }  
           else{  
               textField.setText(buttonstr[10]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[11]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[11]);  
           }  
           else{  
               textField.setText(buttonstr[11]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[12]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[12]);  
           }  
           else{  
               textField.setText(buttonstr[12]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[16]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[16]);  
           }  
           else{  
               textField.setText(buttonstr[16]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[17]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[17]);  
           }  
           else{  
               textField.setText(buttonstr[17]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[18]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[18]);  
           }  
           else{  
               textField.setText(buttonstr[18]);  
               tem=false;  
           }  
       }  
       if(e.getSource()==button[22]){  
           if(tem==false){  
               textField.setText(textField.getText()+buttonstr[22]);  
           }  
           else{  
               textField.setText(buttonstr[22]);  
               //tem=false;  
           }  
       }  
       if(e.getSource()==button[0]){  
           if(textField.getText().length()>1){  
               textField.setText(textField.getText().substring(0,textField.getText().length()-1));  
           }  
           else{  
               textField.setText("0");  
               tem=true;  
           }  
       }  
       //CE键:清除当前显示的数据  
       if(e.getSource()==button[1]){  
           textField.setText("0");  
           tem=true;  
           tem=true;  
           temd=true;  
           temm=false;  
           plus=false;  
           subtract=false;  
           mul=false;  
           div=false;  
           rem=false;  
             
             
       }  
         
       //C键:清除所有数据  
       if(e.getSource()==button[2]){  
           textField.setText("0");  
           cache=0;  
           tem=true;  
           tem=true;  
           cache=0;  
           temd=true;  
           temm=false;  
           plus=false;  
           subtract=false;  
           mul=false;  
           div=false;  
           rem=false;  
             
       }  
         
       //MR键:将存于存储器中的数据显示出来  
       if(e.getSource()==button[9]){  
           textField.setText(""+cache+"");  
       }  
       //MC键:清除存储器中数据  
       if(e.getSource()==button[3]){  
           cache=0;  
           label.setText("");  
       }  
         
         
       //MS键:将显示的数据存储在存储器中  
       if(e.getSource()==button[15]){  
           cache=Double.parseDouble(textField.getText());  
       }  
         
       //M+键:将显示的数据与存储器中的数据相加并进行存储  
       if(e.getSource()==button[21]){  
               label.setText("M");  
               cache=cache+Double.parseDouble(textField.getText());  
               temm=true;  
             
       }  
       //处理小数点  
       if(e.getSource()==button[24]){  
           if(tem==false&&temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               textField.setText(textField.getText()+".");  
               temd=false;  
           }  
       }  
       //处理1/x  
       if(e.getSource()==button[20]){  
           if(temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               textField.setText(""+1/Double.parseDouble(textField.getText())+"");  
               tem=true;  
           }  
       }  
       //处理+/-  
       if(e.getSource()==button[23]){  
           if(temd==true&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               Double dou=Double.parseDouble(textField.getText());  
               dou=-dou;  
               textField.setText(""+dou+"");  
                 
           }  
       }  
       //除法  
       if(e.getSource()==button[7]){  
           if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               textField.setText(textField.getText()+"/");  
               div=true;  
           }  
       }  
       //乘法  
       if(e.getSource()==button[13]){  
           if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               textField.setText(textField.getText()+"*");  
               mul=true;  
           }  
       }  
       //百分数表示  
       if(e.getSource()==button[14]){  
           if((plus==true&&subtract==false&&mul==false&&div==false)||(plus==false&&subtract==true&&mul==false&&div==false)||  
                   (plus==false&&subtract==false&&mul==true&&div==false)||(plus==false&&subtract==false&&mul==false&&div==true)){  
               if(plus==true){  
                   int i=textField.getText().indexOf("+");  
                   String substr1=textField.getText().substring(0,i);  
                   String substr2=textField.getText().substring(i+1,textField.getText().length());  
                   textField.setText(""+(Double.parseDouble(substr1)+Double.parseDouble(substr1)*Double.parseDouble(substr2)/100)+"");  
               }  
               if(subtract==true){  
                   int i=textField.getText().indexOf("-");  
                   String substr1=textField.getText().substring(0,i);  
                   String substr2=textField.getText().substring(i+1,textField.getText().length());  
                   textField.setText(""+(Double.parseDouble(substr1)-Double.parseDouble(substr1)*Double.parseDouble(substr2)/100)+"");  
               }  
               if(mul==true){  
                   int i=textField.getText().indexOf("*");  
                   String substr1=textField.getText().substring(0,i);  
                   String substr2=textField.getText().substring(i+1,textField.getText().length());  
                   textField.setText(""+(Double.parseDouble(substr1)*(Double.parseDouble(substr1)*Double.parseDouble(substr2)/100))+"");  
               }  
               if(subtract==true){  
                   int i=textField.getText().indexOf("/");  
                   String substr1=textField.getText().substring(0,i);  
                   String substr2=textField.getText().substring(i+1,textField.getText().length());  
                   textField.setText(""+(Double.parseDouble(substr1)/(Double.parseDouble(substr1)*Double.parseDouble(substr2)/100))+"");  
               }  
           }  
                 
             
       }  
       //加法  
       if(e.getSource()==button[25]){  
           if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               textField.setText(textField.getText()+"+");  
               plus=true;  
           }  
       }  
       //减法  
       if(e.getSource()==button[19]){  
           if(tem==false&&plus==false&&subtract==false&&mul==false&&rem==false&&div==false){  
               textField.setText(textField.getText()+"-");  
               subtract=true;  
           }  
       }  
       //等于  
       if(e.getSource()==button[26]){  
           if(plus==true){  
               int i=textField.getText().indexOf("+");  
               String substr1=textField.getText().substring(0,i);  
               String substr2=textField.getText().substring(i+1,textField.getText().length());  
               if(substr2.length()==0){  
                   textField.setText(""+substr1+"");  
               }  
               else{  
                   double result=Double.parseDouble(substr1)+Integer.parseInt(substr2);  
                   textField.setText(""+result+"");  
               }  
                 
               plus=false;  
               tem=true;  
           }  
           if(subtract==true){  
               int i=textField.getText().indexOf("-");  
               String substr1=textField.getText().substring(0,i);  
               String substr2=textField.getText().substring(i+1,textField.getText().length());  
               if(substr2.length()==0){  
                   textField.setText(""+substr1+"");  
               }  
               else{  
                   double result=Double.parseDouble(substr1)-Integer.parseInt(substr2);  
                   textField.setText(""+result+"");  
               }  
                 
               subtract=false;  
               tem=true;  
           }  
           if(mul==true){  
               int i=textField.getText().indexOf("*");  
               String substr1=textField.getText().substring(0,i);  
               String substr2=textField.getText().substring(i+1,textField.getText().length());  
               if(substr2.length()==0){  
                   textField.setText(""+substr1+"");  
               }  
               else{  
                   double result=Double.parseDouble(substr1)*Integer.parseInt(substr2);  
                   textField.setText(""+result+"");  
               }  
                 
               mul=false;  
               tem=true;  
           }  
           if(div==true){  
               int i=textField.getText().indexOf("/");  
               String substr1=textField.getText().substring(0,i);  
               String substr2=textField.getText().substring(i+1,textField.getText().length());  
               if(substr2.length()==0){  
                   textField.setText(""+substr1+"");  
               }  
               else if(Double.parseDouble(substr2)==0){  
                   textField.setText("除数不能为零");  
               }  
               else{  
                   double result=Double.parseDouble(substr1)/Double.parseDouble(substr2);  
                   textField.setText(""+result+"");  
               }  
                 
               div=false;  
               tem=true;  
           }  
       }  
       //求平方根  
       if(e.getSource()==button[8]){  
           if(plus==false&&subtract==false&&mul==false&&rem==false&&div==false&&tem==false){  
               textField.setText(""+Math.sqrt(Double.parseDouble(textField.getText()))+"");  
               tem=true;  
           }  
             
       }  
   }  
   public static void main(String[] args){
   CounterFrame coun=new  CounterFrame();
}

    
 
}  
 
 
 
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值