Java calculator app 🖩

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ม.ค. 2025

ความคิดเห็น • 499

  • @BroCodez
    @BroCodez  4 ปีที่แล้ว +631

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Calculator implements ActionListener{
    JFrame frame;
    JTextField textfield;
    JButton[] numberButtons = new JButton[10];
    JButton[] functionButtons = new JButton[9];
    JButton addButton,subButton,mulButton,divButton;
    JButton decButton, equButton, delButton, clrButton, negButton;
    JPanel panel;

    Font myFont = new Font("Ink Free",Font.BOLD,30);

    double num1=0,num2=0,result=0;
    char operator;

    Calculator(){

    frame = new JFrame("Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(420, 550);
    frame.setLayout(null);

    textfield = new JTextField();
    textfield.setBounds(50, 25, 300, 50);
    textfield.setFont(myFont);
    textfield.setEditable(false);

    addButton = new JButton("+");
    subButton = new JButton("-");
    mulButton = new JButton("*");
    divButton = new JButton("/");
    decButton = new JButton(".");
    equButton = new JButton("=");
    delButton = new JButton("Del");
    clrButton = new JButton("Clr");
    negButton = new JButton("(-)");

    functionButtons[0] = addButton;
    functionButtons[1] = subButton;
    functionButtons[2] = mulButton;
    functionButtons[3] = divButton;
    functionButtons[4] = decButton;
    functionButtons[5] = equButton;
    functionButtons[6] = delButton;
    functionButtons[7] = clrButton;
    functionButtons[8] = negButton;

    for(int i =0;i

    • @vickysanth9653
      @vickysanth9653 4 ปีที่แล้ว +25

      it would be more easy to understad for beginners like me if you go slower than usual.. because i don't understand many terms like "ActionListener", interfaces etc.. it's like new to me since I'm a beginner..

    • @sidra3210
      @sidra3210 4 ปีที่แล้ว +3

      Nice👍🔥

    • @shokirovollohberdi
      @shokirovollohberdi 3 ปีที่แล้ว +2

      @@komaltandle465 LOL

    • @agent47hitman75
      @agent47hitman75 3 ปีที่แล้ว +6

      @NOA777 the method Double.parseDouble() casts a value to a double type value. the getText() method is used to get the text from the text field. So Double.parseDouble(txtfield.getText()) is actually casting the string values to a double type value of whatever is written in the rext field as we can't perform calculation with string type of values.

    • @medjl6083
      @medjl6083 3 ปีที่แล้ว

      @NOA777
      The method parseDouble() of wrapper class Double, convert string to double.

  • @deepeshsingh7717
    @deepeshsingh7717 4 ปีที่แล้ว +172

    People are missing out on your amazing content. I am still learning Java, and your content helps a lot.

    • @Bryysanity
      @Bryysanity ปีที่แล้ว +3

      how are you now on your Java journey?

    • @kylewc2286
      @kylewc2286 11 หลายเดือนก่อน +2

      ​@@Bryysanity Im curious too

  • @AhsanSharief
    @AhsanSharief 6 หลายเดือนก่อน +6

    00:03 How to make a simple calculator program using Java
    03:50 Creating a Java calculator app with J buttons and J panel
    07:05 Adding text field and buttons to the calculator app
    10:42 Creating an array of JButtons
    14:32 Adding delete and clear buttons to the calculator app interface.
    17:54 Adding buttons to the panel
    21:34 Adding functionality to decimal button and various math operations
    26:25 Implementing Clear and Delete functionality in Java calculator app
    30:17 Added functionality to the negative button, allowing the user to flip the sign of the number displayed in the text field.
    33:44 How to make a very simple calculator using Java

  • @ashkaneghbali2702
    @ashkaneghbali2702 3 ปีที่แล้ว +28

    Came here straight after the 12 hour java tutorial. You are Awesome Bro. Keep 'em coming :)

  • @milesmiller9588
    @milesmiller9588 4 ปีที่แล้ว +22

    Started practice with GUI's and this helped a lot!

  • @StefaanMeeuws
    @StefaanMeeuws 3 ปีที่แล้ว +12

    Most impressive. Very lucid coding. Congrats on teaching me a little more than I knew!

  • @EbbieMonch
    @EbbieMonch 2 ปีที่แล้ว +5

    This tutorial was very good and even gave the challenge of fixing the Delete and Clear font size while keeping the rest of the numbers and functions font the same size

  • @Sorjen108
    @Sorjen108 2 ปีที่แล้ว +6

    This channel is so underrated
    I went through many tuto on youtube, but none of them was that good(sincerely), and this is in regards to any programming language
    This developer is Gold

  • @alessandroformica6824
    @alessandroformica6824 4 ปีที่แล้ว +36

    Thank you, Bro! My prayers to the algorithm.

  • @flyetimadtravel2157
    @flyetimadtravel2157 4 ปีที่แล้ว +11

    Bro You are legend. I am not a student I started learning JAVA to make my travel agency CRM.

  • @zul_afluz
    @zul_afluz ปีที่แล้ว +2

    Extremely helpful. Got my mini project done within 30 minutes referring your source code and guide. Thankkksss a lotttt ☺👏

  • @slonbeskonechen8310
    @slonbeskonechen8310 4 ปีที่แล้ว +14

    Please, don't stop!!! More and more tutorials!!!!

    • @Zito_from_OHIO
      @Zito_from_OHIO 8 หลายเดือนก่อน +1

      the first sentence (Please, don't stop!!) remember me of a chat i had i chai 😏😏

    • @Ali42480
      @Ali42480 2 หลายเดือนก่อน

      @@Zito_from_OHIO

  • @kumarkelash4423
    @kumarkelash4423 4 ปีที่แล้ว +22

    you are really a great teacher you have made my life easy hahahah... God bless you sir

  • @fmsabisai
    @fmsabisai ปีที่แล้ว +6

    This is an amazing tutorial, thumbs up. I would have loved to see how you handled division by 0. I have also noticed that its possible to have multiple dots in a number which would result in an error during calculations.

    • @yasarcarel
      @yasarcarel 10 หลายเดือนก่อน

      if(e.getSource() == decButton)
      {
      if(textfield.getText().contains("."))
      {
      String temp = textfield.getText();
      textfield.setText("");
      for(int i=0;i

    • @incarnateblizzard4874
      @incarnateblizzard4874 6 หลายเดือนก่อน

      little bit late, but came across the tutorial right now.
      for multiple dots I did (most likely theres a better solution):
      if(e.getSource() == decButton) {
      boolean alrDec = false;
      for(int i = 0; i

  • @rasisulhubbi9316
    @rasisulhubbi9316 2 ปีที่แล้ว

    I just start learning java a month ago and a little bit confuse what to do, and i found your channel, its really help me to practice

  • @halimaomar9820
    @halimaomar9820 2 ปีที่แล้ว +13

    I had a few problems with this application and I fixed one. It looks like when you use the negative button you can't click it first then click the number button you want to be negative. It will cause the program to crash. To fix this problem you can use a try and catch method plus a NumberFormatException.
    The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float). It has this problem with negative values.
    However, the negative button works when you click the number button first then click the negative button. I still put this exception in just in case someone clicks the negative button first.
    It isn't a great fix because I would like the negative button to work both ways so it won't matter which you click first the number or the negative button. When I figure this out then I will update folks. First, I will attempt to replicate what my phone calculator does with negative buttons. They have a +/- option that has a few different behaviors that I can implement.
    The second problem I identified is that the functionality of the negative button doesn't work. I checked Bro Code's source code and copied then pasted it on my Eclipse file but still has the same problem. I knew it would but I wanted to make sure it wasn't just me.
    When you trying adding or multiplying or dividing a number against a negative number it will convert the negative number to -1. No matter what value your negative number is. For example it's -8, it will convert it to -1. Example 1, 3 * -8 = -24, but the calculator will process the equation like this: 3 * -1 = -3. I think maybe I can add a case to the switch statement so I'm going to try that first. Or I can add a loop to the negative button to address the -1 problem.
    As I said before, when I have solutions for this then I will post it to my comment thread.

    • @halimaomar9820
      @halimaomar9820 2 ปีที่แล้ว +3

      Update: I found a solution to the problem I identified with negative numbers. This solution I tested and it works. Negative value numbers won't automatic be converted to -1 but will have the value you intended.
      Code:
      if (e.getSource() == negButton) {
      double num = Double.parseDouble(textfield.getText());
      if (num >= 0) {
      double resultneg = num * -1;
      textfield.setText(resultneg + " ");
      } else {
      double resultpos = num * -1;
      textfield.setText(resultpos + " ");
      }
      If you notice I changed the button to no longer just be a negative button but a +/- button. I did this because of the calculator apps today on phones and computers are setup with +/- functions.

    • @halimaomar9820
      @halimaomar9820 2 ปีที่แล้ว +4

      Full solution code fixing all the bugs/errors:
      try {
      if (e.getSource() == negButton) {
      double num = Double.parseDouble(textfield.getText());
      if (num >= 0) {
      double resultneg = num * -1;
      textfield.setText(resultneg + "");
      } else {
      double resultpos = num * -1;
      textfield.setText(resultpos + "");
      }
      }
      }
      catch (NumberFormatException num) {
      System.out.println(
      "NumberFormatException occurred");
      }

    • @Rakavi-h4f
      @Rakavi-h4f 3 หลายเดือนก่อน +1

      ​@@halimaomar9820 if else condition to check num > 0 or less than or equal to zero , pretty much does the same function as the original bro code.Though it checks explicitly,the results are the same
      Btw, the two possible errors that you have spotted out are correct but unfortunately there is no solution for those in your code

  • @manthanpatki146
    @manthanpatki146 3 ปีที่แล้ว +1

    well I am just commenting to support you because this video has been really helpful and i learnt more ways to use GUI in java.

  • @parkashkumar765
    @parkashkumar765 2 ปีที่แล้ว +1

    You always perfect sir because your way of teaching is all the time perfect. Thank you so much sir.🥰

  • @BlueOrcas
    @BlueOrcas 2 ปีที่แล้ว +2

    I knew bro couldn't miss the negative sign before. He just cares for us bros and was testing us to grow stronger💪

  • @westonpeabody8010
    @westonpeabody8010 2 ปีที่แล้ว +2

    Thanks man. Believe it or not Ive been learning Java for years and until now Ive never actually wrote a fully functional calculator.

  • @mansinaidu
    @mansinaidu ปีที่แล้ว

    bro this was really awesome ....
    my sir told it but it was too confusing
    but your program was clean and neat....
    thank you so much .

  • @karllouiserito
    @karllouiserito 3 ปีที่แล้ว +1

    Thanks Bro I've learned something new today about Java especially that GUI.

  • @noah77
    @noah77 4 ปีที่แล้ว +10

    Cool, this is nice.
    Awesome video.
    And also, I have finished creating my AI ChatBot!!

    • @BroCodez
      @BroCodez  4 ปีที่แล้ว +2

      nice! Which app is it for?

  • @BigBadBicycle
    @BigBadBicycle ปีที่แล้ว +1

    I just begun with Java and made my own calculator like this. But after finishing and seeing this video, there is a lot of things I could do to definetly reduce my code size.

  • @mohammedhh6488
    @mohammedhh6488 2 ปีที่แล้ว +2

    Thank you very much for your assistance, you explained the code perfectly. Thumb Up.

  • @secretchannel2151
    @secretchannel2151 4 ปีที่แล้ว +5

    You are a true Bro. Thank you very much sir! Everyone keep commenting, liking and subbing to help the algorithm!

  • @YaroMalik
    @YaroMalik 11 หลายเดือนก่อน

    Bro the goat. Just finished learning Java with you and am building my first project with you too

  • @Respects103
    @Respects103 11 หลายเดือนก่อน

    best yt for coding i learned many things from you thanks you so much bro code

  • @ChinmayaHelchel
    @ChinmayaHelchel 2 ปีที่แล้ว

    Thank you @BroCode ☺
    It was really up to the point !

  • @gyangaha109
    @gyangaha109 4 ปีที่แล้ว +2

    yo bro, love it
    thanks
    keep up the good work

  • @JEE-nf1cv
    @JEE-nf1cv ปีที่แล้ว

    This was indeed Helpful brother
    Thanks for the tutorial

    • @animegaming7116
      @animegaming7116 ปีที่แล้ว

      the package come back as error what to do

  • @365motivation.9
    @365motivation.9 2 ปีที่แล้ว

    Bro code,this is an amazing tutorial.Thank you Mann,you taught me alot here.

  • @alireza_programmer
    @alireza_programmer 2 ปีที่แล้ว +3

    Thanks a million , It was really nice and perfect for a beginner like me

  • @bellabrink8610
    @bellabrink8610 3 ปีที่แล้ว

    Amazing content as always man, thank you!

  • @sci-figameguy8241
    @sci-figameguy8241 3 ปีที่แล้ว +3

    Hey I was wondering if you could help me, I'm making this using IntelliJ right now and at 6:40 when I run it to check if a frame comes up I get the error: "Error: Could not find or load main class Main" and I see you don't have a main class either but for some reason yours works?

  • @ManuelErica
    @ManuelErica 6 หลายเดือนก่อน

    You're the man. Good job!

  • @biplobahmed6509
    @biplobahmed6509 2 ปีที่แล้ว

    no caption king. learned a lot from you . thnaks for the quality content

  • @Gaminiheraliyawala
    @Gaminiheraliyawala ปีที่แล้ว

    Dear Bro you are simply amazing... ❤❤💐💐being sooo... generous in sharing your wealth of knowledge and you certainly deserve a very big appreciation for making me impressed to learn Java programming. God Bless You Bro...🙏🙏. I really loved it and impatient to try it ASAP..... 👌👌👍👍

  • @mohamedibrahimmorsy1749
    @mohamedibrahimmorsy1749 ปีที่แล้ว +2

    u can add this also to prevent adding more than one decimal point
    if (e.getSource() == decButton) {
    //programming the decimal buttons
    String temp = textField.getText();
    if (!temp.contains(".")) {
    //preventing adding more than one cecimal
    textField.setText(temp + ".");
    }
    }

    • @centice_
      @centice_ ปีที่แล้ว

      Thanks a lot. I need another help. I want to add the feature that is if I press Clr button it should show 0. I can easily do this by just putting textfield.setText("0"); in the clr button function. But the problem arises after that. If any number is given as input after pressing Clr button the zero stays before them, I want the zero disappear if any button is pressed after that. How can I do so?

    • @obamuyipatience922
      @obamuyipatience922 13 วันที่ผ่านมา

      the functionality button for me isnt working

  • @suaddacic7728
    @suaddacic7728 3 ปีที่แล้ว

    Thanks man
    You explain java better than my teacher

  • @muhammadbsoul1459
    @muhammadbsoul1459 2 ปีที่แล้ว

    Thanks pro
    Very helpful video
    Keep going

  • @B_lla008
    @B_lla008 ปีที่แล้ว

    thanks for clean work it really help me !you are super good :)

  • @meguminsatou1591
    @meguminsatou1591 ปีที่แล้ว

    man Thank you i really need this tutorial for my computer programming 2

  • @cutie3127
    @cutie3127 ปีที่แล้ว

    Your amazing Content makes me to passionate about java more...
    I thought to left but ...

  • @thebeginner613
    @thebeginner613 2 ปีที่แล้ว

    Polich machaa..
    You are great 👏🏻👏🏻

  • @கீர்த்திக்வாசன்-ர1ஞ

    awesome broo..keep rocking

  • @mohdaman5928
    @mohdaman5928 2 ปีที่แล้ว

    i truly learn a lot man thanks

  • @gabrielvalentin3106
    @gabrielvalentin3106 3 ปีที่แล้ว

    Excellent as always bro!

  • @etude5942
    @etude5942 6 หลายเดือนก่อน

    Thank you so much for the perfect explanation ❤❤❤❤😭

  • @athulretnakar1907
    @athulretnakar1907 3 หลายเดือนก่อน

    Thankyou for your valuable content 😍

  • @letsgame108
    @letsgame108 2 ปีที่แล้ว +1

    Thank you so much bro. this is really amazing

  • @divyakale1271
    @divyakale1271 ปีที่แล้ว

    Thanks
    Wonderful video ! This video is easy to understand and very helpful.

  • @grenadine9448
    @grenadine9448 3 ปีที่แล้ว +1

    Awesome content !

  • @manasapanda9248
    @manasapanda9248 2 ปีที่แล้ว

    Amazing video I really enjoy this as this is very useful in my program

  • @LiyaAi-fm9ec
    @LiyaAi-fm9ec 5 หลายเดือนก่อน

    Very beautiful, Love it!

  • @orlandohunter2785
    @orlandohunter2785 3 ปีที่แล้ว

    Bro! These videos are awesome 👏

  • @alvydagr8108
    @alvydagr8108 2 ปีที่แล้ว

    Wow even though your style is different, its super easy to understand and frankly, might make me do my calculator over just because It seems easier the way you did it

  • @ahmedshifa
    @ahmedshifa ปีที่แล้ว

    Amazing content, thank you!

  • @neurosp
    @neurosp 3 ปีที่แล้ว

    In new in java , I found that you can type 0,3,4,5,56,6 it take me some time to fix it , also remove 5+2= 7.0 , now working on implement the keyboard . Oh , and eliminate the empty space every time you push +or / or whatever operator.

  • @omersond4891
    @omersond4891 3 ปีที่แล้ว +1

    very helpful video. i understand for loops, if() more than before

  • @Baltazar_Gunar
    @Baltazar_Gunar 3 ปีที่แล้ว

    i just finish second book about java. A Beginners Guide and Complete Reference from Herbert SCHİLDT. I was looking for an example of real GUI software that wasn't beyond my knowledge and actually did something. So I could get an idea of ​​the general programming structure. Your video help me a lot about this. Thank you.

  • @dipesh1401
    @dipesh1401 3 ปีที่แล้ว +4

    for delete button use below code:-
    if(e.getSource()==delButton) {
    String string = textfield.getText();
    textfield.setText(string.substring(0,string.length()-1));
    }
    everytime it will delete the last elemnt of the string.

    • @AnsarAbbas-ed5ig
      @AnsarAbbas-ed5ig 2 ปีที่แล้ว

      Your programme is not right bro

    • @dipesh1401
      @dipesh1401 2 ปีที่แล้ว

      @@AnsarAbbas-ed5ig bro I have tested this code then uploaded here.

    • @AnsarAbbas-ed5ig
      @AnsarAbbas-ed5ig 2 ปีที่แล้ว

      @@dipesh1401 OK bro i hope it would correct

  • @wolanus
    @wolanus 4 ปีที่แล้ว +1

    Another great video. Thanks!

  • @Dontwatchthischannel
    @Dontwatchthischannel 5 หลายเดือนก่อน +2

    Incase you guys didnt know, calc is short for calculator, im just speaking in slang...

  • @stanislavdimitrov1643
    @stanislavdimitrov1643 ปีที่แล้ว

    Great, fantastic. It may seem a little but too advanced at the beginning, but you can try it and see:)

  • @helpdesk9535
    @helpdesk9535 3 ปีที่แล้ว

    Thanks for the help Big Bro!

  • @Sapocrate
    @Sapocrate 3 ปีที่แล้ว

    Thanks! (I appreciate also the 420 on the preview)

  • @Ahmad-jc7by
    @Ahmad-jc7by ปีที่แล้ว

    so enjoyable, thanks for this Man

  • @kemann3815
    @kemann3815 3 ปีที่แล้ว

    Amazing video. That robotic laugh at the end got me 😂

  • @mahaalibrahim7934
    @mahaalibrahim7934 4 ปีที่แล้ว +2

    That’s was a lot of code thanks for your effort

  • @ThunderHockeyTFL
    @ThunderHockeyTFL 3 ปีที่แล้ว

    big thanks, very helpful video! subbed

  • @rudranagariya8546
    @rudranagariya8546 3 ปีที่แล้ว

    Thank you so much bro you are the only best coder

  • @HenocDossou
    @HenocDossou ปีที่แล้ว

    Very cool! Helped me alot!

  • @Hypa_Playz
    @Hypa_Playz ปีที่แล้ว

    Nice content keep it up!!!🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

  • @ritikrajput2894
    @ritikrajput2894 ปีที่แล้ว

    Wonderful Tutorial

  • @quikmath8071
    @quikmath8071 2 ปีที่แล้ว

    Awesome tutorial

  • @Dejuk22
    @Dejuk22 ปีที่แล้ว

    well done bro🙌 thank you!!

  • @ThePraQNome
    @ThePraQNome ปีที่แล้ว

    Great tutorial, thank you.

  • @brianpelarez
    @brianpelarez 2 ปีที่แล้ว

    Good day BRO! Thanks so much I can now code my simple calculator, through your tutorial.

  • @kampfer6375
    @kampfer6375 2 ปีที่แล้ว

    Very well explained 👍

  • @shibildas
    @shibildas 2 ปีที่แล้ว

    Bro, that was awesome ❤️

  • @chiyuzhang3089
    @chiyuzhang3089 ปีที่แล้ว

    Thanks Bro, great video!

  • @omeraran9398
    @omeraran9398 3 ปีที่แล้ว

    I love your channel.

  • @athenkosinyombolo4275
    @athenkosinyombolo4275 3 ปีที่แล้ว

    This was really helpful

  • @lucianodiaz298
    @lucianodiaz298 ปีที่แล้ว

    I liked very much this video and i from Buenos Aires, Argentina

  • @soumelee5661
    @soumelee5661 ปีที่แล้ว

    thanks bro for this video!! very helpful :)

  • @FTech109
    @FTech109 ปีที่แล้ว +2

    I got no idea how do you guys remember the what classes inside the libraries , should we just refer documentation,it's very confusing for me how to use these libraries using documentation

  • @Lugandalitulicalessons
    @Lugandalitulicalessons 2 ปีที่แล้ว +2

    import javax.swing.AbstractAction;
    import java.awt.AWTError;
    import java.awt.AWTEvent;
    My Netbins adds(the last dot continuations) to the original code.How can i fix this?.

  • @Daniel78852
    @Daniel78852 3 ปีที่แล้ว

    Thanks man that helped a lot. Nw I see how Java works.

  • @ichouabdelali8076
    @ichouabdelali8076 3 ปีที่แล้ว

    in this programme , if you put par exemple 5*3*2 = it gives you 6 not 30 , and thank u for every thing else ur a lagende Bro

  • @afnan4311
    @afnan4311 2 ปีที่แล้ว

    I just finished this and I’m sooo happy n I feel awesome😭🫂

  • @infotechlab
    @infotechlab 2 ปีที่แล้ว

    hey bro! thanks a lot for this amazing content..

  • @khusanbekgafurov
    @khusanbekgafurov 2 ปีที่แล้ว

    Awesome job I like it Thanks

  • @Mia_sarahia_kit_official
    @Mia_sarahia_kit_official ปีที่แล้ว

    thank you so much bro code it help me a lot when i watch your vedio. love yah

  • @kakaraka132
    @kakaraka132 4 หลายเดือนก่อน +2

    By the way chat calc is short for Calculator, he is just using slang

  • @reo4465
    @reo4465 2 ปีที่แล้ว

    Thank you so much for making this wonderful vidoes ❤️

  • @glebignites5278
    @glebignites5278 3 ปีที่แล้ว

    Thank you very much! It was very helpful.

  • @ОлегДомащенко-ы7б
    @ОлегДомащенко-ы7б ปีที่แล้ว

    great example. Thanks

  • @YouTubeYab
    @YouTubeYab 9 หลายเดือนก่อน

    why didn't you used id 'string = string.substring(0, string.length() - 1)' at 28:10?

  • @anuvette
    @anuvette ปีที่แล้ว

    thanks for the video.
    for the decimal point button, i added a little bit of if statement to prevent the program from adding more dots to the textfield:
    if(e.getSource()==decButton)
    {
    String myString = textfield.getText();
    if (!myString.contains(".")) { // Check if there is no dot already present
    textfield.setText(textfield.getText().concat("."));
    }
    }