Advertisement
Gireada

IOC Calculator

Nov 1st, 2017
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.79 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package lab3;
  7.  
  8. /**
  9.  *
  10.  * @author Iulian Grigore
  11.  */
  12. public class Calculator extends javax.swing.JFrame {
  13.  
  14.     /**
  15.      * Creates new form Calculator
  16.      */
  17.     public Calculator() {
  18.         initComponents();
  19.     }
  20.  
  21.     /**
  22.      * This method is called from within the constructor to initialize the form.
  23.      * WARNING: Do NOT modify this code. The content of this method is always
  24.      * regenerated by the Form Editor.
  25.      */
  26.     @SuppressWarnings("unchecked")
  27.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  28.     private void initComponents() {
  29.  
  30.         jButton1 = new javax.swing.JButton();
  31.         jButton2 = new javax.swing.JButton();
  32.         jButton3 = new javax.swing.JButton();
  33.         jButton4 = new javax.swing.JButton();
  34.         jButton5 = new javax.swing.JButton();
  35.         jButton6 = new javax.swing.JButton();
  36.         jButton7 = new javax.swing.JButton();
  37.         jButton8 = new javax.swing.JButton();
  38.         jButton9 = new javax.swing.JButton();
  39.         jButton10 = new javax.swing.JButton();
  40.         jButton11 = new javax.swing.JButton();
  41.         jButton12 = new javax.swing.JButton();
  42.         jTextField1 = new javax.swing.JTextField();
  43.         jTextField2 = new javax.swing.JTextField();
  44.  
  45.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  46.  
  47.         jButton1.setText("1");
  48.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  49.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  50.                 jButton1ActionPerformed(evt);
  51.             }
  52.         });
  53.  
  54.         jButton2.setText("4");
  55.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  56.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  57.                 jButton2ActionPerformed(evt);
  58.             }
  59.         });
  60.  
  61.         jButton3.setText("7");
  62.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  63.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  64.                 jButton3ActionPerformed(evt);
  65.             }
  66.         });
  67.  
  68.         jButton4.setText("8");
  69.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  70.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  71.                 jButton4ActionPerformed(evt);
  72.             }
  73.         });
  74.  
  75.         jButton5.setText("5");
  76.         jButton5.addActionListener(new java.awt.event.ActionListener() {
  77.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  78.                 jButton5ActionPerformed(evt);
  79.             }
  80.         });
  81.  
  82.         jButton6.setText("2");
  83.         jButton6.addActionListener(new java.awt.event.ActionListener() {
  84.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  85.                 jButton6ActionPerformed(evt);
  86.             }
  87.         });
  88.  
  89.         jButton7.setText("9");
  90.         jButton7.addActionListener(new java.awt.event.ActionListener() {
  91.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  92.                 jButton7ActionPerformed(evt);
  93.             }
  94.         });
  95.  
  96.         jButton8.setText("6");
  97.         jButton8.addActionListener(new java.awt.event.ActionListener() {
  98.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  99.                 jButton8ActionPerformed(evt);
  100.             }
  101.         });
  102.  
  103.         jButton9.setText("3");
  104.         jButton9.addActionListener(new java.awt.event.ActionListener() {
  105.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  106.                 jButton9ActionPerformed(evt);
  107.             }
  108.         });
  109.  
  110.         jButton10.setText("+");
  111.         jButton10.addActionListener(new java.awt.event.ActionListener() {
  112.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  113.                 jButton10ActionPerformed(evt);
  114.             }
  115.         });
  116.  
  117.         jButton11.setText("-");
  118.         jButton11.addActionListener(new java.awt.event.ActionListener() {
  119.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  120.                 jButton11ActionPerformed(evt);
  121.             }
  122.         });
  123.  
  124.         jButton12.setText("CLEAR");
  125.         jButton12.addActionListener(new java.awt.event.ActionListener() {
  126.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  127.                 jButton12ActionPerformed(evt);
  128.             }
  129.         });
  130.  
  131.         jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
  132.         jTextField1.setToolTipText("");
  133.         jTextField1.addActionListener(new java.awt.event.ActionListener() {
  134.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  135.                 jTextField1ActionPerformed(evt);
  136.             }
  137.         });
  138.  
  139.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  140.         getContentPane().setLayout(layout);
  141.         layout.setHorizontalGroup(
  142.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  143.             .addGroup(layout.createSequentialGroup()
  144.                 .addGap(40, 40, 40)
  145.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  146.                     .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
  147.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  148.                         .addComponent(jTextField1)
  149.                         .addGroup(layout.createSequentialGroup()
  150.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  151.                                 .addGroup(layout.createSequentialGroup()
  152.                                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  153.                                         .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
  154.                                         .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  155.                                         .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  156.                                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  157.                                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  158.                                         .addGroup(layout.createSequentialGroup()
  159.                                             .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  161.                                             .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
  162.                                         .addGroup(layout.createSequentialGroup()
  163.                                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  164.                                                 .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  165.                                                 .addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  166.                                             .addGap(12, 12, 12)
  167.                                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.                                                 .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
  169.                                                 .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)))))
  170.                                 .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  171.                             .addGap(31, 31, 31)
  172.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  173.                                 .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  174.                                 .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))))
  175.                 .addContainerGap(31, Short.MAX_VALUE))
  176.         );
  177.         layout.setVerticalGroup(
  178.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  179.             .addGroup(layout.createSequentialGroup()
  180.                 .addContainerGap()
  181.                 .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
  182.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  183.                 .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  184.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  185.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  186.                     .addGroup(layout.createSequentialGroup()
  187.                         .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  188.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  189.                         .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  190.                     .addGroup(layout.createSequentialGroup()
  191.                         .addComponent(jButton12)
  192.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  193.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  194.                             .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  195.                             .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
  196.                             .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
  197.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  198.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  199.                             .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
  200.                             .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
  201.                             .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
  202.                         .addGap(7, 7, 7)
  203.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  204.                             .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
  205.                             .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
  206.                             .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))))
  207.                 .addContainerGap(26, Short.MAX_VALUE))
  208.         );
  209.  
  210.         pack();
  211.     }// </editor-fold>                        
  212.     int suma = 0, operation = 0;
  213.     String istoric = " ";
  214.     public void operatie(int operatie, int numar)
  215.     {
  216.         switch (operatie) {
  217.             case 1:
  218.             {
  219.                 istoric = numar + "+" + istoric;
  220.                 suma += numar;
  221.                 break;
  222.             }    
  223.             case 2:{
  224.                 istoric = numar + "-" + istoric;    
  225.                 suma -= numar;
  226.                 break;}
  227.             case 3:{
  228.                 istoric = " ";
  229.                 suma = 0;
  230.                 break;}
  231.             default:{
  232.                 suma = numar;operation = 0;
  233.                 istoric = numar + istoric;
  234.                 break;}
  235.         }
  236.         UpdateDisplay();
  237.     }
  238.     public void UpdateDisplay()
  239.     {
  240.         String suma2 = " " + suma;
  241.         jTextField1.setText(suma2);
  242.         jTextField2.setText(istoric);
  243.     }
  244.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  245.         operatie(operation, 1);
  246.     }                                        
  247.  
  248.     private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  249.         operatie(operation, 6);
  250.     }                                        
  251.  
  252.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  253.         operatie(operation, 2);
  254.     }                                        
  255.  
  256.     private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  257.         operation = 3;
  258.         operatie(operation, 1);
  259.     }                                        
  260.  
  261.     private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  262.         operation = 2;
  263.     }                                        
  264.  
  265.     private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  266.         operation = 1;
  267.     }                                        
  268.  
  269.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  270.         operatie(operation, 4);
  271.     }                                        
  272.  
  273.     private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  274.         operatie(operation, 3);
  275.     }                                        
  276.  
  277.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  278.         operatie(operation, 5);
  279.     }                                        
  280.  
  281.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  282.         operatie(operation, 6);
  283.     }                                        
  284.  
  285.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  286.         operatie(operation, 7);
  287.     }                                        
  288.  
  289.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  290.         operatie(operation, 9);
  291.     }                                        
  292.  
  293.     private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  294.         // TODO add your handling code here:
  295.     }                                          
  296.  
  297.     /**
  298.      * @param args the command line arguments
  299.      */
  300.     public static void main(String args[]) {
  301.         /* Set the Nimbus look and feel */
  302.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  303.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  304.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  305.          */
  306.         try {
  307.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  308.                 if ("Nimbus".equals(info.getName())) {
  309.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  310.                     break;
  311.                 }
  312.             }
  313.         } catch (ClassNotFoundException ex) {
  314.             java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  315.         } catch (InstantiationException ex) {
  316.             java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  317.         } catch (IllegalAccessException ex) {
  318.             java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  319.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  320.             java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  321.         }
  322.         //</editor-fold>
  323.  
  324.         /* Create and display the form */
  325.         java.awt.EventQueue.invokeLater(new Runnable() {
  326.             public void run() {
  327.                 new Calculator().setVisible(true);
  328.             }
  329.         });
  330.     }
  331.  
  332.     // Variables declaration - do not modify                    
  333.     private javax.swing.JButton jButton1;
  334.     private javax.swing.JButton jButton10;
  335.     private javax.swing.JButton jButton11;
  336.     private javax.swing.JButton jButton12;
  337.     private javax.swing.JButton jButton2;
  338.     private javax.swing.JButton jButton3;
  339.     private javax.swing.JButton jButton4;
  340.     private javax.swing.JButton jButton5;
  341.     private javax.swing.JButton jButton6;
  342.     private javax.swing.JButton jButton7;
  343.     private javax.swing.JButton jButton8;
  344.     private javax.swing.JButton jButton9;
  345.     private javax.swing.JTextField jTextField1;
  346.     private javax.swing.JTextField jTextField2;
  347.     // End of variables declaration                  
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement