Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


gui calculator filter_list
Author
Message
gui calculator #1
I've recently started learning GUI and decided to practice that's why i've decided to start with a simple calculator .
Code:
package calgui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calc extends JFrame implements ActionListener {

    private JFrame f1;
    private JTextArea text1;
    private JTextArea text2;
    private JTextArea text3;
    private JRadioButton sum, minus, division, multiply;
    private JButton calculate, clear;
    private JPanel pan1, pan2, pan3, pan4, pan5, pan6;

    public Calc() {
        pan1 = new JPanel();
        pan2 = new JPanel();
        pan3 = new JPanel();
        pan4 = new JPanel();
        pan5 = new JPanel();
        pan6 = new JPanel();

        text1 = new JTextArea(1, 12);
        text2 = new JTextArea(1, 12);
        text3 = new JTextArea(3, 12);
        sum = new JRadioButton("Sum");
        minus = new JRadioButton("Minus");
        division = new JRadioButton("division");
        multiply = new JRadioButton("multiply");
        calculate = new JButton("calculate");
        clear = new JButton("clear");
        ButtonGroup group = new ButtonGroup();
        group.add(sum);
        group.add(division);
        group.add(minus);
        group.add(multiply);

        pan1.setLayout(new BorderLayout());
        pan2.setLayout(new GridLayout(1, 1, 10, 0));
        pan3.setLayout(new GridLayout(1, 1, 0, 10));
        pan4.setLayout(new GridLayout(1, 1, 0, 10));
        pan6.setLayout(new GridLayout(1, 2));
        setLayout(new BorderLayout());

        pan1.add(pan2, BorderLayout.NORTH);
        pan1.add(pan3, BorderLayout.WEST);
        pan1.add(pan4, BorderLayout.EAST);
        pan2.add(text1);
        pan2.add(text2);
        pan3.add(sum);
        pan3.add(minus);
        pan4.add(division);
        pan4.add(multiply);
        pan5.add(text3);
        pan6.add(calculate);
        pan6.add(clear);

        add(pan1, BorderLayout.NORTH);
        add(pan5, BorderLayout.CENTER);
        add(pan6, BorderLayout.SOUTH);

        calculate.addActionListener(this);
        clear.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(435, 150);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        int i;
        if (source == calculate) {
            if (sum.isSelected()) {
                i = Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText());
                text3.setText(Integer.toString(i));
            }
            if (minus.isSelected()) {
                i = Integer.parseInt(text1.getText()) - Integer.parseInt(text2.getText());
                text3.setText(Integer.toString(i));
            }
            if (division.isSelected()) {
                i = Integer.parseInt(text1.getText()) / Integer.parseInt(text2.getText());
                text3.setText(Integer.toString(i));
            }
            if (multiply.isSelected()) {
                i = Integer.parseInt(text1.getText()) * Integer.parseInt(text2.getText());
                text3.setText(Integer.toString(i));
            }
        } else {
            text1.setText(null);
            text2.setText(null);
            text3.setText(null);
        }
    }
}

Code:
package calgui;

public class CalGui {

    public static void main(String[] args) {
        new Calc();

    }
}

I'm working now on a better version the look is done i still have some calculation code to work on i'll post it when i'm done.

Reply

RE: gui calculator #2
Nice Project, but here are some recs which will get you pretty far and let you learn alot about how programming works:

