Java Tutorial: Basic GUI Calculator Part 2

///Java Tutorial: Basic GUI Calculator Part 2

Java Tutorial: Basic GUI Calculator Part 2

FavoriteLoadingAdd to favorites

Sorry for the wait! I’ve been busy working on… THINGS 😀

Java468x60

TURN IT TO 1080P 😀

source

By |2020-05-13T03:25:51+00:00May 13th, 2020|Java Video Tutorials|19 Comments

19 Comments

  1. Zip Code May 13, 2020 at 3:25 am - Reply

    great tutorials Bucky you are like the awsomest teacher around

  2. Ulli May 13, 2020 at 3:25 am - Reply

    Hi expert. I'm just starting with Java (at 69) and am happy to find and follow your videos. (I tried to do it by the book/videos). But some error messages come upat the console if I run it in eclipse-neon2. Please help me:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Calc cannot be resolved to a type
    Calc cannot be resolved to a type
    Illegal modifier for the local class Calc; only abstract or final is permitted

    at Gui.<init>(Gui.java:63) >>line 63 Calc c = new Calc(); //this gives the error message
    at Main.main(Main.java:5) >> line5 Gui g = new Gui();
    +++ confusing: line 62 Numbers n = new Numbers(); >> gives no error message
    line 325 reads: private class Calc implements ActionListener{ >> this is marked as error;
    but it works well with: private class Numbers implements ActionListener{ Thanks a lot!

  3. YoungTRell May 13, 2020 at 3:25 am - Reply

    I know I am really late but what if I wanted to do say 10+10-15 or just continuous calculation?

  4. Munasib Hossain May 13, 2020 at 3:25 am - Reply

    variable opChosen is not initialissed. cant find it anywhere before the if statement -_-

  5. Varsik Nikoyan May 13, 2020 at 3:25 am - Reply

    Can you explain me please why do we declare the equalsClicked boolean?
    it is never defined, it is always false, I can't understand how does it show is equal clicked or not?
    p.s. the program works normally without it

  6. Christine Mack May 13, 2020 at 3:25 am - Reply

    Hi, i tried your code and mine doesn't display on the textfield. Here's my code:
    package calculator;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class gui extends JFrame
    {
    private JTextField answerfield;
    private JButton one,two, three, four,five,six,seven,eight,nine,zero,add,sub,equals;
    private String stemp1, stemp2, sanswer;
    private double answer=0.0;
    private JPanel contentPanel;
    private boolean equalsClicked = false, opChosen = false;
    public gui()
    {
    super("calculator");

    answerfield = new JTextField(null,20);
    answerfield.setEditable(false);

    one = new JButton("1");
    two = new JButton("2");
    three = new JButton("3");
    four = new JButton("4");
    five = new JButton("5");
    six = new JButton("6");
    seven = new JButton("7");
    eight = new JButton("8");
    nine = new JButton("9");
    zero = new JButton("0");
    add = new JButton("+");
    sub = new JButton("-");
    equals = new JButton("=");

    //action listeners
    Numbers n = new Numbers();
    Calc c = new Calc();

    one.addActionListener(n);
    two.addActionListener(n);
    three.addActionListener(n);
    four.addActionListener(n);
    five.addActionListener(n);
    six.addActionListener(n);
    seven.addActionListener(n);
    eight.addActionListener(n);
    nine.addActionListener(n);
    zero.addActionListener(n);

    contentPanel = new JPanel();
    contentPanel.setBackground(Color.ORANGE);
    contentPanel.setLayout(new FlowLayout());
    contentPanel.add(answerfield,BorderLayout.NORTH);

    contentPanel.add(one);contentPanel.add(two);contentPanel.add(three);contentPanel.add(four);
    contentPanel.add(five);contentPanel.add(six);contentPanel.add(seven);contentPanel.add(eight);
    contentPanel.add(nine);contentPanel.add(zero);contentPanel.add(add);contentPanel.add(sub);
    contentPanel.add(equals);

    this.setContentPane(contentPanel);

    }

    private class Numbers implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    JButton src = (JButton) event.getSource();
    if(src.equals(one))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="1";
    }
    else
    {
    stemp1 += "1";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="1";
    }
    else
    {
    stemp2 += "1";
    }
    }

    }
    if(src.equals(two))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="2";
    }
    else
    {
    stemp1 += "2";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="2";
    }
    else
    {
    stemp2 += "2";
    }
    }

    }
    if(src.equals(three))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="3";
    }
    else
    {
    stemp1 += "3";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="3";
    }
    else
    {
    stemp2 += "3";
    }
    }

    }
    if(src.equals(four))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="4";
    }
    else
    {
    stemp1 += "4";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="4";
    }
    else
    {
    stemp2 += "4";
    }
    }

    }

    if(src.equals(five))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="5";
    }
    else
    {
    stemp1 += "5";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="5";
    }
    else
    {
    stemp2 += "5";
    }
    }

    }
    if(src.equals(six))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="6";
    }
    else
    {
    stemp1 += "6";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="6";
    }
    else
    {
    stemp2 += "6";
    }
    }

    }
    if(src.equals(seven))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="7";
    }
    else
    {
    stemp1 += "7";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="7";
    }
    else
    {
    stemp2 += "7";
    }
    }

    }
    if(src.equals(eight))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="8";
    }
    else
    {
    stemp1 += "8";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="8";
    }
    else
    {
    stemp2 += "8";
    }
    }

    }
    if(src.equals(nine))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="9";
    }
    else
    {
    stemp1 += "9";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="9";
    }
    else
    {
    stemp2 += "9";
    }
    }

    }
    if(src.equals(zero))
    {
    if(opChosen==false)
    {
    if(stemp1==null)
    {
    stemp1="0";
    }
    else
    {
    stemp1 += "0";
    }
    }
    else
    {
    if(stemp2==null)
    {
    stemp2="0";
    }
    else
    {
    stemp2 += "0";
    }
    }

    }

    if(equalsClicked=false)
    {
    if(opChosen==false)
    {
    answerfield.setText(stemp1);
    }
    else
    {
    answerfield.setText(stemp2);
    }
    }

    }
    }

    private class Calc implements ActionListener
    {
    public void actionPerformed(ActionEvent event)
    {
    JButton src = (JButton) event.getSource();
    }
    }
    }

  7. kris hardeez May 13, 2020 at 3:25 am - Reply

    Nice Tutorial really helpful Keep it up!!

  8. Prince Chaudhary May 13, 2020 at 3:25 am - Reply

    your hella funny bro Nice vid though

  9. Joe Hart May 13, 2020 at 3:25 am - Reply

    Hi bro, I don't know why but I've ended up with loads of Curley brackets at the end? And plus my calculation is not working?

  10. mm ray94 May 13, 2020 at 3:25 am - Reply

    I did everything you did at the beginning the numbers were working but once I finished it didn't work at all !!

  11. saeid nazari May 13, 2020 at 3:25 am - Reply

    How long are you working with Java and how long does it take to learn?

  12. Neel Wadhwana May 13, 2020 at 3:25 am - Reply

    For some reason, whenever I click on my buttons on the calculator, nothing appears in the text field. Can you please help me with this?

  13. aa moh May 13, 2020 at 3:25 am - Reply

    🙂

  14. egmccare May 13, 2020 at 3:25 am - Reply

    Part 1 and part 2 work properly, I am using eclipse for Mac. Thanks for sharing your knowledge! Next parts 3 and 4 …!

  15. Vaseline200 May 13, 2020 at 3:25 am - Reply

    the opening is quite scary= =''

  16. Hudaz Khu May 13, 2020 at 3:25 am - Reply

    Can you plss help me??. i got error about the if(src.equal(one))?.. cannot find the symbol. how am i supposed to fix it??

  17. MrKomp23 May 13, 2020 at 3:25 am - Reply

    Hi for some reason the Calc c = new Calc(); gives the error that Calc cant be resolved to a type. Please help.

  18. fidorestiada May 13, 2020 at 3:25 am - Reply

    Mine doesn't run, properly.What did you wrote on the Calc class?

  19. Mayer Scharlat May 13, 2020 at 3:25 am - Reply

    great tutorial! learned a ton. Thanks so much.

Leave A Comment

*