/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */ package Java_Calc; /** * This class represents a simple calculator GUI. */ public class Java_Cal extends javax.swing.JFrame { double firstnum; // Stores the first operand double secondnum; // Stores the second operand double result; // Stores the result of the operation String operations; // Stores the current operation String answer; // Stores the final answer /** * Creates new form Java_Cal */ public Java_Cal() { initComponents(); // Initializes the GUI components } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { // GUI components initialization code // ... } // ActionPerformed method for backspace button private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String backspace=null; if(jtxtDisplay.getText().length() > 0){ StringBuilder strB =new StringBuilder(jtxtDisplay.getText()); strB.deleteCharAt(jtxtDisplay.getText().length() - 1); backspace = strB.toString(); jtxtDisplay.setText(backspace); } } // ActionPerformed method for clear button private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { jtxtDisplay.setText(""); } // ActionPerformed methods for number buttons private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { String Enternumber = jtxtDisplay.getText() + jButton5.getText(); jtxtDisplay.setText(Enternumber); } // ActionPerformed methods for other number buttons... // ... // ActionPerformed method for percentage button private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { firstnum = Double.parseDouble(jtxtDisplay.getText()); jtxtDisplay.setText(""); operations = "%"; } // ActionPerformed method for division button private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { firstnum = Double.parseDouble(jtxtDisplay.getText()); jtxtDisplay.setText(""); operations = "/"; } // ActionPerformed method for multiplication button private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) { firstnum = Double.parseDouble(jtxtDisplay.getText()); jtxtDisplay.setText(""); operations = "*"; } // ActionPerformed method for subtraction button private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) { firstnum = Double.parseDouble(jtxtDisplay.getText()); jtxtDisplay.setText(""); operations = "-"; } // ActionPerformed method for addition button private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) { firstnum = Double.parseDouble(jtxtDisplay.getText()); jtxtDisplay.setText("+"); operations = "+"; } // ActionPerformed method for positive/negative toggle button private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) { double ops = Double.parseDouble(String.valueOf(jtxtDisplay.getText())); ops =ops * (-1); jtxtDisplay.setText(String.valueOf(ops)); } // ActionPerformed method for equals button private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) { String answer; secondnum = Double.parseDouble(jtxtDisplay.getText()); if (operations == "+") { result = firstnum + secondnum; answer = String.format("%.0f",result); jtxtDisplay.setText(answer); } else if (operations == "-") { result = firstnum - secondnum; answer = String.format("%.0f",result); jtxtDisplay.setText(answer); } else if (operations == "*") { result = firstnum * secondnum; answer = String.format("%.0f",result); jtxtDisplay.setText(answer) ; } else if (operations == "/") { result = firstnum / secondnum; answer = String.format("%.0f",result); jtxtDisplay.setText(answer); } else if (operations == "%") { result = firstnum % secondnum; answer = String.format("%.0f",result); jtxtDisplay.setText(answer); } } // ActionPerformed method for decimal point button private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) { String Enternumber = jtxtDisplay.getText() + jButton18.getText(); jtxtDisplay.setText(Enternumber); } // ActionPerformed method for zero button private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) { String Enternumber = jtxtDisplay.getText() + jButton17.getText(); jtxtDisplay.setText(Enternumber); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Java_Cal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Java_Cal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Java_Cal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Java_Cal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Java_Cal().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; // Variables for other buttons... // ... private javax.swing.JTextField jtxtDisplay; // End of variables declaration }