rootUser

To make calculator

May 26th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package calculator;
  2.  
  3. public class Calculator
  4. {
  5.     public static void main(String[] args) throws Exception
  6.     {
  7.         MyCalculator ob = new MyCalculator();
  8.         ob.setVisible(true);
  9.     }
  10. }
  11.  
  12. public class MyCalculator extends javax.swing.JFrame
  13. {
  14.     double num1,num2,result;
  15.     String n1,n2,nr;
  16.     /**
  17.      * Creates new form MyCalculator
  18.      */
  19.     public MyCalculator() throws Exception
  20.     {
  21.         initComponents();
  22.     }
  23.  
  24.     /**
  25.      * This method is called from within the constructor to initialize the form.
  26.      * WARNING: Do NOT modify this code. The content of this method is always
  27.      * regenerated by the Form Editor.
  28.      */
  29.     @SuppressWarnings("unchecked")
  30.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  31.     private void initComponents() {
  32.  
  33.         num1TextField = new javax.swing.JTextField();
  34.         num2TextField = new javax.swing.JTextField();
  35.         resultTextField = new javax.swing.JTextField();
  36.         AddButton = new javax.swing.JButton();
  37.         SubstractButton = new javax.swing.JButton();
  38.         MultiplyButton = new javax.swing.JButton();
  39.         DivideButton = new javax.swing.JButton();
  40.         ModulusButton = new javax.swing.JButton();
  41.         PercentageButton = new javax.swing.JButton();
  42.         LogarithmButtom = new javax.swing.JButton();
  43.         KilometerToMileConvertButton = new javax.swing.JButton();
  44.         CelsiusToFahrenheitConvertButton = new javax.swing.JButton();
  45.         ClearButton = new javax.swing.JButton();
  46.         jLabel1 = new javax.swing.JLabel();
  47.         jLabel2 = new javax.swing.JLabel();
  48.  
  49.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  50.  
  51.         num1TextField.addActionListener(new java.awt.event.ActionListener() {
  52.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  53.                 num1TextFieldActionPerformed(evt);
  54.             }
  55.         });
  56.  
  57.         num2TextField.addActionListener(new java.awt.event.ActionListener() {
  58.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  59.                 num2TextFieldActionPerformed(evt);
  60.             }
  61.         });
  62.  
  63.         resultTextField.addActionListener(new java.awt.event.ActionListener() {
  64.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  65.                 resultTextFieldActionPerformed(evt);
  66.             }
  67.         });
  68.  
  69.         AddButton.setText("ADD");
  70.         AddButton.addActionListener(new java.awt.event.ActionListener() {
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  72.                 AddButtonActionPerformed(evt);
  73.             }
  74.         });
  75.  
  76.         SubstractButton.setText("SUBSTRACT");
  77.         SubstractButton.addActionListener(new java.awt.event.ActionListener() {
  78.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  79.                 SubstractButtonActionPerformed(evt);
  80.             }
  81.         });
  82.  
  83.         MultiplyButton.setText("MULTIPLY");
  84.         MultiplyButton.addActionListener(new java.awt.event.ActionListener() {
  85.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  86.                 MultiplyButtonActionPerformed(evt);
  87.             }
  88.         });
  89.  
  90.         DivideButton.setText("DIVIDE");
  91.         DivideButton.addActionListener(new java.awt.event.ActionListener() {
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  93.                 DivideButtonActionPerformed(evt);
  94.             }
  95.         });
  96.  
  97.         ModulusButton.setText("MODULUS");
  98.         ModulusButton.addActionListener(new java.awt.event.ActionListener() {
  99.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  100.                 ModulusButtonActionPerformed(evt);
  101.             }
  102.         });
  103.  
  104.         PercentageButton.setText("PERCENTAGE");
  105.         PercentageButton.addActionListener(new java.awt.event.ActionListener() {
  106.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  107.                 PercentageButtonActionPerformed(evt);
  108.             }
  109.         });
  110.  
  111.         LogarithmButtom.setText("LOGARITHM");
  112.         LogarithmButtom.addActionListener(new java.awt.event.ActionListener() {
  113.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  114.                 LogarithmButtomActionPerformed(evt);
  115.             }
  116.         });
  117.  
  118.         KilometerToMileConvertButton.setText("KM --> MILE");
  119.         KilometerToMileConvertButton.addActionListener(new java.awt.event.ActionListener() {
  120.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  121.                 KilometerToMileConvertButtonActionPerformed(evt);
  122.             }
  123.         });
  124.  
  125.         CelsiusToFahrenheitConvertButton.setText("*C --> *F");
  126.         CelsiusToFahrenheitConvertButton.addActionListener(new java.awt.event.ActionListener() {
  127.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  128.                 CelsiusToFahrenheitConvertButtonActionPerformed(evt);
  129.             }
  130.         });
  131.  
  132.         ClearButton.setText("CLEAR");
  133.         ClearButton.addActionListener(new java.awt.event.ActionListener() {
  134.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  135.                 ClearButtonActionPerformed(evt);
  136.             }
  137.         });
  138.  
  139.         jLabel1.setText("Operation :");
  140.  
  141.         jLabel2.setText("Convert:");
  142.  
  143.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  144.         getContentPane().setLayout(layout);
  145.         layout.setHorizontalGroup(
  146.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.             .addGroup(layout.createSequentialGroup()
  148.                 .addGap(22, 22, 22)
  149.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150.                     .addGroup(layout.createSequentialGroup()
  151.                         .addComponent(ClearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
  152.                         .addContainerGap())
  153.                     .addGroup(layout.createSequentialGroup()
  154.                         .addGap(0, 0, Short.MAX_VALUE)
  155.                         .addComponent(num1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
  156.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  157.                         .addComponent(num2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
  158.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  159.                         .addComponent(resultTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                         .addContainerGap(60, Short.MAX_VALUE))
  161.                     .addGroup(layout.createSequentialGroup()
  162.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  163.                             .addComponent(ModulusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  164.                             .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  165.                             .addComponent(KilometerToMileConvertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
  166.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  167.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.                             .addGroup(layout.createSequentialGroup()
  169.                                 .addComponent(CelsiusToFahrenheitConvertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  170.                                 .addGap(0, 0, Short.MAX_VALUE))
  171.                             .addGroup(layout.createSequentialGroup()
  172.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173.                                     .addGroup(layout.createSequentialGroup()
  174.                                         .addComponent(SubstractButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  175.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  176.                                         .addComponent(MultiplyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
  177.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  178.                                         .addComponent(DivideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
  179.                                     .addGroup(layout.createSequentialGroup()
  180.                                         .addComponent(PercentageButton)
  181.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  182.                                         .addComponent(LogarithmButtom, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
  183.                                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
  184.             .addGroup(layout.createSequentialGroup()
  185.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  186.                     .addGroup(layout.createSequentialGroup()
  187.                         .addGap(36, 36, 36)
  188.                         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
  189.                     .addGroup(layout.createSequentialGroup()
  190.                         .addGap(39, 39, 39)
  191.                         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
  192.                 .addGap(0, 0, Short.MAX_VALUE))
  193.         );
  194.         layout.setVerticalGroup(
  195.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  196.             .addGroup(layout.createSequentialGroup()
  197.                 .addGap(20, 20, 20)
  198.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  199.                     .addComponent(num1TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
  200.                     .addComponent(num2TextField)
  201.                     .addComponent(resultTextField))
  202.                 .addGap(14, 14, 14)
  203.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  204.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  205.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  206.                     .addComponent(AddButton)
  207.                     .addComponent(SubstractButton)
  208.                     .addComponent(MultiplyButton)
  209.                     .addComponent(DivideButton))
  210.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  211.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  212.                     .addComponent(ModulusButton)
  213.                     .addComponent(PercentageButton)
  214.                     .addComponent(LogarithmButtom))
  215.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  216.                 .addComponent(jLabel2)
  217.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  218.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  219.                     .addComponent(KilometerToMileConvertButton)
  220.                     .addComponent(CelsiusToFahrenheitConvertButton))
  221.                 .addGap(18, 18, 18)
  222.                 .addComponent(ClearButton)
  223.                 .addContainerGap(27, Short.MAX_VALUE))
  224.         );
  225.  
  226.         pack();
  227.     }// </editor-fold>                        
  228.  
  229.     private void resultTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                                
  230.         // TODO add your handling code here:
  231.     }                                              
  232.  
  233.     private void num2TextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
  234.         // TODO add your handling code here:
  235.     }                                            
  236.  
  237.     private void DivideButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  238.         // TODO add your handling code here:
  239.         num1=Integer.parseInt(num1TextField.getText());
  240.         num2=Integer.parseInt(num2TextField.getText());
  241.         try
  242.         {
  243.             result=num1/num2;
  244.         }
  245.         catch(Exception e)
  246.         {
  247.             System.out.println(""+e);
  248.             e.printStackTrace();
  249.         }
  250.         resultTextField.setText(""+result);
  251.     }                                            
  252.  
  253.     private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  254.         // TODO add your handling code here:
  255.         num1=Integer.parseInt(num1TextField.getText());
  256.         num2=Integer.parseInt(num2TextField.getText());
  257.         result=num1+num2;
  258.    
  259.         resultTextField.setText(""+result);
  260.        
  261.     }                                        
  262.  
  263.     private void ModulusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
  264.         // TODO add your handling code here:
  265.         num1=Integer.parseInt(num1TextField.getText());
  266.         num2=Integer.parseInt(num2TextField.getText());
  267.         result=num1%num2;
  268.         resultTextField.setText(""+result);
  269.     }                                            
  270.  
  271.     private void MultiplyButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
  272.         // TODO add your handling code here:
  273.         num1=Integer.parseInt(num1TextField.getText());
  274.         num2=Integer.parseInt(num2TextField.getText());
  275.         result=num1*num2;
  276.         resultTextField.setText(""+result);
  277.     }                                              
  278.  
  279.     private void SubstractButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
  280.         // TODO add your handling code here:
  281.         num1=Integer.parseInt(num1TextField.getText());
  282.         num2=Integer.parseInt(num2TextField.getText());
  283.         result=num1-num2;
  284.         resultTextField.setText(""+result);
  285.     }                                              
  286.  
  287.     private void KilometerToMileConvertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                            
  288.         // TODO add your handling code here:
  289.         n1 = num1TextField.getText();
  290.         boolean f = true ;
  291.         for(int i = 0 ; i<n1.length();i++){
  292.             if(n1.charAt(i)<'0' || n1.charAt(i)>'9'){
  293.                 f = false ;
  294.                 break;
  295.             }
  296.         }
  297.         if(f){
  298.             num1=Integer.parseInt(n1); ;
  299.             result=num1*0.621371;
  300.         }
  301.         if(f)
  302.             resultTextField.setText(""+result);
  303.         else
  304.             resultTextField.setText("invalid io.");
  305.        
  306.     }                                                            
  307.  
  308.     private void PercentageButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
  309.         // TODO add your handling code here:
  310.         num1=Integer.parseInt(num1TextField.getText());
  311.         num2=Integer.parseInt(num2TextField.getText());
  312.         result=(num1*num2)/100;
  313.         resultTextField.setText(""+result);
  314.     }                                                
  315.  
  316.     private void LogarithmButtomActionPerformed(java.awt.event.ActionEvent evt) {                                                
  317.         // TODO add your handling code here:
  318.         n1 = num1TextField.getText();
  319.         boolean f = true ;
  320.         for(int i = 0 ; i<n1.length();i++){
  321.             if(n1.charAt(i)<'0' || n1.charAt(i)>'9'){
  322.                 f = false ;
  323.                 break;
  324.             }
  325.         }
  326.         if(f){
  327.             num1=Integer.parseInt(n1);
  328.             result=Math.log(num1);
  329.         }
  330.         if(f)
  331.             resultTextField.setText(""+result);
  332.         else
  333.             resultTextField.setText("invalid io.");
  334.     }                                              
  335.  
  336.     private void CelsiusToFahrenheitConvertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                                
  337.         // TODO add your handling code here:
  338.         n1 = num1TextField.getText(); ;
  339.         boolean f = true ;
  340.         for(int i = 0 ; i<n1.length();i++){
  341.             if(n1.charAt(i)<'0' || n1.charAt(i)>'9'){
  342.                 f = false ;
  343.                 break;
  344.             }
  345.         }
  346.         if(f){
  347.             num1=Integer.parseInt(n1); ;
  348.             result=num1*33.8;
  349.         }
  350.         if(f)
  351.             resultTextField.setText(""+result);
  352.         else
  353.             resultTextField.setText("invalid io.");
  354.        
  355.        
  356.     }                                                                
  357.  
  358.     private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  359.         // TODO add your handling code here:
  360.         num1TextField.setText("");
  361.         num2TextField.setText("");
  362.         resultTextField.setText("");
  363.        
  364.     }                                          
  365.  
  366.     private void num1TextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                              
  367.         // TODO add your handling code here:
  368.     }                                            
  369.  
  370.     /**
  371.      * @param args the command line arguments
  372.      */
  373.     public static void main(String args[]) {
  374.         /* Set the Nimbus look and feel */
  375.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  376.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  377.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  378.          */
  379.         try {
  380.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  381.                 if ("Nimbus".equals(info.getName())) {
  382.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  383.                     break;
  384.                 }
  385.             }
  386.         } catch (ClassNotFoundException ex) {
  387.             java.util.logging.Logger.getLogger(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  388.         } catch (InstantiationException ex) {
  389.             java.util.logging.Logger.getLogger(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  390.         } catch (IllegalAccessException ex) {
  391.             java.util.logging.Logger.getLogger(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  392.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  393.             java.util.logging.Logger.getLogger(MyCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  394.         }
  395.         //</editor-fold>
  396.  
  397.         /* Create and display the form */
  398.         java.awt.EventQueue.invokeLater(new Runnable() {
  399.             public void run() {
  400.                 try {
  401.                     new MyCalculator().setVisible(true);
  402.                 } catch (Exception ex) {
  403.                     //Logger.getLogger(MyCalculator.class.getName()).log(Level.SEVERE, null, ex);
  404.                     System.out.println(""+ex);
  405.                     ex.printStackTrace();
  406.                 }
  407.             }
  408.         });
  409.     }
  410.  
  411.     // Variables declaration - do not modify                    
  412.     private javax.swing.JButton AddButton;
  413.     private javax.swing.JButton CelsiusToFahrenheitConvertButton;
  414.     private javax.swing.JButton ClearButton;
  415.     private javax.swing.JButton DivideButton;
  416.     private javax.swing.JButton KilometerToMileConvertButton;
  417.     private javax.swing.JButton LogarithmButtom;
  418.     private javax.swing.JButton ModulusButton;
  419.     private javax.swing.JButton MultiplyButton;
  420.     private javax.swing.JButton PercentageButton;
  421.     private javax.swing.JButton SubstractButton;
  422.     private javax.swing.JLabel jLabel1;
  423.     private javax.swing.JLabel jLabel2;
  424.     private javax.swing.JTextField num1TextField;
  425.     private javax.swing.JTextField num2TextField;
  426.     private javax.swing.JTextField resultTextField;
  427.     // End of variables declaration                  
  428. }
Add Comment
Please, Sign In to add comment