Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package GUI;
- /**
- *
- * @author
- */
- public class CalculatorGUI extends javax.swing.JFrame {
- public double firstDouble; // the first value is stored here
- public double secondDouble; // the second value is stored here
- public double answer = 0; // self-explanatory, spiteful comment
- boolean operatorIsSelected = false; // used to know if a value is firstDouble or secondDouble
- String operators = ""; // used to denote operator to be used in calculation
- String repeat = "";
- int i = 0;
- /**
- * Creates new form CalculatorGUI
- */
- public CalculatorGUI() {
- initComponents();
- result.setText(""); // ensures calculator screen is clear
- }
- /* The following code allows values to be set on the calculator screen and
- manipulated. */
- private void operation(String value){
- String text = result.getText();
- if (text.length() > 0 && operatorIsSelected == false) {
- result.setText(result.getText() + value);
- } else if (operatorIsSelected == true) {
- //for (int i = 0; i < 1;i++) {
- result.setText(null);
- operatorIsSelected = false;
- result.setText(result.getText() + value);
- //}
- } else {
- result.setText(value);
- }
- }
- -- SAVED SPACE --
- private void oneActionPerformed(java.awt.event.ActionEvent evt) {
- operation("1");
- }
- private void twoActionPerformed(java.awt.event.ActionEvent evt) {
- operation("2");
- }
- private void threeActionPerformed(java.awt.event.ActionEvent evt) {
- operation("3");
- }
- private void fourActionPerformed(java.awt.event.ActionEvent evt) {
- operation("4");
- }
- private void fiveActionPerformed(java.awt.event.ActionEvent evt) {
- operation("5");
- }
- private void sixActionPerformed(java.awt.event.ActionEvent evt) {
- operation("6");
- }
- private void sevenActionPerformed(java.awt.event.ActionEvent evt) {
- operation("7");
- }
- private void eightActionPerformed(java.awt.event.ActionEvent evt) {
- operation("8");
- }
- private void nineActionPerformed(java.awt.event.ActionEvent evt) {
- operation("9");
- }
- private void zeroActionPerformed(java.awt.event.ActionEvent evt) {
- operation("0");
- }
- private void clearAllActionPerformed(java.awt.event.ActionEvent evt) {
- result.setText("");
- firstDouble = 0;
- secondDouble = 0;
- i = 0;
- }
- private void resultActionPerformed(java.awt.event.ActionEvent evt) {
- // This is the calculator screen, do not edit as all the other buttons manipulate this.
- }
- private void divisionActionPerformed(java.awt.event.ActionEvent evt) {
- String numberwords = result.getText();
- firstDouble = Double.parseDouble(numberwords);
- result.setText("/");
- operatorIsSelected = true;
- operators = "/";
- repeat = "/";
- }
- private void multiplicationActionPerformed(java.awt.event.ActionEvent evt) {
- String numberwords = result.getText();
- firstDouble = Double.parseDouble(numberwords);
- result.setText("X");
- operatorIsSelected = true;
- operators = "*";
- repeat = "*";
- }
- private void additionActionPerformed(java.awt.event.ActionEvent evt) {
- String numberwords = result.getText();
- firstDouble = Double.parseDouble(numberwords);
- result.setText("+");
- operatorIsSelected = true;
- operators = "+";
- repeat = "+";
- }
- private void subtractionActionPerformed(java.awt.event.ActionEvent evt) {
- String numberwords = result.getText();
- firstDouble = Double.parseDouble(numberwords);
- result.setText("-");
- operatorIsSelected = true;
- operators = "-";
- repeat = "-";
- }
- private void equalsActionPerformed(java.awt.event.ActionEvent evt) {
- 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;
- default:
- if(i == 0){
- double savedValue = secondDouble;
- switch(repeat){
- case "/":
- answer = answer / savedValue;
- operators = "";
- break;
- case "*":
- answer = answer * savedValue;
- operators = "";
- break;
- case "+":
- answer = answer + savedValue;
- operators = "";
- break;
- case "-":
- answer = answer - savedValue;
- 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 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(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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement