Basic Calculator Using Java with GUI


This tutorial will show you on how to Create a Basic Calculator using java with the four mathematical operations. The calculator is designed with GUI (Graphical User Interface).

The calculator uses a JFrame Form with jTextfield, jCombobox, jLablel, and jButton elements located in your Netbeans tool palletes. We also uses “if Statement” to identify the operation before the program calculate the two value. Please follow all the steps to complete this tutorial.

Basic Calculator in Java with GUI Steps

  1. Create/Add a new JFrameForm in your project.

2. Design the JFrameFrom the same with the image below.

3. Double click your jButton to automatically generate the button event.

  1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  2. }

4. Insert the code below inside your button event.

  1. if (jComboBox1.getSelectedItem().equals("Addition")){
  2. double result = (Double.parseDouble(jTextField1.getText()) + Double.parseDouble(jTextField2.getText()));
  3. jTextField3.setText(Double.toString(result));
  4. }else if (jComboBox1.getSelectedItem().equals("Subtraction")){
  5. double result = (Double.parseDouble(jTextField1.getText()) - Double.parseDouble(jTextField2.getText()));
  6. jTextField3.setText(Double.toString(result));
  7. }else if (jComboBox1.getSelectedItem().equals("Multiplication")){
  8. double result = (Double.parseDouble(jTextField1.getText()) * Double.parseDouble(jTextField2.getText()));
  9. jTextField3.setText(Double.toString(result));
  10. }else if (jComboBox1.getSelectedItem().equals("Division")){
  11. double result = (Double.parseDouble(jTextField1.getText()) / Double.parseDouble(jTextField2.getText()));
  12. jTextField3.setText(Double.toString(result));
  13. }

5. Final Output

Basic Calculator using Java Output

I hope you might find tutorial helpful.If you have any questions or suggestion about Basic Calculator using Java, Please feel free to contact me on our Contact Page.

Related Tutorial you may like: Basic Calculator Using VB.Net

Facebook Comments


Please enter your comment!
Please enter your name here