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("(-)");
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..
@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.
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
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
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.
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?
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
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.
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.
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
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
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.
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..... 👌👌👍👍
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?
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
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.
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.
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.
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
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..
Nice👍🔥
@@komaltandle465 LOL
@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.
@NOA777
The method parseDouble() of wrapper class Double, convert string to double.
Most impressive. Very lucid coding. Congrats on teaching me a little more than I knew!
People are missing out on your amazing content. I am still learning Java, and your content helps a lot.
how are you now on your Java journey?
@@Bryysanity Im curious too
Came here straight after the 12 hour java tutorial. You are Awesome Bro. Keep 'em coming :)
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
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
Started practice with GUI's and this helped a lot!
off course same here bro😎
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
Extremely helpful. Got my mini project done within 30 minutes referring your source code and guide. Thankkksss a lotttt ☺👏
Thanks Bro I've learned something new today about Java especially that GUI.
Thank you @BroCode ☺
It was really up to the point !
Thanks man. Believe it or not Ive been learning Java for years and until now Ive never actually wrote a fully functional calculator.
Thank you, Bro! My prayers to the algorithm.
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.
I knew bro couldn't miss the negative sign before. He just cares for us bros and was testing us to grow stronger💪
Amazing content as always man, thank you!
Bro You are legend. I am not a student I started learning JAVA to make my travel agency CRM.
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.
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 .
You always perfect sir because your way of teaching is all the time perfect. Thank you so much sir.🥰
yo bro, love it
thanks
keep up the good work
Thank you very much for your assistance, you explained the code perfectly. Thumb Up.
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?
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
Bro code,this is an amazing tutorial.Thank you Mann,you taught me alot here.
Thanks a million , It was really nice and perfect for a beginner like me
you are really a great teacher you have made my life easy hahahah... God bless you sir
Thanks man
You explain java better than my teacher
Please, don't stop!!! More and more tutorials!!!!
the first sentence (Please, don't stop!!) remember me of a chat i had i chai 😏😏
You are a true Bro. Thank you very much sir! Everyone keep commenting, liking and subbing to help the algorithm!
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.
Bro the goat. Just finished learning Java with you and am building my first project with you too
Another great video. Thanks!
Bro! These videos are awesome 👏
Thank you very much! It was very helpful.
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.
if(e.getSource() == decButton)
{
if(textfield.getText().contains("."))
{
String temp = textfield.getText();
textfield.setText("");
for(int i=0;i
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
very helpful video. i understand for loops, if() more than before
Polich machaa..
You are great 👏🏻👏🏻
Excellent as always bro!
Amazing video. That robotic laugh at the end got me 😂
thanks for clean work it really help me !you are super good :)
big thanks, very helpful video! subbed
no caption king. learned a lot from you . thnaks for the quality content
man Thank you i really need this tutorial for my computer programming 2
best yt for coding i learned many things from you thanks you so much bro code
Awesome content !
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
Cool, this is nice.
Awesome video.
And also, I have finished creating my AI ChatBot!!
nice! Which app is it for?
Amazing content, thank you!
That’s was a lot of code thanks for your effort
You're the man. Good job!
This was really helpful
Great, fantastic. It may seem a little but too advanced at the beginning, but you can try it and see:)
i truly learn a lot man thanks
Thanks! (I appreciate also the 420 on the preview)
Thanks for the help Big Bro!
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.
Bro, that was awesome ❤️
Good day BRO! Thanks so much I can now code my simple calculator, through your tutorial.
Well defined. Thank you.
You make it look very easy. Thanks Bro!
this is great! Thank you
Thank you so much bro. this is really amazing
Thank you so much for making this wonderful vidoes ❤️
Thanks bro. You helped me a lot.
And when you turning on a calculator, the screen usually shows "0", but not empty value
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..... 👌👌👍👍
Very cool! Helped me alot!
Great video!
awesome broo..keep rocking
Thanks pro
Very helpful video
Keep going
well done bro🙌 thank you!!
Very well explained 👍
so enjoyable, thanks for this Man
Your amazing Content makes me to passionate about java more...
I thought to left but ...
Amazing video I really enjoy this as this is very useful in my program
awwwwwwwwwwwsome video bro!!! I subscribed to your channel!!! and I learned a lot! and can you make a pokemon type of tutorial?
haha my latest video on FileReaders is pokemon themed actually lol
I love your channel.
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?
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
Great tutorial, thank you.
Thanks man that helped a lot. Nw I see how Java works.
hey bro! thanks a lot for this amazing content..
Nice content keep it up!!!🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
Thank you so much for the perfect explanation ❤❤❤❤😭
Thank you so much bro you are the only best coder
Underrated!
thank you
this is relly amazing
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.
hey dude what is that if you wouldn't bother explaining, (in whichever lenguage you want)
That was great example, but what if user will click decButton more than once?
Thank you. This is my first heavy coding project😂
Thanks Bro, great video!
I just finished this and I’m sooo happy n I feel awesome😭🫂
Awesome tutorial
This was indeed Helpful brother
Thanks for the tutorial
the package come back as error what to do
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.
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.
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.
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");
}
Wonderful Tutorial
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
you are a hero!