Комментарии:
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));
}
}
}
This was indeed Helpful brother
Thanks for the tutorial
Thank You ❤
ОтветитьAmazing!
ОтветитьVery cool! Helped me alot!
Ответитьthe decbutton button is reapting can you make one time
ОтветитьThanks a lot bro. I already made it
ОтветитьActionListener not work
ОтветитьThanks!!!
Ответитьwhat pulgin should I first install to build the calculator????
ОтветитьGod when😢
Ответитьwhy did you implement constructor
Ответитьcan you change the color of the calculator?
Ответитьthank you
Ответить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
Ответитьit can count for single digit how can we use for double or more digit?
ОтветитьNice
Ответить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 + ".");
}
}
guds
ОтветитьThanks
Wonderful video ! This video is easy to understand and very helpful.
Your amazing Content makes me to passionate about java more...
I thought to left but ...
Thanks legend🫡💪
Ответить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..... 👌👌👍👍
ОтветитьBest code
Ответитьman Thank you i really need this tutorial for my computer programming 2
Ответитьmines working but the result is not correct
how do I fix it?
thanks bro
Ответитьsuch are the people who are not stingy.
Ответитьthanks.
ОтветитьGod of Java this guy💯💯❤️🤞
ОтветитьGreat, fantastic. It may seem a little but too advanced at the beginning, but you can try it and see:)
Ответить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
thanks a lot , but why when i used for loop for focButton , it didn't work ;
Ответить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("."));
}
}
<3
ОтветитьBro pls... Send the full program code to help ful others to do project... Copy to paste
Ответитьvajco
Ответитьthanks for clean work it really help me !you are super good :)
ОтветитьGreat tutorial, 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.
Ответить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 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
ОтветитьThanks man
ОтветитьThank you very much
Ответитьnice :)
Ответить