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 Caspar Cedro
- */
- 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);
- }
- }
- /**
- * 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")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- division = new javax.swing.JButton();
- one = new javax.swing.JButton();
- four = new javax.swing.JButton();
- seven = new javax.swing.JButton();
- multiplication = new javax.swing.JButton();
- two = new javax.swing.JButton();
- five = new javax.swing.JButton();
- eight = new javax.swing.JButton();
- addition = new javax.swing.JButton();
- three = new javax.swing.JButton();
- six = new javax.swing.JButton();
- nine = new javax.swing.JButton();
- subtraction = new javax.swing.JButton();
- zero = new javax.swing.JButton();
- equals = new javax.swing.JButton();
- decimal = new javax.swing.JButton();
- clear = new javax.swing.JButton();
- clearAll = new javax.swing.JButton();
- result = new javax.swing.JFormattedTextField();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- division.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- division.setText("รท");
- division.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- division.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- divisionActionPerformed(evt);
- }
- });
- one.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- one.setText("1");
- one.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- oneActionPerformed(evt);
- }
- });
- four.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- four.setText("4");
- four.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- fourActionPerformed(evt);
- }
- });
- seven.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- seven.setText("7");
- seven.setToolTipText("");
- seven.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- sevenActionPerformed(evt);
- }
- });
- multiplication.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- multiplication.setText("x");
- multiplication.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- multiplicationActionPerformed(evt);
- }
- });
- two.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- two.setText("2");
- two.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- twoActionPerformed(evt);
- }
- });
- five.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- five.setText("5");
- five.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- fiveActionPerformed(evt);
- }
- });
- eight.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- eight.setText("8");
- eight.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- eightActionPerformed(evt);
- }
- });
- addition.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- addition.setText("+");
- addition.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- addition.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- additionActionPerformed(evt);
- }
- });
- three.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- three.setText("3");
- three.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- threeActionPerformed(evt);
- }
- });
- six.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- six.setText("6");
- six.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- six.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- sixActionPerformed(evt);
- }
- });
- nine.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- nine.setText("9");
- nine.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- nineActionPerformed(evt);
- }
- });
- subtraction.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
- subtraction.setText("-");
- subtraction.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- subtraction.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- subtractionActionPerformed(evt);
- }
- });
- zero.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- zero.setText("0");
- zero.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- zeroActionPerformed(evt);
- }
- });
- equals.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
- equals.setText("=");
- equals.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- equalsActionPerformed(evt);
- }
- });
- decimal.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
- decimal.setText(".");
- clear.setText("C");
- clearAll.setText("CA");
- clearAll.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- clearAllActionPerformed(evt);
- }
- });
- result.setEditable(false);
- result.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- resultActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(result)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(one, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(four, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(seven, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(division))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(two, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(five, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(eight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(multiplication, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(zero)))
- .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(decimal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(addition, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(subtraction, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
- .addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addComponent(clearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(0, 0, Short.MAX_VALUE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(clearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(8, 8, 8)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(division, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(one, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(four, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(seven, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(addition, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(subtraction, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(three, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(57, 57, 57)
- .addComponent(nine, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addGroup(layout.createSequentialGroup()
- .addGap(102, 102, 102)
- .addComponent(six, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(decimal, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(multiplication, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(two, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(five, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(eight, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(zero, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(12, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- 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