So I have written the code, and added extra features to the calculator, I also added comments to make everything clear. There are still a few errors which I dont know how to solve. 1. After doing any equation, ie. 10+20 I get 30, but if I want to do more calculations ie. subtract 5, the 5 adds to the value 30 (305). How can I correct this? 2. I dont know how to implement the % and
Salman Fazal You must see this video series. It is probably the best series on calculator. Anything you want to do with the calc. You can do. I assure you won't get any unexpected results!!. Plz Check out
Honestly this is kind of buggy, but it's good to learn basic Java I guess. Whenever the decimal and then +/- is pressed you get an error. If you don't clear after getting the answer and you try and do another sum it just adds the number to the end of the answer and produces errors when you try to press another operation button. Any ideas how to fix this?
I made a nice calculator using HTML, CSS3, and javascript. The button with the divide symbol is an A with a sqiggly line (~) over it and a dot to the right of it. What the hell is that?
why didn't you fix the error and on mine when i try to press = it says java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Anyone who is having troubles with the decimals, replace the Integer.parseInt() methods with Double.parseDouble(). That should fix all problems.
This is a good tutorial but the calculator does not work well. the decimal is completely broken you cannot add, subtract, multiply, divide with it
Im 5 years late but you are awesome thanks a lot for this tutorial
I have been following with this tutorial. I must have made a mistake somewhere. Whenever I click '=' on my calculator GUI, the answer is always 0.0. Please could somebody help me. I made my ints into doubles. By the way, I know it is lined up correctly. I can't be bothered to re line 194 lines of code.
package calculator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 320;
public static final int HEIGHT = 480;
private GridBagLayout layout;
private GridBagConstraints gbc;
private JButton[] numberButtons;
private JButton[] opButtons;
private JTextField field;
private double num1, num2, ans;
private double op;
// [0] = gridx, [1] = gridy, [2] = gridwidth, [3] = gridheight
private int[][] numConstraints = new int[][] {
{0, 5, 2, 1},
{0, 4, 1, 1},
{1, 4, 1, 1},
{2, 4, 1, 1},
{0, 3, 1, 1},
{1, 3, 1, 1},
{2, 3, 1, 1},
{0, 2, 1, 1},
{1, 2, 1, 1},
{2, 2, 1, 1},
};
private int[][] opConstraints = new int[][] {
{2, 5, 1, 1},
{3, 4, 1, 2},
{3, 3, 1, 1},
{3, 2, 1, 1},
{3, 1, 1, 1},
{2, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 1},
};
public Calculator() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
layout = new GridBagLayout();
layout.columnWidths = new int[] {80, 80, 80, 80};
layout.rowHeights = new int[] {80, 80, 80, 80, 80, 80};
setLayout(layout);
gbc = new GridBagConstraints();
numberButtons = new JButton[10];
for (int i = 0; i < numberButtons.length; i++) {
numberButtons[i] = new JButton("" + i);
numberButtons[i].addActionListener(this);
gbc.gridx = numConstraints[i][0];
gbc.gridy = numConstraints[i][1];
gbc.gridwidth = numConstraints[i][2];
gbc.gridheight = numConstraints[i][3];
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
add(numberButtons[i], gbc);
}
opButtons = new JButton[8];
opButtons[0] = new JButton(".");
opButtons[1] = new JButton("=");
opButtons[2] = new JButton("+");
opButtons[3] = new JButton("-");
opButtons[4] = new JButton("*");
opButtons[5] = new JButton("/");
opButtons[6] = new JButton("+/-");
opButtons[7] = new JButton("C");
for(int i = 0; i < opButtons.length; i++) {
gbc.gridx = opConstraints[i][0];
gbc.gridy = opConstraints[i][1];
gbc.gridwidth = opConstraints[i][2];
gbc.gridheight = opConstraints[i][3];
opButtons[i].addActionListener(this);
add(opButtons[i], gbc);
}
field = new JTextField();
field.setBorder(BorderFactory.createLineBorder(Color.BLACK));
field.setEditable(false);
field.setFont(new Font("Arial", Font.PLAIN, 24));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.gridheight = 1;
add(field, gbc);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(new Calculator(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < numberButtons.length; i++) {
if(e.getSource() == numberButtons[i]) {
field.setText(field.getText() + i);
}
}
if(e.getSource() == opButtons[0] && field.getText().contains(".")) {
field.setText(field.getText() + ".");
}
if(e.getSource() == opButtons[6]) {
field.setText("" + (-1 * Double.parseDouble(field.getText())));
}
if(e.getSource() == opButtons[7]) {
field.setText("");
}
if(e.getSource() == opButtons[2]) {
num1 = Double.parseDouble(field.getText());
op = 1;
field.setText("");
}
if(e.getSource() == opButtons[3]) {
num1 = Double.parseDouble(field.getText());
op = 2;
field.setText("");
}
if(e.getSource() == opButtons[4]) {
num1 = Double.parseDouble(field.getText());
op = 3;
field.setText("");
}
if(e.getSource() == opButtons[5]) {
num1 = Double.parseDouble(field.getText());
op = 4;
field.setText("");
}
if(e.getSource() == opButtons[1]) {
num2 = Double.parseDouble(field.getText());
if(op == 1) {
} else if(op == 2) {
ans = num1 + num2;
} else if(op == 3) {
ans = num1 - num2;
} else if(op == 3) {
ans = num1 * num2;
} else if(op == 4) {
ans = num1 / num2;
}
op = 0;
field.setText("" + ans);
}
}
}
why ARE THERE ERRORS IN CONSOLE BUT THERES NO PROBLEMS HUHHHHHHHH
So I have written the code, and added extra features to the calculator, I also added comments to make everything clear. There are still a few errors which I dont know how to solve.
1. After doing any equation, ie. 10+20 I get 30, but if I want to do more calculations ie. subtract 5, the 5 adds to the value 30 (305). How can I correct this?
2. I dont know how to implement the % and
Salman Fazal You must see this video series. It is probably the best series on calculator. Anything you want to do with the calc. You can do. I assure you won't get any unexpected results!!. Plz Check out
Honestly this is kind of buggy, but it's good to learn basic Java I guess. Whenever the decimal and then +/- is pressed you get an error. If you don't clear after getting the answer and you try and do another sum it just adds the number to the end of the answer and produces errors when you try to press another operation button. Any ideas how to fix this?
help please my calculator is so dumbbb
when i type 11+9 it gives me 20 LOL
its 21
you alright mate???
Nice Video!!
I made a nice calculator using HTML, CSS3, and javascript. The button with the divide symbol is an A with a sqiggly line (~) over it and a dot to the right of it. What the hell is that?
Thanks alot for the java bug of addiction ! :)
why didn't you fix the error and on mine when i try to press = it says java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Because you never sent me a comment...
I need to see your code in order to fix it. Send me it using www.pastebin.com
pj6444 thank you i will and i appreciate it and nice video
put some comments :)
Oops I forgot :P