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





Messages In This Thread
gui calculator - by Vi-Sion - 10-23-2016, 08:44 PM
RE: gui calculator - by insidious - 10-24-2016, 04:37 AM
RE: gui calculator - by Vi-Sion - 10-24-2016, 11:41 AM
RE: gui calculator - by insidious - 10-24-2016, 03:36 PM
RE: gui calculator - by Vi-Sion - 10-24-2016, 08:29 PM
RE: gui calculator - by Vi-Sion - 10-25-2016, 02:11 PM
RE: gui calculator - by nieprophsono - 06-01-2020, 07:04 AM
RE: gui calculator - by mothered - 06-01-2020, 10:26 AM
RE: gui calculator - by kingzlatan - 09-03-2024, 08:26 PM



Users browsing this thread: 1 Guest(s)