Advertisement
wingman007

Java2014GraphicCalculator

Nov 30th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.57 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.  
  7. package jCalculator;
  8.  
  9. /**
  10.  *
  11.  * @author fmi
  12.  */
  13. public class JavaCalculator extends javax.swing.JFrame {
  14.  
  15.     private double total = 0;
  16.    
  17.     private char operation;
  18.    
  19.     /**
  20.      * Creates new form JavaCalculator
  21.      */
  22.     public JavaCalculator() {
  23.         initComponents();
  24.     }
  25.  
  26.     /**
  27.      * This method is called from within the constructor to initialize the form.
  28.      * WARNING: Do NOT modify this code. The content of this method is always
  29.      * regenerated by the Form Editor.
  30.      */
  31.     @SuppressWarnings("unchecked")
  32.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  33.     private void initComponents() {
  34.  
  35.         txtDisplay = new javax.swing.JTextField();
  36.         jPanel1 = new javax.swing.JPanel();
  37.         btnOne = new javax.swing.JButton();
  38.         btnTwo = new javax.swing.JButton();
  39.         btnThree = new javax.swing.JButton();
  40.         jPanel2 = new javax.swing.JPanel();
  41.         btnAdd = new javax.swing.JButton();
  42.         btnEquals = new javax.swing.JButton();
  43.         btnClear = new javax.swing.JButton();
  44.  
  45.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  46.  
  47.         btnOne.setText("1");
  48.         btnOne.addActionListener(new java.awt.event.ActionListener() {
  49.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  50.                 btnOneActionPerformed(evt);
  51.             }
  52.         });
  53.  
  54.         btnTwo.setText("2");
  55.         btnTwo.addActionListener(new java.awt.event.ActionListener() {
  56.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  57.                 btnTwoActionPerformed(evt);
  58.             }
  59.         });
  60.  
  61.         btnThree.setText("3");
  62.         btnThree.addActionListener(new java.awt.event.ActionListener() {
  63.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  64.                 btnThreeActionPerformed(evt);
  65.             }
  66.         });
  67.  
  68.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  69.         jPanel1.setLayout(jPanel1Layout);
  70.         jPanel1Layout.setHorizontalGroup(
  71.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  72.             .addGroup(jPanel1Layout.createSequentialGroup()
  73.                 .addContainerGap()
  74.                 .addComponent(btnOne)
  75.                 .addGap(18, 18, 18)
  76.                 .addComponent(btnTwo)
  77.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
  78.                 .addComponent(btnThree)
  79.                 .addContainerGap())
  80.         );
  81.         jPanel1Layout.setVerticalGroup(
  82.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83.             .addGroup(jPanel1Layout.createSequentialGroup()
  84.                 .addContainerGap()
  85.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  86.                     .addComponent(btnOne)
  87.                     .addComponent(btnTwo)
  88.                     .addComponent(btnThree))
  89.                 .addContainerGap(170, Short.MAX_VALUE))
  90.         );
  91.  
  92.         btnAdd.setText("+");
  93.         btnAdd.addActionListener(new java.awt.event.ActionListener() {
  94.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  95.                 btnAddActionPerformed(evt);
  96.             }
  97.         });
  98.  
  99.         btnEquals.setText("=");
  100.         btnEquals.addActionListener(new java.awt.event.ActionListener() {
  101.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  102.                 btnEqualsActionPerformed(evt);
  103.             }
  104.         });
  105.  
  106.         btnClear.setText("Clear");
  107.         btnClear.setToolTipText("");
  108.         btnClear.addActionListener(new java.awt.event.ActionListener() {
  109.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  110.                 btnClearActionPerformed(evt);
  111.             }
  112.         });
  113.  
  114.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  115.         jPanel2.setLayout(jPanel2Layout);
  116.         jPanel2Layout.setHorizontalGroup(
  117.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  118.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  119.                 .addContainerGap(22, Short.MAX_VALUE)
  120.                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  121.                     .addComponent(btnEquals)
  122.                     .addComponent(btnAdd))
  123.                 .addGap(31, 31, 31))
  124.             .addGroup(jPanel2Layout.createSequentialGroup()
  125.                 .addContainerGap()
  126.                 .addComponent(btnClear)
  127.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  128.         );
  129.         jPanel2Layout.setVerticalGroup(
  130.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  131.             .addGroup(jPanel2Layout.createSequentialGroup()
  132.                 .addContainerGap()
  133.                 .addComponent(btnAdd)
  134.                 .addGap(18, 18, 18)
  135.                 .addComponent(btnEquals)
  136.                 .addGap(18, 18, 18)
  137.                 .addComponent(btnClear)
  138.                 .addContainerGap(78, Short.MAX_VALUE))
  139.         );
  140.  
  141.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  142.         getContentPane().setLayout(layout);
  143.         layout.setHorizontalGroup(
  144.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  145.             .addGroup(layout.createSequentialGroup()
  146.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  147.                     .addGroup(layout.createSequentialGroup()
  148.                         .addGap(36, 36, 36)
  149.                         .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
  150.                     .addGroup(layout.createSequentialGroup()
  151.                         .addGap(25, 25, 25)
  152.                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  153.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
  154.                 .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  155.                 .addContainerGap())
  156.         );
  157.         layout.setVerticalGroup(
  158.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  159.             .addGroup(layout.createSequentialGroup()
  160.                 .addGap(28, 28, 28)
  161.                 .addComponent(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  162.                 .addGap(18, 18, 18)
  163.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  164.                     .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  165.                     .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  166.                 .addContainerGap(22, Short.MAX_VALUE))
  167.         );
  168.  
  169.         pack();
  170.     }// </editor-fold>                        
  171.  
  172.     private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {                                      
  173.         // TODO add your handling code here:
  174.         String display = txtDisplay.getText() + "1";
  175.         txtDisplay.setText(display);
  176.     }                                      
  177.  
  178.     private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {                                      
  179.         // TODO add your handling code here:
  180.         String display = txtDisplay.getText() + "2";
  181.         txtDisplay.setText(display);
  182.     }                                      
  183.  
  184.     private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {                                        
  185.         // TODO add your handling code here:
  186.         String display = txtDisplay.getText() + "3";
  187.         txtDisplay.setText(display);
  188.     }                                        
  189.  
  190.     private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                      
  191.         // TODO add your handling code here:
  192.         total += Double.parseDouble(txtDisplay.getText());
  193.         txtDisplay.setText("");
  194.     }                                      
  195.  
  196.     private void btnEqualsActionPerformed(java.awt.event.ActionEvent evt) {                                          
  197.         // TODO add your handling code here:
  198.         double current = Double.parseDouble(txtDisplay.getText());
  199.         total += current;
  200.         txtDisplay.setText(Double.toString(total));
  201.     }                                        
  202.  
  203.     private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {                                        
  204.         // TODO add your handling code here:
  205.         total = 0;
  206.         txtDisplay.setText("");
  207.     }                                        
  208.  
  209.     /**
  210.      * @param args the command line arguments
  211.      */
  212.     public static void main(String args[]) {
  213.         /* Set the Nimbus look and feel */
  214.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  215.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  216.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  217.          */
  218.         try {
  219.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  220.                 if ("Nimbus".equals(info.getName())) {
  221.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  222.                     break;
  223.                 }
  224.             }
  225.         } catch (ClassNotFoundException ex) {
  226.             java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  227.         } catch (InstantiationException ex) {
  228.             java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  229.         } catch (IllegalAccessException ex) {
  230.             java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  231.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  232.             java.util.logging.Logger.getLogger(JavaCalculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  233.         }
  234.         //</editor-fold>
  235.  
  236.         /* Create and display the form */
  237.         java.awt.EventQueue.invokeLater(new Runnable() {
  238.             public void run() {
  239.                 new JavaCalculator().setVisible(true);
  240.             }
  241.         });
  242.     }
  243.  
  244.     // Variables declaration - do not modify                    
  245.     private javax.swing.JButton btnAdd;
  246.     private javax.swing.JButton btnClear;
  247.     private javax.swing.JButton btnEquals;
  248.     private javax.swing.JButton btnOne;
  249.     private javax.swing.JButton btnThree;
  250.     private javax.swing.JButton btnTwo;
  251.     private javax.swing.JPanel jPanel1;
  252.     private javax.swing.JPanel jPanel2;
  253.     private javax.swing.JTextField txtDisplay;
  254.     // End of variables declaration                  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement