记事本写的科学计算器(源代码)

本文档提供了一个使用Java Swing实现的科学计算器的源代码,包含普通模式和科学模式,支持多种数学运算及操作,如平方根、指数、模运算等。计算器还具备模式切换、内存存储等功能。
摘要由CSDN通过智能技术生成
/**
 * Calculator
 * A shareware calculator
 *
 * @author {@link http://blog.csdn.net/hongweijin Bingbing Li}
 *
 * @recitation 0101 Matt Carlson
 *
 * @date 12/7/2005 12:08AM
 *
 */
 
 import javax.swing.*;
 import javax.swing.border.*;
 import java.awt.*;
 import java.awt.event.*;
 
 public class Calculator extends JFrame implements ActionListener
 {
     public static final int WIDTH = 800;            // width of the window                 
     public static final int HEIGHT = 600;           // height of the window                
     public static final int BUTTON_WIDTH = 80;      // width of the buttons                
     public static final int BUTTON_HEIGHT = 60;     // height of the buttons               
                                                                                            
     private CardLayout dealer;                      // card layout                         
     private JPanel        deckPanel;                   // used to card layout                 
                                                                                            
     private JTextField result;                      // the calculate result                
     private JCheckBoxMenuItem scientificMode;       // the menu item of the mode           
                                                                                            
     private Box vPanel1;                            // the first line buttons of the       
                                                        // scientific mode.                    
                                                                                            
     private JButton mod;                            // the modular button                  
     private JButton xey;                            // the button of Yth root of X         
     private JButton ms;                             // save button                         
    private JButton mr;                             // release the stored value            
     private    JButton mc;                             // clear the stored value              
                                                                                            
     private double head = 0.0;                      // the first number of the equation    
     private double tail = 0.0;                      // the second number of the equation   
                                                                                            
     private boolean substitution = true;            // whether substituted the answer or not
                                                                                            
     private String operatedCommand = "NOTHING";     // the current operated command        
     private String preOperatedCommand = "NOTHING";  // remember the pre-operated command
    
     private double variableMemory = 0.0;            // the variable of the memory
    
     private JButton jButtonR;                       // the register's button
     private JButton jButtonE;                       // the edit's button
    
     private JTextField nameField;                   // the field of the name
     private JTextField countryField;                // the field of the country
     private JTextField zipField;                    // the field of the zip
     private JTextField stateField;                  // the field of the state
     private JTextField cityField ;                  // the field of the city
     private JTextField streetAddressField;          // the field of the address
    
     private JTextField first;                       // the first part of the key
     private JTextField second;                      // the second part of the key
     private JTextField third;                       // the third part of the key
     private JTextField fourth;                      // the fourth part of the key
    
    
     public Calculator()
     {
         setSize(WIDTH, HEIGHT);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setTitle("Normal Calculator");
        
         Container contentPre = getContentPane();    // the container of the window
        
         dealer = new CardLayout();                  // create a new card layout
         deckPanel = new JPanel();                   
         deckPanel.setLayout(dealer);
        
         Box content = Box.createVerticalBox();      // create a new vertical Box
        
         Box registration = Box.createVerticalBox();
        
         /****************************************************
         *    menu
         *   file
         *****************************************************/
         JMenu fileMenu = new JMenu("File");        
         JMenuItem exitItemOfFile = new JMenuItem("Exit");
         exitItemOfFile.addActionListener(this);
         fileMenu.add(exitItemOfFile);
         /**
         *    menu
         *   settings
         */
         JMenu settingsMenu = new JMenu("Settings");
         JMenuItem registrationInfo = new JMenuItem("Registration Info");
         registrationInfo.addActionListener(this);
         settingsMenu.add(registrationInfo);
        
         scientificMode = new JCheckBoxMenuItem("Scientific Mode");
         scientificMode.addActionListener(this);
         settingsMenu.add(scientificMode);
        
         JMenuBar jMenuBar = new JMenuBar();
         jMenuBar.add(fileMenu);
         jMenuBar.add(settingsMenu);
         setJMenuBar(jMenuBar);
        
         /****************************************************
         *    textFiled panel
        *****************************************************/
       
        result = new JTextField("0");                     // the initiated value of the answer
        result.setBackground(Color.CYAN);                 // set the back ground color with cyan
        result.setHorizontalAlignment(JTextField.RIGHT);  // set the horizontal alignment 
       
       
        content.add(result);
            
         content.add(paintButtons());
        
         deckPanel.add("cal", content);                    // add the calculators card to the layout
        
         registration.add(paintRegistration());            // add the register window
        
         deckPanel.add("reg", registration);               // add the register window to the layout
        
         contentPre.add(deckPanel);                   // add the cards to the container
     }
    
     /**
     * paint the buttons of the two models.
     *
     */
     public Box paintButtons()
     {
         /****************************************************
        *    Buttons
        *****************************************************/
       
         /**
         *    line1
         */
         vPanel1 = Box.createVerticalBox();                                     
        
         // add the backwards's button                                                                          
         JButton backwards = new JButton("1/X");                                    
         backwards.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));      
         backwards.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));    
         backwards.addActionListener(this);                                         
         vPanel1.add(backwards);   
                                                         
         // add the factorial button                                                                           
         JButton factorial = new JButton("X!");                                     
         factorial.setMaximumSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));      
         factorial.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));    
         factorial.addActionListener(this);                                         
         vPanel1.add(factorial);                                  
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值