Java calculator app

Java calculator app

Bro Code

4 года назад

412,507 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@BroCodez
@BroCodez - 09.07.2020 04:30

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<9;i++) {
functionButtons[i].addActionListener(this);
functionButtons[i].setFont(myFont);
functionButtons[i].setFocusable(false);
}

for(int i =0;i<10;i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myFont);
numberButtons[i].setFocusable(false);
}

negButton.setBounds(50,430,100,50);
delButton.setBounds(150,430,100,50);
clrButton.setBounds(250,430,100,50);

panel = new JPanel();
panel.setBounds(50, 100, 300, 300);
panel.setLayout(new GridLayout(4,4,10,10));

panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);

frame.add(panel);
frame.add(negButton);
frame.add(delButton);
frame.add(clrButton);
frame.add(textfield);
frame.setVisible(true);
}

public static void main(String[] args) {

Calculator calc = new Calculator();
}

@Override
public void actionPerformed(ActionEvent e) {

for(int i=0;i<10;i++) {
if(e.getSource() == numberButtons[i]) {
textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}
if(e.getSource()==decButton) {
textfield.setText(textfield.getText().concat("."));
}
if(e.getSource()==addButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='+';
textfield.setText("");
}
if(e.getSource()==subButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='-';
textfield.setText("");
}
if(e.getSource()==mulButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='*';
textfield.setText("");
}
if(e.getSource()==divButton) {
num1 = Double.parseDouble(textfield.getText());
operator ='/';
textfield.setText("");
}
if(e.getSource()==equButton) {
num2=Double.parseDouble(textfield.getText());

switch(operator) {
case'+':
result=num1+num2;
break;
case'-':
result=num1-num2;
break;
case'*':
result=num1*num2;
break;
case'/':
result=num1/num2;
break;
}
textfield.setText(String.valueOf(result));
num1=result;
}
if(e.getSource()==clrButton) {
textfield.setText("");
}
if(e.getSource()==delButton) {
String string = textfield.getText();
textfield.setText("");
for(int i=0;i<string.length()-1;i++) {
textfield.setText(textfield.getText()+string.charAt(i));
}
}
if(e.getSource()==negButton) {
double temp = Double.parseDouble(textfield.getText());
temp*=-1;
textfield.setText(String.valueOf(temp));
}
}
}

Ответить
@tusharkumar5050
@tusharkumar5050 - 15.01.2024 16:58

great

Ответить
@JEE-nf1cv
@JEE-nf1cv - 09.01.2024 16:15

This was indeed Helpful brother
Thanks for the tutorial

Ответить
@mostafayounes9490
@mostafayounes9490 - 31.12.2023 00:09

Thank You ❤

Ответить
@Wisdom-Thoughts
@Wisdom-Thoughts - 29.12.2023 16:35

Amazing!

Ответить
@user-mk8mj2pe2j
@user-mk8mj2pe2j - 28.12.2023 05:45

Very cool! Helped me alot!

Ответить
@user-ye5jq9wh5b
@user-ye5jq9wh5b - 22.12.2023 13:37

the decbutton button is reapting can you make one time

Ответить
@duybao6426
@duybao6426 - 19.12.2023 07:16

Thanks a lot bro. I already made it

Ответить
@reynaldobuscagan7731
@reynaldobuscagan7731 - 14.12.2023 10:18

ActionListener not work

Ответить
@smf722
@smf722 - 13.12.2023 14:44

Thanks!!!

Ответить
@yohamuch4851
@yohamuch4851 - 11.12.2023 20:11

what pulgin should I first install to build the calculator????

Ответить
@nathanuseni1953
@nathanuseni1953 - 09.12.2023 19:59

God when😢

Ответить
@adithyanmanoj190
@adithyanmanoj190 - 07.12.2023 05:00

why did you implement constructor

Ответить
@yuan166
@yuan166 - 06.12.2023 12:07

can you change the color of the calculator?

Ответить
@45noam78
@45noam78 - 03.12.2023 17:59

thank you

Ответить
@omni2852
@omni2852 - 17.11.2023 09:10

How do you run your program even if you are not finish with the code? I was following your code then my application wont run because of error. I was checking the design output

Ответить
@nexxerjm348
@nexxerjm348 - 13.11.2023 09:06

it can count for single digit how can we use for double or more digit?

Ответить
@khalidhashimeh
@khalidhashimeh - 12.11.2023 05:55

Nice

Ответить
@mohamedibrahimmorsy1749
@mohamedibrahimmorsy1749 - 25.10.2023 11:57

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

}

Ответить
@Reigh992
@Reigh992 - 16.10.2023 15:08

guds

Ответить
@divyakale1271
@divyakale1271 - 14.10.2023 08:36

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

Ответить
@cutie3127
@cutie3127 - 06.10.2023 20:27

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

Ответить
@milanuzelac8787
@milanuzelac8787 - 03.10.2023 16:21

Thanks legend🫡💪

Ответить
@Gaminiheraliyawala
@Gaminiheraliyawala - 27.09.2023 00:47

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

Ответить
@abebayehujm
@abebayehujm - 20.09.2023 11:25

Best code

Ответить
@meguminsatou1591
@meguminsatou1591 - 19.09.2023 11:52

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

Ответить
@kintarofumetsugawa2171
@kintarofumetsugawa2171 - 18.09.2023 04:41

mines working but the result is not correct
how do I fix it?

Ответить
@nikolacywinska
@nikolacywinska - 08.09.2023 18:03

thanks bro

Ответить
@user-px5pj7ux5k
@user-px5pj7ux5k - 29.08.2023 03:05

such are the people who are not stingy.

Ответить
@jaicsdx
@jaicsdx - 25.08.2023 23:13

thanks.

Ответить
@user-bs7dc3oc6y
@user-bs7dc3oc6y - 18.08.2023 10:42

God of Java this guy💯💯❤️🤞

Ответить
@stanislavdimitrov1643
@stanislavdimitrov1643 - 10.08.2023 17:29

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

Ответить
@navaneeth__
@navaneeth__ - 30.07.2023 11:25

We need to click the equal button between the operation like 1+2=3+2=5
How can solve it click equal button between 2 operations

Ответить
@MohamadMERHI-rd7cp
@MohamadMERHI-rd7cp - 28.07.2023 12:24

thanks a lot , but why when i used for loop for focButton , it didn't work ;

Ответить
@anuvette
@anuvette - 26.07.2023 18:55

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

}

Ответить
@user-hm3nw6xz6h
@user-hm3nw6xz6h - 16.07.2023 18:05

<3

Ответить
@karthikk78
@karthikk78 - 13.07.2023 15:15

Bro pls... Send the full program code to help ful others to do project... Copy to paste

Ответить
@traidix6226
@traidix6226 - 13.07.2023 12:14

vajco

Ответить
@BellaSeyda
@BellaSeyda - 12.07.2023 15:51

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

Ответить
@ThePraQNome
@ThePraQNome - 11.07.2023 06:18

Great tutorial, thank you.

Ответить
@fmsabisai
@fmsabisai - 10.07.2023 11:23

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.

Ответить
@BigBadBicycle
@BigBadBicycle - 07.07.2023 01:29

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.

Ответить
@rsdigiomoney
@rsdigiomoney - 05.07.2023 02:22

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

Ответить
@user-yu3sd3vd6g
@user-yu3sd3vd6g - 02.07.2023 07:41

Thanks man

Ответить
@deshanchathurawickramasing8090
@deshanchathurawickramasing8090 - 30.06.2023 03:01

Thank you very much

Ответить
@zari_723
@zari_723 - 23.06.2023 09:48

nice :)

Ответить