make the program so that it will add/subtract unlimited lengths of numbers. An int can only hold 32bits, or 4bytes ( I think, this is what an int holds in Java, correct me if i'm wrong) regardless, an int has a cap on how large of a number it can store. So, for large calculations this is obviously not good.
How about storing the number in a string as it comes in, this means you can store a much larger number. Then, implement a function that will add/subtract. Basically, you're 'implementing' addition and subtraction.

Maybe that makes sense maybe it doesn't. Essentially it's this:
you have a string "1235472364832327432643289464573261"
and another string "384627395074369850846284070978473"

the first question you need to ask is, how do you add/subtract in real life?
well, ya go number by number
1+3 = 4, got it, store that 4 in a string which will be your sum (in such a way that you can add numbers before it.) Maybe a String isn't your best option maybe you can think of something better, but a String would work. It cannot be an int, because an int would be too small to hold the added number.
6+7 = 13 --> here's a problem, because if you put thirteen in as the number, it's not going to add right, so you have to carry over the one. This is where it gets interesting. This also reveals a bit about how a computer's CPU works. There's actually a circuit that needs to deal with carrying over digits.

Hope my suggestion helps~~~



I think you'll be able to figure it out Wink
(This post was last modified: 10-24-2016, 04:40 AM by insidious.)
[Image: pBD38Xq.png]
Email: insidious@protonmail.ch

[+] 1 user Likes insidious's post
Reply

RE: gui calculator #3
Thanks for your comment @insidious15 much appreciated this is a good idea you have here but i'm still on a really early version of this calculator and my whole project is about GUI wasn't much interested in the calculation but yeah your right going for Strings to do the calculation looks good but i think using long instead of int should be enough for a small project (MAX_VALUE of long is 9,223,372,036,854,775,807 ).
i'll update the thread as soon as i'm done with my second version,am already done with the gui and it looks way better.
Still have the calculation to work on .
(This post was last modified: 10-24-2016, 11:48 AM by Vi-Sion.)

[+] 1 user Likes Vi-Sion's post
Reply

RE: gui calculator #4
(10-24-2016, 11:41 AM)Vi-Sion Wrote: Thanks for your comment @insidious15 much appreciated this is a good idea you have here but i'm still on a really early version of this calculator and my whole project is about GUI wasn't much interested in the calculation but yeah your right going for Strings to do the calculation looks good but i think using long instead of int should be enough for a small project (MAX_VALUE of long is 9,223,372,036,854,775,807 ).
i'll update the thread as soon as i'm done with my second version,am already done with the gui and it looks way better.
Still have the calculation to work on .

Nice! Well good luck with it. GUI's were never my thing, have always preferred a CLI.

Cool that you're doing it though, good luck!
(This post was last modified: 10-24-2016, 03:36 PM by insidious.)
[Image: pBD38Xq.png]
Email: insidious@protonmail.ch

Reply

RE: gui calculator #5
This is how my interface look now and am actually pretty satisfied with it.
Looks much better .

[Image: calc.png]

Reply

RE: gui calculator #6
Done with my update now it does the calculation properly but still quite simple, in this version you can only use small operations with only one operator for example 4+2  4/2 100*56
in the next update i'll try to make it work for operations with many operators 32+2*3-2/5 for example !!
wish me luck guys and feel free to tell me if theres anything wrong with my code or if you need me to add something to make it better.

Code:
package calgui2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calc2 implements ActionListener {

   private JFrame f1;
   private JPanel pan1;
   private int temp, a, b;
   private JTextArea text;
   private JButton dot;
   private JButton zero;
   private JButton one;
   private JButton two;
   private JButton three;
   private JButton four;
   private JButton five;
   private JButton six;
   private JButton seven;
   private JButton eight;
   private JButton nine;
   private JButton modulo;
   private JButton division;
   private JButton multiply;
   private JButton minus;
   private JButton sum;
   private JButton clear;
   private JButton calculate;
   private final int width1 = 50;
   private final int height1 = 25;
   private final Dimension d1 = new Dimension(width1, height1);
   private final Dimension d2 = new Dimension(width1 * 2, height1 * 2);
   private JButton button[] = new JButton[11];
   private JButton operators[] = new JButton[5];
   private Object operator;

   public Calc2() {

       f1 = new JFrame("calculator");
       pan1 = new JPanel();
       text = new JTextArea(3, 20);
       seven = new JButton("7");
       seven.setSize(d1);
       eight = new JButton("8");
       eight.setSize(d1);
       nine = new JButton("9");
       nine.setSize(d1);
       division = new JButton("/");
       division.setSize(d1);
       clear = new JButton("C");
       clear.setSize(d2);
       four = new JButton("4");
       four.setSize(d1);
       five = new JButton("5");
       five.setSize(d1);
       six = new JButton("6");
       six.setSize(d1);
       multiply = new JButton("*");
       multiply.setSize(d1);
       one = new JButton("1");
       one.setSize(d1);
       two = new JButton("2");
       two.setSize(d1);
       three = new JButton("3");
       three.setSize(d1);
       minus = new JButton("-");
       minus.setSize(d1);
       calculate = new JButton("=");
       calculate.setSize(d2);
       zero = new JButton("0");
       zero.setSize(d1);
       dot = new JButton(".");
       dot.setSize(d1);
       modulo = new JButton("%");
       modulo.setSize(d1);
       sum = new JButton("+");
       sum.setSize(d1);

       button[0] = dot;
       button[1] = zero;
       button[2] = one;
       button[3] = two;
       button[4] = three;
       button[5] = four;
       button[6] = five;
       button[7] = six;
       button[8] = seven;
       button[9] = eight;
       button[10] = nine;

       operators[0] = modulo;
       operators[1] = sum;
       operators[2] = minus;
       operators[3] = multiply;
       operators[4] = division;

       pan1.setLayout(null);
       pan1.add(seven).setLocation(0, 0);
       pan1.add(eight).setLocation(50, 0);
       pan1.add(nine).setLocation(100, 0);
       pan1.add(division).setLocation(150, 0);
       pan1.add(clear).setLocation(200, 0);
       pan1.add(four).setLocation(0, 25);
       pan1.add(five).setLocation(50, 25);
       pan1.add(six).setLocation(100, 25);
       pan1.add(multiply).setLocation(150, 25);
       pan1.add(one).setLocation(0, 50);
       pan1.add(two).setLocation(50, 50);
       pan1.add(three).setLocation(100, 50);
       pan1.add(minus).setLocation(150, 50);
       pan1.add(calculate).setLocation(200, 50);
       pan1.add(zero).setLocation(0, 75);
       pan1.add(dot).setLocation(50, 75);
       pan1.add(modulo).setLocation(100, 75);
       pan1.add(sum).setLocation(150, 75);

       seven.addActionListener(this);
       eight.addActionListener(this);
       nine.addActionListener(this);
       division.addActionListener(this);
       clear.addActionListener(this);
       four.addActionListener(this);
       five.addActionListener(this);
       six.addActionListener(this);
       multiply.addActionListener(this);
       one.addActionListener(this);
       two.addActionListener(this);
       three.addActionListener(this);
       minus.addActionListener(this);
       calculate.addActionListener(this);
       zero.addActionListener(this);
       dot.addActionListener(this);
       modulo.addActionListener(this);
       sum.addActionListener(this);

       f1.setLayout(new BorderLayout());
       f1.add(text, BorderLayout.NORTH);
       f1.add(pan1, BorderLayout.CENTER);
       f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f1.setSize(302, 170);
       f1.setVisible(true);
   }

   public boolean isbutton(Object source) {
       boolean b = false;
       for (int i = 0; i < button.length; i++) {
           if (source == button[i]) {
               b = true;
               break;
           }

       }
       return b;
   }

   public boolean isoperator(Object source) {
       boolean b = false;
       for (int i = 0; i < operators.length; i++) {
           if (source == operators[i]) {
               b = true;
               break;
           }
       }
       return b;
   }

   public void sum(int a, int b) {
       text.setText(Integer.toString(a + b));
   }

   public void multiply(int a, int b) {
       text.setText(Integer.toString(a * b));
   }

   public void minus(int a, int b) {
       text.setText(Integer.toString(a - b));
   }

   public void division(int a, int b) {
       text.setText(Integer.toString(a / b));
   }

   public void modulo(int a, int b) {
       text.setText(Integer.toString(a % b));
   }

   @Override
   public void actionPerformed(ActionEvent e) {
       Object source = e.getSource();

       String n = e.getActionCommand();

       if (isbutton(e.getSource())) {

           text.setText(text.getText() + n);
       }
       if (isoperator(e.getSource())) {
           text.setText(text.getText() + n);
           a = Integer.parseInt(text.getText().substring(0, text.getText().length() - 1));
           temp = text.getText().length();
           operator = source;

       }

       if (source == clear) {
           text.setText(null);
           a = 0;
           b = 0;
       }
       if ((source == calculate) && (operator == sum)) {
           b = Integer.parseInt(text.getText().substring(temp, text.getText().length()));
           sum(a, b);

       }
       if ((source == calculate) && (operator == multiply)) {
           b = Integer.parseInt(text.getText().substring(temp, text.getText().length()));
           multiply(a, b);
       }
       if ((source == calculate) && (operator == minus)) {
           b = Integer.parseInt(text.getText().substring(temp, text.getText().length()));
           minus(a, b);
       }
       if ((source == calculate) && (operator == modulo)) {
           b = Integer.parseInt(text.getText().substring(temp, text.getText().length()));
           modulo(a, b);
       }
       if ((source == calculate) && (operator == division)) {
           b = Integer.parseInt(text.getText().substring(temp, text.getText().length()));
           division(a, b);
       }
   }

}
Code:
package calgui2;
public class CalGui2 {
public static void main(String[] args) {
   new Calc2();
   }
   
}
(This post was last modified: 10-25-2016, 02:13 PM by Vi-Sion.)

Reply

RE: gui calculator #7
A very good start man, keep up the good work! I use percentagecalculator.co.uk to work on my IT skills. Maybe give it a try. I used it as an example when creating my calculating software. But I ended up using this calculator for work because the one I created was to difficult to understand even for me. I added too many useless options that occupied the cache and the program itself stated to work slower. By the way this calculator is completely free and simple to use. I wish you good luck in your beginnings!
(This post was last modified: 06-02-2020, 12:11 PM by nieprophsono.)

Reply

RE: gui calculator #8
This Is actually well coded for a beginner, although after almost 4 years, I'm sure things have progressed.
[Image: AD83g1A.png]

Reply

RE: gui calculator #9
since youve only just started with gui i would suggest to use javafx not jswing. jswing is a bit old

Reply







Users browsing this thread: 1 Guest(s)