/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GUI; /** * * @author REDACTED */ public class CalculatorGUI extends javax.swing.JFrame { public double firstDouble; public double secondDouble; public double answer = 0; public double yolo = 0; boolean operatorIsSelected = false; double olo = 0; boolean press = false; String operators = ""; public static void equals(String[] args) { } /** * Creates new form CalculatorGUI */ public CalculatorGUI() { initComponents(); result.setText(""); } public int numberLength() { String numberL = result.getText(); return numberL.length(); } /** * 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") <!-- code redacted --> private void oneActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "1"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "1"); } } else { result.setText("1"); } } private void twoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "2"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "2"); } } else { result.setText("2"); } } private void threeActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "3"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "3"); } } else { result.setText("3"); } } private void fourActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "4"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "4"); } } else { result.setText("4"); } } private void fiveActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "5"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "5"); } } else { result.setText("5"); } } private void sixActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "6"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "6"); } } else { result.setText("6"); } } private void sevenActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "7"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "7"); } } else { result.setText("7"); } } private void eightActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "8"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "8"); } } else { result.setText("8"); } } private void nineActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "9"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "9"); } } else { result.setText("9"); } } private void zeroActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: numberLength(); if (numberLength() > 0 && operatorIsSelected == false) { result.setText(result.getText() + "0"); } else if (operatorIsSelected == true) { for (int i = 0; i < 1;) { result.setText(null); i++; operatorIsSelected = false; result.setText(result.getText() + "0"); } } else { result.setText("0"); } } private void clearAllActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: result.setText(""); firstDouble = 0; secondDouble = 0; } private void resultActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void divisionActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String numberwords = result.getText(); firstDouble = Double.parseDouble(numberwords); result.setText("/"); operatorIsSelected = true; operators = "/"; } private void multiplicationActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String numberwords = result.getText(); firstDouble = Double.parseDouble(numberwords); result.setText("X"); operatorIsSelected = true; operators = "*"; } private void additionActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String numberwords = result.getText(); firstDouble = Double.parseDouble(numberwords); result.setText("+"); operatorIsSelected = true; operators = "+"; } private void subtractionActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String numberwords = result.getText(); firstDouble = Double.parseDouble(numberwords); result.setText("-"); operatorIsSelected = true; operators = "-"; } private void equalsActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: secondDouble = Double.parseDouble(result.getText()); switch (operators) { case "/": answer = firstDouble / secondDouble; operators = ""; break; case "*": answer = firstDouble * secondDouble; operators = ""; break; case "+": answer = firstDouble + secondDouble; operators = ""; break; case "-": answer = firstDouble - secondDouble; operators = ""; break; } result.setText("" + answer + ""); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] */ 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(CalculatorGUI.class .getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CalculatorGUI.class .getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CalculatorGUI.class .getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CalculatorGUI.class .getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new CalculatorGUI().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton addition; private javax.swing.JButton clear; private javax.swing.JButton clearAll; private javax.swing.JButton decimal; private javax.swing.JButton division; private javax.swing.JButton eight; private javax.swing.JButton equals; private javax.swing.JButton five; private javax.swing.JButton four; private javax.swing.JButton multiplication; private javax.swing.JButton nine; private javax.swing.JButton one; private javax.swing.JFormattedTextField result; private javax.swing.JButton seven; private javax.swing.JButton six; private javax.swing.JButton subtraction; private javax.swing.JButton three; private javax.swing.JButton two; private javax.swing.JButton zero; // End of variables declaration }
Obviously there is quite a lot of spaghetti code, am I'm not quite knowledgeable enough with Java to know any alternatives to the problem...