• Good News


Profile
France Luck

Create a simple Calculator using Netbeans

I made a tutorial about creating a simple Calculator using Netbeans, I Hope it helps Thank you.

public class SimpleCalculatorUI extends javax.swing.JFrame {
double num1;
double num2;
double result;
String Operation;
boolean period = false;

Buttons 1 to 9 and 0

String getnumbers = jTextField1.getText() + jButton1.getText();
 jTextField1.setText(getnumbers);

Paste this program to other buttons and just change its value example (jButton2).

For Arithmethic Operations Addition (+) Subtraction (-) Division (/) Multiplication (*).

ADDITION
num1 = Double.parseDouble(jTextField1.getText());
        jTextField1.setText("");
        Operation = "+";
        period = false;

SUBTRACTION
num1 = Double.parseDouble(jTextField1.getText());
        jTextField1.setText("");
        Operation = "-";
        period = false;

DIVISION
num1 = Double.parseDouble(jTextField1.getText());
        jTextField1.setText("");
        Operation = "/";
        period = false;

MULTIPLICATION
num1 = Double.parseDouble(jTextField1.getText());
        jTextField1.setText("");
        Operation = "*";
        period = false;

For positive and negative buttons (+/-)

  double opera = Double.parseDouble(String.valueOf(jTextField1.getText()));
        opera = opera * (-1);
        jTextField1.setText(String.valueOf(opera));

For decimal button (.)
String getnumbers = jTextField1.getText() + jButton16.getText();
        jTextField1.setText(getnumbers);

Clear Button
jTextField1.setText("");

For Equal button

String Answers;
        num2 = Double.parseDouble(jTextField1.getText());
        
        switch (Operation){
            
            case "+":
            result = num1+num2;
            break;
            
            case "-":
            result = num1-num2;
            break;
            
            case "/":
            result = num1/num2;
            break;
                   
            case "*":
            result = num1*num2;
            break;
        }
        
        jTextField1.setText(String.valueOf(result));

THANKS HOPE IT HELPS.
        

Comments

1 Comments
  • Anime Feels
    Oct 12, 2020 23:04
    Ohhh thank you!! Gonna watch this ❤️