Advertisement
mdporan

Calculator

Feb 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 41.00 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 guicalculator;
  7.  
  8. import java.awt.AWTException;
  9.  
  10. public class CalculatorGui extends javax.swing.JFrame {
  11.  
  12.     double num, num1, ans;
  13.     String sign;
  14.     int cal;
  15.  
  16.     /**
  17.      * Creates new form CalculatorGui
  18.      */
  19.     public CalculatorGui() {
  20.         super("CalCulator");
  21.         initComponents();
  22.         rdbt1.setEnabled(false);
  23.     }
  24.  
  25.     public void arithmetic_operation() {
  26.         switch (cal) {
  27.             case 1:
  28.                 num1 = Double.parseDouble(text.getText());
  29.                 ans = num + num1;
  30.                 l.setText(l.getText()+num1);
  31.                 text.setText(Double.toString(ans));
  32.                 break;
  33.             case 2:
  34.                 num1 = Double.parseDouble(text.getText());
  35.                 ans = num - num1;
  36.                 l.setText(l.getText()+num1);
  37.                 text.setText(Double.toString(ans));
  38.                 break;
  39.             case 3:
  40.                 num1 = Double.parseDouble(text.getText());
  41.                 ans = num * num1;
  42.                 l.setText(l.getText()+num1);
  43.                 text.setText(Double.toString(ans));
  44.                 break;
  45.             case 4:
  46.                 try {
  47.                     num1 = Double.parseDouble(text.getText());
  48.                     ans = num / num1;
  49.                     l.setText(l.getText()+num1);
  50.                     text.setText(Double.toString(ans));
  51.                     break;
  52.                 } catch (ArithmeticException e) {
  53.                     text.setText("Math Error");
  54.                 }
  55.             case 5:
  56.                 try{
  57.                 ans = fact(num);
  58.                
  59.                 text.setText(Double.toString(ans));
  60.                 break;
  61.                 }catch(ArithmeticException e){
  62.                     text.setText("Invalid");
  63.                 }
  64.                
  65.             case 6:
  66.                 num = Double.parseDouble(text.getText());
  67.                 l.setText(l.getText() + num + ")");
  68.                 ans = Math.sin(num);
  69.                 text.setText(Double.toString(ans));
  70.                 break;
  71.             case 7:
  72.                 num = Double.parseDouble(text.getText());
  73.                 l.setText(l.getText() + num + ")");
  74.                 ans = Math.cos(num);
  75.                 text.setText(Double.toString(ans));
  76.                 break;
  77.             case 8:
  78.                 num = Double.parseDouble(text.getText());
  79.                 l.setText(l.getText() + num + ")");
  80.                 ans = Math.tan(num);
  81.                 text.setText(Double.toString(ans));
  82.                 break;
  83.             case 9:
  84.                 try{
  85.              
  86.                 num = Double.parseDouble(text.getText());
  87.                 l.setText(l.getText() + num + ")");
  88.                 ans = 1.0/Math.tan(num);
  89.                 text.setText(Double.toString(ans));
  90.                 break;
  91.                 }catch(ArithmeticException e){
  92.                     text.setText("Invalid");
  93.                 }
  94.             case 10:
  95.                 try{
  96.                 num1 = Double.parseDouble(text.getText());
  97.                 l.setText(l.getText() + num);
  98.                 ans = Math.pow(num,num1);
  99.                 text.setText(Double.toString(ans));
  100.                 break;
  101.                 }catch(NumberFormatException e){
  102.                     text.setText("Invalid");
  103.                 }
  104.  
  105.         }
  106.  
  107.     }
  108.  
  109.     static double fact(double n) {
  110.         if (n == 1.0) {
  111.             return 1;
  112.  
  113.         } else {
  114.             return (n * fact(n - 1.0));
  115.         }
  116.  
  117.     }
  118.  
  119.     @Override
  120.     public void enable() {
  121.         text.setEnabled(true);
  122.         rdbt1.setEnabled(false);
  123.         rdbt2.setEnabled(true);
  124.         bt1.setEnabled(true);
  125.         bt20.setEnabled(true);
  126.         bt20.setEnabled(true);
  127.         bt25.setEnabled(true);
  128.         bt28.setEnabled(true);
  129.         bt2.setEnabled(true);
  130.         bt30.setEnabled(true);
  131.         bt32.setEnabled(true);
  132.         bt3.setEnabled(true);
  133.         bt4.setEnabled(true);
  134.         bt5.setEnabled(true);
  135.         bt6.setEnabled(true);
  136.         bt19.setEnabled(true);
  137.         bt23.setEnabled(true);
  138.         bt26.setEnabled(true);
  139.         bt29.setEnabled(true);
  140.         bt31.setEnabled(true);
  141.         bt33.setEnabled(true);
  142.         bt22.setEnabled(true);
  143.         l.setEnabled(true);
  144.         btb.setEnabled(true);
  145.         bts.setEnabled(true);
  146.         btc.setEnabled(true);
  147.         btc1.setEnabled(true);
  148.         btsq.setEnabled(true);
  149.         btr.setEnabled(true);
  150.         btc.setEnabled(true);
  151.         btt.setEnabled(true);
  152.         btp.setEnabled(true);
  153.  
  154.     }
  155.  
  156.     @Override
  157.     public void disable() {
  158.         text.setEnabled(false);
  159.         rdbt1.setEnabled(true);
  160.         rdbt2.setEnabled(false);
  161.         bt1.setEnabled(false);
  162.         bt20.setEnabled(false);
  163.         bt20.setEnabled(false);
  164.         bt25.setEnabled(false);
  165.         bt28.setEnabled(false);
  166.         bt2.setEnabled(false);
  167.         bt30.setEnabled(false);
  168.         bt32.setEnabled(false);
  169.         bt3.setEnabled(false);
  170.         bt4.setEnabled(false);
  171.         bt5.setEnabled(false);
  172.         bt6.setEnabled(false);
  173.         bt19.setEnabled(false);
  174.         bt23.setEnabled(false);
  175.         bt26.setEnabled(false);
  176.         bt29.setEnabled(false);
  177.         bt31.setEnabled(false);
  178.         bt33.setEnabled(false);
  179.         bt22.setEnabled(false);
  180.         l.setEnabled(false);
  181.         btb.setEnabled(false);
  182.         bts.setEnabled(false);
  183.         btc.setEnabled(false);
  184.         btc1.setEnabled(false);
  185.         btsq.setEnabled(false);
  186.         btr.setEnabled(false);
  187.         btc.setEnabled(false);
  188.         btt.setEnabled(false);
  189.         btp.setEnabled(false);
  190.  
  191.     }
  192.  
  193.     /**
  194.      * This method is called from within the constructor to initialize the form.
  195.      * WARNING: Do NOT modify this code. The content of this method is always
  196.      * regenerated by the Form Editor.
  197.      */
  198.     @SuppressWarnings("unchecked")
  199.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  200.     private void initComponents() {
  201.  
  202.         buttonGroup1 = new javax.swing.ButtonGroup();
  203.         jButton3 = new javax.swing.JButton();
  204.         jPanel1 = new javax.swing.JPanel();
  205.         text = new javax.swing.JTextField();
  206.         bt1 = new javax.swing.JButton();
  207.         rdbt1 = new javax.swing.JRadioButton();
  208.         rdbt2 = new javax.swing.JRadioButton();
  209.         bt19 = new javax.swing.JButton();
  210.         bt20 = new javax.swing.JButton();
  211.         bt22 = new javax.swing.JButton();
  212.         bt23 = new javax.swing.JButton();
  213.         bt2 = new javax.swing.JButton();
  214.         bt25 = new javax.swing.JButton();
  215.         bt26 = new javax.swing.JButton();
  216.         bt3 = new javax.swing.JButton();
  217.         bt28 = new javax.swing.JButton();
  218.         bt29 = new javax.swing.JButton();
  219.         bt4 = new javax.swing.JButton();
  220.         bt30 = new javax.swing.JButton();
  221.         bt31 = new javax.swing.JButton();
  222.         bt5 = new javax.swing.JButton();
  223.         bt32 = new javax.swing.JButton();
  224.         bt33 = new javax.swing.JButton();
  225.         bt6 = new javax.swing.JButton();
  226.         l = new javax.swing.JLabel();
  227.         btb = new javax.swing.JButton();
  228.         bts = new javax.swing.JButton();
  229.         btc = new javax.swing.JButton();
  230.         btt = new javax.swing.JButton();
  231.         btc1 = new javax.swing.JButton();
  232.         btsq = new javax.swing.JButton();
  233.         btp = new javax.swing.JButton();
  234.         btr = new javax.swing.JButton();
  235.  
  236.         jButton3.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  237.         jButton3.setText("sin");
  238.  
  239.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  240.  
  241.         jPanel1.setBackground(new java.awt.Color(153, 255, 255));
  242.         jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
  243.         jPanel1.setName(""); // NOI18N
  244.  
  245.         text.setEditable(false);
  246.         text.setBackground(new java.awt.Color(15, 88, 108));
  247.         text.setForeground(new java.awt.Color(102, 255, 204));
  248.         text.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
  249.         text.addActionListener(new java.awt.event.ActionListener() {
  250.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  251.                 textActionPerformed(evt);
  252.             }
  253.         });
  254.  
  255.         bt1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  256.         bt1.setText("9");
  257.         bt1.addActionListener(new java.awt.event.ActionListener() {
  258.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  259.                 bt1ActionPerformed(evt);
  260.             }
  261.         });
  262.  
  263.         buttonGroup1.add(rdbt1);
  264.         rdbt1.setText("ON");
  265.         rdbt1.addActionListener(new java.awt.event.ActionListener() {
  266.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  267.                 rdbt1ActionPerformed(evt);
  268.             }
  269.         });
  270.  
  271.         buttonGroup1.add(rdbt2);
  272.         rdbt2.setText("OFF");
  273.         rdbt2.addActionListener(new java.awt.event.ActionListener() {
  274.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  275.                 rdbt2ActionPerformed(evt);
  276.             }
  277.         });
  278.  
  279.         bt19.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  280.         bt19.setText("x!");
  281.         bt19.addActionListener(new java.awt.event.ActionListener() {
  282.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  283.                 bt19ActionPerformed(evt);
  284.             }
  285.         });
  286.  
  287.         bt20.setBackground(new java.awt.Color(153, 0, 0));
  288.         bt20.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  289.         bt20.setForeground(new java.awt.Color(255, 255, 255));
  290.         bt20.setText("AC");
  291.         bt20.addActionListener(new java.awt.event.ActionListener() {
  292.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  293.                 bt20ActionPerformed(evt);
  294.             }
  295.         });
  296.  
  297.         bt22.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  298.         bt22.setText("8");
  299.         bt22.addActionListener(new java.awt.event.ActionListener() {
  300.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  301.                 bt22ActionPerformed(evt);
  302.             }
  303.         });
  304.  
  305.         bt23.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  306.         bt23.setText("/");
  307.         bt23.addActionListener(new java.awt.event.ActionListener() {
  308.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  309.                 bt23ActionPerformed(evt);
  310.             }
  311.         });
  312.  
  313.         bt2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  314.         bt2.setText("7");
  315.         bt2.addActionListener(new java.awt.event.ActionListener() {
  316.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  317.                 bt2ActionPerformed(evt);
  318.             }
  319.         });
  320.  
  321.         bt25.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  322.         bt25.setText("6");
  323.         bt25.addActionListener(new java.awt.event.ActionListener() {
  324.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  325.                 bt25ActionPerformed(evt);
  326.             }
  327.         });
  328.  
  329.         bt26.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  330.         bt26.setText("X");
  331.         bt26.addActionListener(new java.awt.event.ActionListener() {
  332.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  333.                 bt26ActionPerformed(evt);
  334.             }
  335.         });
  336.  
  337.         bt3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  338.         bt3.setText("5");
  339.         bt3.addActionListener(new java.awt.event.ActionListener() {
  340.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  341.                 bt3ActionPerformed(evt);
  342.             }
  343.         });
  344.  
  345.         bt28.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  346.         bt28.setText("4");
  347.         bt28.addActionListener(new java.awt.event.ActionListener() {
  348.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  349.                 bt28ActionPerformed(evt);
  350.             }
  351.         });
  352.  
  353.         bt29.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  354.         bt29.setText("-");
  355.         bt29.addActionListener(new java.awt.event.ActionListener() {
  356.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  357.                 bt29ActionPerformed(evt);
  358.             }
  359.         });
  360.  
  361.         bt4.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  362.         bt4.setText("3");
  363.         bt4.addActionListener(new java.awt.event.ActionListener() {
  364.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  365.                 bt4ActionPerformed(evt);
  366.             }
  367.         });
  368.  
  369.         bt30.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  370.         bt30.setText("2");
  371.         bt30.addActionListener(new java.awt.event.ActionListener() {
  372.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  373.                 bt30ActionPerformed(evt);
  374.             }
  375.         });
  376.  
  377.         bt31.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  378.         bt31.setText("+");
  379.         bt31.addActionListener(new java.awt.event.ActionListener() {
  380.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  381.                 bt31ActionPerformed(evt);
  382.             }
  383.         });
  384.  
  385.         bt5.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  386.         bt5.setText("1");
  387.         bt5.addActionListener(new java.awt.event.ActionListener() {
  388.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  389.                 bt5ActionPerformed(evt);
  390.             }
  391.         });
  392.  
  393.         bt32.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  394.         bt32.setText(".");
  395.         bt32.addActionListener(new java.awt.event.ActionListener() {
  396.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  397.                 bt32ActionPerformed(evt);
  398.             }
  399.         });
  400.  
  401.         bt33.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  402.         bt33.setText("=");
  403.         bt33.addActionListener(new java.awt.event.ActionListener() {
  404.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  405.                 bt33ActionPerformed(evt);
  406.             }
  407.         });
  408.  
  409.         bt6.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
  410.         bt6.setText("0");
  411.         bt6.addActionListener(new java.awt.event.ActionListener() {
  412.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  413.                 bt6ActionPerformed(evt);
  414.             }
  415.         });
  416.  
  417.         l.setForeground(new java.awt.Color(255, 51, 51));
  418.  
  419.         btb.setBackground(new java.awt.Color(153, 0, 0));
  420.         btb.setForeground(new java.awt.Color(255, 255, 255));
  421.         btb.setText("DEL");
  422.         btb.addActionListener(new java.awt.event.ActionListener() {
  423.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  424.                 btbActionPerformed(evt);
  425.             }
  426.         });
  427.  
  428.         bts.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  429.         bts.setText("sin");
  430.         bts.addActionListener(new java.awt.event.ActionListener() {
  431.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  432.                 btsActionPerformed(evt);
  433.             }
  434.         });
  435.  
  436.         btc.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  437.         btc.setText("cos");
  438.         btc.addActionListener(new java.awt.event.ActionListener() {
  439.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  440.                 btcActionPerformed(evt);
  441.             }
  442.         });
  443.  
  444.         btt.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  445.         btt.setText("tan");
  446.         btt.addActionListener(new java.awt.event.ActionListener() {
  447.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  448.                 bttActionPerformed(evt);
  449.             }
  450.         });
  451.  
  452.         btc1.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  453.         btc1.setText("cot");
  454.         btc1.addActionListener(new java.awt.event.ActionListener() {
  455.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  456.                 btc1ActionPerformed(evt);
  457.             }
  458.         });
  459.  
  460.         btsq.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
  461.         btsq.setText("x2");
  462.         btsq.addActionListener(new java.awt.event.ActionListener() {
  463.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  464.                 btsqActionPerformed(evt);
  465.             }
  466.         });
  467.  
  468.         btp.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  469.         btp.setText("pow");
  470.         btp.addActionListener(new java.awt.event.ActionListener() {
  471.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  472.                 btpActionPerformed(evt);
  473.             }
  474.         });
  475.  
  476.         btr.setFont(new java.awt.Font("Dialog", 0, 12)); // NOI18N
  477.         btr.setText(" √");
  478.         btr.addActionListener(new java.awt.event.ActionListener() {
  479.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  480.                 btrActionPerformed(evt);
  481.             }
  482.         });
  483.  
  484.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  485.         jPanel1.setLayout(jPanel1Layout);
  486.         jPanel1Layout.setHorizontalGroup(
  487.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  488.             .addGroup(jPanel1Layout.createSequentialGroup()
  489.                 .addContainerGap()
  490.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  491.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  492.                         .addComponent(text)
  493.                         .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
  494.                         .addComponent(rdbt1)
  495.                         .addGroup(jPanel1Layout.createSequentialGroup()
  496.                             .addComponent(bts)
  497.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  498.                             .addComponent(btc)
  499.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  500.                             .addComponent(btt)
  501.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  502.                             .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  503.                                 .addComponent(rdbt2)
  504.                                 .addComponent(btc1))))
  505.                     .addGroup(jPanel1Layout.createSequentialGroup()
  506.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  507.                             .addGroup(jPanel1Layout.createSequentialGroup()
  508.                                 .addComponent(bt19)
  509.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  510.                                 .addComponent(btsq)
  511.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  512.                                 .addComponent(btp)
  513.                                 .addGap(4, 4, 4)
  514.                                 .addComponent(btr, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
  515.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  516.                                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  517.                                     .addGroup(jPanel1Layout.createSequentialGroup()
  518.                                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  519.                                             .addGroup(jPanel1Layout.createSequentialGroup()
  520.                                                 .addGap(73, 73, 73)
  521.                                                 .addComponent(btb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  522.                                             .addGroup(jPanel1Layout.createSequentialGroup()
  523.                                                 .addComponent(bt1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  524.                                                 .addGap(0, 0, Short.MAX_VALUE))
  525.                                             .addGroup(jPanel1Layout.createSequentialGroup()
  526.                                                 .addComponent(bt2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  527.                                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  528.                                                 .addComponent(bt22, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
  529.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
  530.                                     .addGroup(jPanel1Layout.createSequentialGroup()
  531.                                         .addComponent(bt3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  532.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  533.                                         .addComponent(bt25, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  534.                                         .addGap(11, 11, 11))
  535.                                     .addGroup(jPanel1Layout.createSequentialGroup()
  536.                                         .addComponent(bt4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  537.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  538.                                         .addComponent(bt28, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  539.                                         .addGap(11, 11, 11))
  540.                                     .addGroup(jPanel1Layout.createSequentialGroup()
  541.                                         .addComponent(bt5, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  542.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  543.                                         .addComponent(bt30, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  544.                                         .addGap(11, 11, 11))
  545.                                     .addGroup(jPanel1Layout.createSequentialGroup()
  546.                                         .addComponent(bt6, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
  547.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  548.                                         .addComponent(bt32, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
  549.                                         .addGap(11, 11, 11)))
  550.                                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  551.                                     .addComponent(bt33, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  552.                                     .addComponent(bt31, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  553.                                     .addComponent(bt29, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  554.                                     .addComponent(bt26, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  555.                                     .addComponent(bt20, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  556.                                     .addComponent(bt23, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))
  557.                         .addGap(1, 1, 1)))
  558.                 .addGap(148, 148, 148))
  559.         );
  560.         jPanel1Layout.setVerticalGroup(
  561.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  562.             .addGroup(jPanel1Layout.createSequentialGroup()
  563.                 .addContainerGap()
  564.                 .addComponent(l, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
  565.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  566.                 .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
  567.                 .addGap(10, 10, 10)
  568.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  569.                     .addComponent(rdbt1)
  570.                     .addComponent(rdbt2))
  571.                 .addGap(18, 18, 18)
  572.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  573.                     .addComponent(bts)
  574.                     .addComponent(btc)
  575.                     .addComponent(btt)
  576.                     .addComponent(btc1))
  577.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  578.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  579.                     .addComponent(bt19)
  580.                     .addComponent(btsq)
  581.                     .addComponent(btr)
  582.                     .addComponent(btp))
  583.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  584.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  585.                     .addComponent(bt1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  586.                     .addComponent(bt20, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  587.                     .addComponent(btb, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
  588.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  589.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  590.                     .addComponent(bt2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  591.                     .addComponent(bt22, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  592.                     .addComponent(bt23, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
  593.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  594.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  595.                     .addComponent(bt3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  596.                     .addComponent(bt25, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  597.                     .addComponent(bt26, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
  598.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  599.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  600.                     .addComponent(bt4, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  601.                     .addComponent(bt28, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  602.                     .addComponent(bt29, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
  603.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  604.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  605.                     .addComponent(bt5, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  606.                     .addComponent(bt30, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  607.                     .addComponent(bt31, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
  608.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  609.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  610.                     .addComponent(bt6, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  611.                     .addComponent(bt32, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
  612.                     .addComponent(bt33, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
  613.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  614.         );
  615.  
  616.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  617.         getContentPane().setLayout(layout);
  618.         layout.setHorizontalGroup(
  619.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  620.             .addGroup(layout.createSequentialGroup()
  621.                 .addContainerGap()
  622.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
  623.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  624.         );
  625.         layout.setVerticalGroup(
  626.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  627.             .addGroup(layout.createSequentialGroup()
  628.                 .addContainerGap()
  629.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  630.                 .addContainerGap())
  631.         );
  632.  
  633.         pack();
  634.     }// </editor-fold>                        
  635.  
  636.     private void textActionPerformed(java.awt.event.ActionEvent evt) {                                    
  637.         text.setEditable(false);
  638.     }                                    
  639.  
  640.     private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  641.         text.setText(text.getText() + "9");
  642.     }                                  
  643.  
  644.     private void bt2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  645.         text.setText(text.getText() + "7");
  646.     }                                  
  647.  
  648.     private void bt22ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  649.         text.setText(text.getText() + "8");
  650.     }                                    
  651.  
  652.     private void bt3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  653.         text.setText(text.getText() + "5");
  654.     }                                  
  655.  
  656.     private void bt25ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  657.         text.setText(text.getText() + "6");
  658.     }                                    
  659.  
  660.     private void bt4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  661.         text.setText(text.getText() + "3");
  662.     }                                  
  663.  
  664.     private void bt28ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  665.         text.setText(text.getText() + "4");
  666.     }                                    
  667.  
  668.     private void bt5ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  669.         text.setText(text.getText() + "1");
  670.     }                                  
  671.  
  672.     private void bt30ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  673.         text.setText(text.getText() + "2");
  674.     }                                    
  675.  
  676.     private void bt6ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  677.         text.setText(text.getText() + "0");
  678.     }                                  
  679.  
  680.     private void bt20ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  681.         text.setText("");
  682.         l.setText("");
  683.     }                                    
  684.  
  685.     private void bt32ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  686.         text.setText(text.getText() + ".");
  687.     }                                    
  688.  
  689.     private void rdbt1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
  690.         enable();
  691.     }                                    
  692.  
  693.     private void rdbt2ActionPerformed(java.awt.event.ActionEvent evt) {                                      
  694.         disable();
  695.     }                                    
  696.  
  697.     private void bt31ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  698.         try {
  699.             num = Double.parseDouble(text.getText());
  700.             cal = 1;
  701.             sign = "+";
  702.             text.setText("");
  703.             l.setText(num + "+");
  704.         } catch (NumberFormatException e) {
  705.             text.setText("Invalid");
  706.         }
  707.     }                                    
  708.  
  709.     private void bt33ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  710.         try {
  711.             arithmetic_operation();
  712.  
  713.         } catch (ArithmeticException e ) {
  714.             text.setText("Invalid");
  715.         }
  716.  
  717.  
  718.     }                                    
  719.  
  720.     private void bt29ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  721.         try {
  722.             num = Double.parseDouble(text.getText());
  723.             cal = 2;
  724.             text.setText("");
  725.             sign = "-";
  726.             l.setText(num + "-");
  727.         } catch (NumberFormatException e) {
  728.             text.setText("Invalid");
  729.         }
  730.     }                                    
  731.  
  732.     private void bt26ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  733.         try {
  734.             num = Double.parseDouble(text.getText());
  735.             cal = 3;
  736.             text.setText("");
  737.             sign = "X";
  738.             l.setText(num + "X");
  739.         } catch (NumberFormatException e) {
  740.             text.setText("Invalid");
  741.         }
  742.     }                                    
  743.  
  744.     private void bt23ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  745.         try {
  746.             num = Double.parseDouble(text.getText());
  747.  
  748.             cal = 4;
  749.             text.setText("");
  750.             sign = "/";
  751.             l.setText(num + "/");
  752.         } catch (NumberFormatException e) {
  753.             text.setText("Invalid");
  754.  
  755.  
  756.     }                                    
  757.     }
  758.     private void bt19ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  759.         try {
  760.             cal = 5;
  761.             num = Double.parseDouble(text.getText());
  762.             sign = "!";
  763.  
  764.             l.setText(num + "!");
  765.         } catch (NumberFormatException e) {
  766.             text.setText("Invalid");
  767.         }
  768.     }                                    
  769.  
  770.     private void btbActionPerformed(java.awt.event.ActionEvent evt) {                                    
  771.         int ln = text.getText().length();
  772.         int n = ln - 1;
  773.         if (ln > 0) {
  774.             StringBuilder back = new StringBuilder(text.getText());
  775.             back.deleteCharAt(n);
  776.             String store = back.toString();
  777.             text.setText(store);
  778.         }
  779.     }                                  
  780.  
  781.     private void btsActionPerformed(java.awt.event.ActionEvent evt) {                                    
  782.         l.setText("sin" + "(");
  783.         cal = 6;
  784.  
  785.  
  786.     }                                  
  787.  
  788.     private void btcActionPerformed(java.awt.event.ActionEvent evt) {                                    
  789.         l.setText("cos" + "(");
  790.         cal = 7;
  791.     }                                  
  792.  
  793.     private void bttActionPerformed(java.awt.event.ActionEvent evt) {                                    
  794.         l.setText("tan" + "(");
  795.         cal = 8;
  796.     }                                  
  797.  
  798.     private void btc1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
  799.         l.setText("cot" + "(");
  800.         cal = 9;
  801.     }                                    
  802.  
  803.     private void btsqActionPerformed(java.awt.event.ActionEvent evt) {                                    
  804.        try{
  805.            num=Double.parseDouble(text.getText());
  806.        
  807.        ans=num*num;
  808.        l.setText(num+" * "+num);
  809.        text.setText(Double.toString(ans));
  810.        }catch(NumberFormatException e){
  811.             text.setText("Invalid");
  812.            
  813.        }
  814.     }                                    
  815.  
  816.     private void btpActionPerformed(java.awt.event.ActionEvent evt) {                                    
  817.       try{
  818.           cal=10;
  819.      
  820.       num=Double.parseDouble(text.getText());
  821.       l.setText(num+" pow ");
  822.       text.setText("");
  823.       }catch(NumberFormatException e){
  824.            text.setText("Invalid");
  825.       }
  826.     }                                  
  827.  
  828.     private void btrActionPerformed(java.awt.event.ActionEvent evt) {                                    
  829.        try{
  830.            num=Double.parseDouble(text.getText());
  831.        
  832.        l.setText("√"+num);
  833.        ans=Math.sqrt(num);
  834.        text.setText(Double.toString(ans));
  835.        }catch(NumberFormatException e){
  836.            text.setText("Invalid");
  837.            
  838.        }
  839.     }                                  
  840.  
  841.     /**
  842.      * @param args the command line arguments
  843.      */
  844.     public static void main(String args[]) {
  845.         /* Set the Nimbus look and feel */
  846.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  847.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  848.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  849.          */
  850.         try {
  851.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  852.                 if ("Nimbus".equals(info.getName())) {
  853.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  854.                     break;
  855.                 }
  856.             }
  857.         } catch (ClassNotFoundException ex) {
  858.             java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  859.         } catch (InstantiationException ex) {
  860.             java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  861.         } catch (IllegalAccessException ex) {
  862.             java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  863.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  864.             java.util.logging.Logger.getLogger(CalculatorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  865.         }
  866.         //</editor-fold>
  867.  
  868.         /* Create and display the form */
  869.         java.awt.EventQueue.invokeLater(new Runnable() {
  870.             @Override
  871.             public void run() {
  872.                 new CalculatorGui().setVisible(true);
  873.             }
  874.         });
  875.     }
  876.  
  877.     // Variables declaration - do not modify                    
  878.     private javax.swing.JButton bt1;
  879.     private javax.swing.JButton bt19;
  880.     private javax.swing.JButton bt2;
  881.     private javax.swing.JButton bt20;
  882.     private javax.swing.JButton bt22;
  883.     private javax.swing.JButton bt23;
  884.     private javax.swing.JButton bt25;
  885.     private javax.swing.JButton bt26;
  886.     private javax.swing.JButton bt28;
  887.     private javax.swing.JButton bt29;
  888.     private javax.swing.JButton bt3;
  889.     private javax.swing.JButton bt30;
  890.     private javax.swing.JButton bt31;
  891.     private javax.swing.JButton bt32;
  892.     private javax.swing.JButton bt33;
  893.     private javax.swing.JButton bt4;
  894.     private javax.swing.JButton bt5;
  895.     private javax.swing.JButton bt6;
  896.     private javax.swing.JButton btb;
  897.     private javax.swing.JButton btc;
  898.     private javax.swing.JButton btc1;
  899.     private javax.swing.JButton btp;
  900.     private javax.swing.JButton btr;
  901.     private javax.swing.JButton bts;
  902.     private javax.swing.JButton btsq;
  903.     private javax.swing.JButton btt;
  904.     private javax.swing.ButtonGroup buttonGroup1;
  905.     private javax.swing.JButton jButton3;
  906.     private javax.swing.JPanel jPanel1;
  907.     private javax.swing.JLabel l;
  908.     private javax.swing.JRadioButton rdbt1;
  909.     private javax.swing.JRadioButton rdbt2;
  910.     private javax.swing.JTextField text;
  911.     // End of variables declaration                  
  912. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement