Simple GUI Calculator in Java

Hello world! I'm back. It’s been four years since my last post. I’d been very busy with work in these past few years that I haven’t had time to blog. I missed blogging somehow and this year I figure I’ll get back to blogging again.

We are going to be making simple programming projects starting from now and for our first project, let’s make a simplest calculator using Java. In this project we’ll be using BlueJ as our Java Development Tool.

1. Download BlueJ installer from https://www.bluej.org/ then install it.

2. Click Start>Click BlueJ.

3. Click Project>New Project>For the sake of example, let’s use Java as a project name.



4. Click New Class>Select Class in the class type radio buttons then enter Calc in the Class Name textbox.



5. Double-click the Calc object.



6. A Window containing default BlueJ source code will then appear. Press Ctrl + A to select all the codes, then Press Delete.



7. Enter the following code:

  1. //imports the necessary packages.  
  2. //swing for frame, awt for controls, and event for button events  
  3. import javax.swing.*;  
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6. //creates a class named Calc and prepares it to accept an event  
  7. public class Calc extends JFrame implements ActionListener  
  8. {  
  9. //creates our controls  
  10.   JTextField ansTextField=new JTextField("",18);  
  11.   JButton oneButton=new JButton("1");  
  12.   JButton twoButton=new JButton("2");  
  13.   JButton plusButton=new JButton("+");  
  14.   JButton minusButton=new JButton("-");  
  15.   JButton equalsButton=new JButton("=");  
  16.  //declares three variables  
  17.   int intNum1,intNum2;  
  18.   String strOperator;  
  19.     public Calc()  
  20. {  
  21.    //creates two panels  
  22.    JPanel topPanel=new JPanel();  
  23.    JPanel bottomPanel=new JPanel();  
  24.    //create two layouts  
  25.    BorderLayout border=new BorderLayout(5,5);  
  26.    FlowLayout flow=new FlowLayout(FlowLayout.RIGHT,10,10);  
  27.    //apply the borderLayout to frame  
  28.    setLayout(border);  
  29.    //prepare our buttons to accept an event  
  30.    oneButton.addActionListener(this);  
  31.    twoButton.addActionListener(this);  
  32.    plusButton.addActionListener(this);  
  33.    minusButton.addActionListener(this);  
  34.    equalsButton.addActionListener(this);  
  35.    //adds the textbox to the top panel  
  36.    topPanel.add(ansTextField);  
  37.    //applies flow layout to bottom panel  
  38.    bottomPanel.setLayout(flow);  
  39.    //add the buttons to the bottom panel  
  40.    bottomPanel.add(oneButton);  
  41.    bottomPanel.add(twoButton);  
  42.    bottomPanel.add(plusButton);  
  43.    bottomPanel.add(minusButton);  
  44.    bottomPanel.add(equalsButton);  
  45.    add(topPanel,BorderLayout.NORTH);  
  46.    add(bottomPanel);  
  47.    //setup our frame  
  48.    setSize(200,200);  
  49.    setLocation(0,0);  
  50.    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  51.    setTitle("Calculator");  
  52.    setVisible(true);  
  53.    
  54. }  
  55. public void actionPerformed(ActionEvent e)  
  56. {  
  57.     //getSource determines the name of the control that recieves an event  
  58.     Object source=e.getSource();  
  59.     //if the name of the control that recieves an event is oneButton, display 1 in the textfield  
  60.     if(source==oneButton)  
  61.     {  
  62.         ansTextField.setText(ansTextField.getText() + oneButton.getText());  
  63.     }  
  64.     //if the name of the control is twoButton, display 2 in the textfield  
  65.     //you can add else ifs for numbers 3 to 9. I just leave that to you to figure out  
  66.     else if(source==twoButton)  
  67.     {  
  68.          ansTextField.setText(ansTextField.getText() + twoButton.getText());  
  69.        
  70.     }  
  71.     //if it is plusButton, get the value displayed in the textfield and place it  
  72.     //in a variable named intNum1  
  73.     //Store "+" in the strOperator variable then clear the text field  
  74.     else if(source==plusButton)  
  75.     {  
  76.         intNum1=Integer.parseInt(ansTextField.getText());  
  77.         strOperator="+";  
  78.         ansTextField.setText("");  
  79.           
  80.     }  
  81.    //if it is minusButton, get the value displayed in the textfield and place it  
  82.    //in a variable named intNum1  
  83.    //store "-" in the strOperator variable then clear the text field  
  84.    else if(source==minusButton)  
  85.     {  
  86.         intNum1=Integer.parseInt(ansTextField.getText());  
  87.         strOperator="-";  
  88.         ansTextField.setText("");  
  89.           
  90.     }  
  91.     //if the name of the control that recieves an event is equals button  
  92.     //get the current value of the text field and assign it as a value of intNum2  
  93.     //if the current value of strOperator is + add the two numbers, if it's value is -  
  94.     //subtract the two numbers  
  95.     else if(source==equalsButton)  
  96.     {  
  97.         intNum2=Integer.parseInt(ansTextField.getText());  
  98.         if(strOperator=="+")  
  99.         {  
  100.           ansTextField.setText(Integer.toString(intNum1 + intNum2));   
  101.         }  
  102.           if(strOperator=="-")  
  103.         {  
  104.           ansTextField.setText(Integer.toString(intNum1 - intNum2));   
  105.         }  
  106.           
  107.     }  
  108. }  
  109. //Creates an instance of our class so that we can run it  
  110. public static void main(String[] args)  
  111. {  
  112.   Calc c=new Calc();    
  113. }  
  114. }  


8. Click the compile button.

9. Close the code window.

10. Right-click the class object>Select void main(String[] args) to run your application.



11. You should then see the following output:



You can use this and make a complete basic calculator. Of course I leave that to you to figure out. If you are having a hard time, you can download the source code here Good luck!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.