Java calculator app 🖩

Поделиться
HTML-код
  • Опубликовано: 27 июл 2024
  • Java simple calculator program w/ GUI
    #Java #calculator #app
    (NOT A COMPLETE CALCULATOR)
  • НаукаНаука

Комментарии • 472

  • @BroCodez
    @BroCodez  4 года назад +528

    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 3 года назад +21

      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 3 года назад +3

      Nice👍🔥

    • @shokirovblog
      @shokirovblog 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.

  • @StefaanMeeuws
    @StefaanMeeuws 2 года назад +10

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

  • @deepeshsingh7717
    @deepeshsingh7717 4 года назад +147

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

    • @Bryysanity
      @Bryysanity 6 месяцев назад +2

      how are you now on your Java journey?

    • @kylewc2286
      @kylewc2286 5 месяцев назад

      ​@@Bryysanity Im curious too

  • @ashkaneghbali2702
    @ashkaneghbali2702 2 года назад +19

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

  • @EbbieMonch
    @EbbieMonch 2 года назад +4

    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

  • @milesmiller9588
    @milesmiller9588 3 года назад +17

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

  • @rasisulhubbi9316
    @rasisulhubbi9316 Год назад

    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

  • @zul_afluz
    @zul_afluz Год назад +1

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

  • @karllouiserito4777
    @karllouiserito4777 3 года назад +1

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

  • @ChinmayaHelchel
    @ChinmayaHelchel 2 года назад

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

  • @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.

  • @alessandroformica6824
    @alessandroformica6824 3 года назад +34

    Thank you, Bro! My prayers to the algorithm.

  • @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.

  • @BlueOrcas
    @BlueOrcas Год назад +2

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

  • @bellabrink8610
    @bellabrink8610 2 года назад

    Amazing content as always man, thank you!

  • @flyetimadtravel2157
    @flyetimadtravel2157 3 года назад +8

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

  • @manthanpatki146
    @manthanpatki146 3 года назад

    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.

  • @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 .

  • @parkashkumar765
    @parkashkumar765 2 года назад +1

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

  • @gyangaha109
    @gyangaha109 3 года назад +2

    yo bro, love it
    thanks
    keep up the good work

  • @mohammedhh6488
    @mohammedhh6488 2 года назад +2

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

  • @FukSN
    @FukSN 2 года назад +18

    Thanks Bro !!
    These tutorials are nice and quick to run through and show beginners like me how certain code works in a real app.
    I find this more valuable than just a lesson on certain things like arrays.
    I have noticed a slow down on the java vids, any chance of any new quick app tutorial vids?

  • @alvydagr8108
    @alvydagr8108 Год назад

    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

  • @365motivation.9
    @365motivation.9 Год назад

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

  • @alirezahassanieghtedar5010
    @alirezahassanieghtedar5010 2 года назад +3

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

  • @kumarkelash4423
    @kumarkelash4423 3 года назад +20

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

  • @suaddacic7728
    @suaddacic7728 3 года назад

    Thanks man
    You explain java better than my teacher

  • @slonbeskonechen8310
    @slonbeskonechen8310 3 года назад +11

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

    • @Zito_from_OHIO
      @Zito_from_OHIO 2 месяца назад

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

  • @secretchannel2151
    @secretchannel2151 3 года назад +5

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

  • @Baltazar_Gunar
    @Baltazar_Gunar 2 года назад

    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.

  • @YaroMalik
    @YaroMalik 5 месяцев назад

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

  • @wolanus
    @wolanus 3 года назад +1

    Another great video. Thanks!

  • @orlandohunter2785
    @orlandohunter2785 3 года назад

    Bro! These videos are awesome 👏

  • @glebignites5278
    @glebignites5278 3 года назад

    Thank you very much! It was very helpful.

  • @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 4 месяца назад

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

    • @incarnateblizzard4874
      @incarnateblizzard4874 15 дней назад

      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

  • @omersond4891
    @omersond4891 3 года назад +1

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

  • @thebeginner613
    @thebeginner613 2 года назад

    Polich machaa..
    You are great 👏🏻👏🏻

  • @gabrielvalentin3106
    @gabrielvalentin3106 2 года назад

    Excellent as always bro!

  • @kemann3815
    @kemann3815 2 года назад

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

  • @B_lla008
    @B_lla008 Год назад

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

  • @ThunderHockeyTFL
    @ThunderHockeyTFL 2 года назад

    big thanks, very helpful video! subbed

  • @biplobahmed6509
    @biplobahmed6509 Год назад

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

  • @meguminsatou1591
    @meguminsatou1591 10 месяцев назад

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

  • @Respects103
    @Respects103 5 месяцев назад

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

  • @grenadine9448
    @grenadine9448 3 года назад +1

    Awesome content !

  • @rsdigiomoney
    @rsdigiomoney Год назад +1

    I followed through this whole tutorial on Linux with text editor and terminal Javac compiling (no IDE!) and it worked flawlessly even when packaged into a jar file. I feel like I learned stuff but even if I didn’t, I at least understand how the program works and it was fun to feel like I’m coding stuff just by following along

  • @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?

  • @ahmedshifa
    @ahmedshifa Год назад

    Amazing content, thank you!

  • @mahaalibrahim7934
    @mahaalibrahim7934 3 года назад +2

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

  • @ManuelErica
    @ManuelErica 16 дней назад

    You're the man. Good job!

  • @athenkosinyombolo4275
    @athenkosinyombolo4275 3 года назад

    This was really helpful

  • @stanislavdimitrov1643
    @stanislavdimitrov1643 11 месяцев назад

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

  • @mohdaman5928
    @mohdaman5928 2 года назад

    i truly learn a lot man thanks

  • @Sapocrate
    @Sapocrate 2 года назад

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

  • @helpdesk9535
    @helpdesk9535 3 года назад

    Thanks for the help Big Bro!

  • @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.

  • @shibildas
    @shibildas Год назад

    Bro, that was awesome ❤️

  • @brianpelarez
    @brianpelarez Год назад

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

  • @ShahzadKhan-jg3hl
    @ShahzadKhan-jg3hl 2 года назад

    Well defined. Thank you.

  • @raviratnakar3803
    @raviratnakar3803 3 года назад +3

    You make it look very easy. Thanks Bro!

  • @konsti02
    @konsti02 3 года назад

    this is great! Thank you

  • @letsgame108
    @letsgame108 Год назад +1

    Thank you so much bro. this is really amazing

  • @reo4465
    @reo4465 2 года назад

    Thank you so much for making this wonderful vidoes ❤️

  • @coolboy.bladersshow9159
    @coolboy.bladersshow9159 3 года назад

    Thanks bro. You helped me a lot.

  • @victorbalocan
    @victorbalocan 3 года назад

    And when you turning on a calculator, the screen usually shows "0", but not empty value

  • @Gaminiheraliyawala
    @Gaminiheraliyawala 10 месяцев назад

    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..... 👌👌👍👍

  • @user-mk8mj2pe2j
    @user-mk8mj2pe2j 7 месяцев назад

    Very cool! Helped me alot!

  • @djziomsuper
    @djziomsuper 3 года назад +1

    Great video!

  • @user-qe5pk6ke3k
    @user-qe5pk6ke3k 3 года назад +1

    awesome broo..keep rocking

  • @muhammadbsoul1459
    @muhammadbsoul1459 Год назад

    Thanks pro
    Very helpful video
    Keep going

  • @Dejuk22
    @Dejuk22 Год назад

    well done bro🙌 thank you!!

  • @kampfer6375
    @kampfer6375 2 года назад

    Very well explained 👍

  • @Ahmad-jc7by
    @Ahmad-jc7by Год назад

    so enjoyable, thanks for this Man

  • @cutie3127
    @cutie3127 9 месяцев назад

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

  • @manasapanda9248
    @manasapanda9248 2 года назад

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

  • @ryanning4516
    @ryanning4516 3 года назад +3

    awwwwwwwwwwwsome video bro!!! I subscribed to your channel!!! and I learned a lot! and can you make a pokemon type of tutorial?

    • @BroCodez
      @BroCodez  3 года назад

      haha my latest video on FileReaders is pokemon themed actually lol

  • @omeraran9398
    @omeraran9398 3 года назад

    I love your channel.

  • @sci-figameguy8241
    @sci-figameguy8241 2 года назад +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?

  • @irizjuh1321
    @irizjuh1321 Год назад

    Wonderful video! This series has been really helpful in training my way of thinking too! Sometimes I pause the video and finish the line or piece of code before starting the video again so I can check it. Thank you so much for the effort!
    For the delete-button Action-listener, I wrote these alternative lines btw.
    if(e.getSource()==delButton) {
    textField.setText(textField.getText().substring(0,(textField.getText().length() - 1)));
    }
    It takes the text in the textfield and replaces it with a substring that leaves out the last index. This way you don't need to create a for-loop and you can do it with one line instead! I really learned this way of thinking through practicing with your videos - so thanks a lot! :D

  • @ThePraQNome
    @ThePraQNome Год назад

    Great tutorial, thank you.

  • @Daniel78852
    @Daniel78852 2 года назад

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

  • @infotechlab
    @infotechlab Год назад

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

  • @Hypa_Playz
    @Hypa_Playz Год назад

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

  • @etude5942
    @etude5942 13 дней назад

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

  • @rudranagariya8546
    @rudranagariya8546 3 года назад

    Thank you so much bro you are the only best coder

  • @ashnasanam217
    @ashnasanam217 2 года назад

    Underrated!

  • @hamadareda1695
    @hamadareda1695 3 года назад

    thank you
    this is relly amazing

  • @dongnez
    @dongnez 3 года назад +2

    It's a good video but I think it would be better if you use layouts to auto realignment.
    Pd: Good video, sorry for my English.

    • @Mario-cz6tr
      @Mario-cz6tr Год назад

      hey dude what is that if you wouldn't bother explaining, (in whichever lenguage you want)

  • @victorbalocan
    @victorbalocan 3 года назад +1

    That was great example, but what if user will click decButton more than once?

  • @nashiroyohei5925
    @nashiroyohei5925 Год назад

    Thank you. This is my first heavy coding project😂

  • @chiyuzhang3089
    @chiyuzhang3089 Год назад

    Thanks Bro, great video!

  • @afnan4311
    @afnan4311 2 года назад

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

  • @quikmath8071
    @quikmath8071 2 года назад

    Awesome tutorial

  • @JEE-nf1cv
    @JEE-nf1cv 6 месяцев назад

    This was indeed Helpful brother
    Thanks for the tutorial

    • @animegaming7116
      @animegaming7116 6 месяцев назад

      the package come back as error what to do

  • @davidbaker418
    @davidbaker418 4 месяца назад

    Im a little confused as to how the function button loop affects the separate clr and del buttons, it seems to be only setting actionlistneer on the array COPY and not directly on the del or clr buttons added to frame.

  • @halimaomar9820
    @halimaomar9820 2 года назад +3

    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 года назад +2

      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 года назад +2

      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");
      }

  • @ritikrajput2894
    @ritikrajput2894 Год назад

    Wonderful Tutorial

  • @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

  • @abdialemu8391
    @abdialemu8391 Год назад

    you are a hero!