Java GUI Calculator Tutorial (NEW) Part 4: Operations

Поділитися
Вставка
  • Опубліковано 3 жов 2024
  • Check out my new Kotlin tutorial series: • Video Hey guys, in this video we do operations!

КОМЕНТАРІ • 20

  • @PatrickFeltes
    @PatrickFeltes  10 років тому +2

    Anyone who is having troubles with the decimals, replace the Integer.parseInt() methods with Double.parseDouble(). That should fix all problems.

  • @R3MIXMODZ
    @R3MIXMODZ 10 років тому +1

    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

  • @echoraido
    @echoraido 5 років тому

    Im 5 years late but you are awesome thanks a lot for this tutorial

  • @hamandfraz
    @hamandfraz 10 років тому

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

  • @RealLava
    @RealLava 4 роки тому

    why ARE THERE ERRORS IN CONSOLE BUT THERES NO PROBLEMS HUHHHHHHHH

  • @SalmanFazal01
    @SalmanFazal01 10 років тому

    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

    • @encodism7371
      @encodism7371 9 років тому

      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

  • @MemekingSupreme
    @MemekingSupreme 10 років тому

    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?

  • @meir231
    @meir231 8 років тому

    help please my calculator is so dumbbb
    when i type 11+9 it gives me 20 LOL
    its 21

  • @ntnbansal5181
    @ntnbansal5181 10 років тому

    Nice Video!!

  • @orionlamothe6590
    @orionlamothe6590 9 років тому

    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?

  • @stringlights94
    @stringlights94 10 років тому

    Thanks alot for the java bug of addiction ! :)

  • @thethunderboss4663
    @thethunderboss4663 10 років тому

    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)

    • @PatrickFeltes
      @PatrickFeltes  10 років тому

      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

    • @thethunderboss4663
      @thethunderboss4663 10 років тому

      pj6444 thank you i will and i appreciate it and nice video

  • @BBvidz
    @BBvidz 10 років тому

    put some comments :)