Monday, June 27, 2011

Mini Calculator : A Sample Java Swing Application


//   A Sample Java Swing Application

import java.awt.Window;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.CardLayout;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Calc extends JFrame implements ActionListener
{

        JButton addBut, subBut, mulBut, divBut, setBut, extBut;

        JTextField txt1, txt2, txt3;

        JPanel pan;

        public Calc()
{

super("Sample Frame");

                setBounds(100,100,300,300);
                centerWindow(this);
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                this.add(mainPanel());
}

        public JPanel mainPanel()
        {

                pan = new JPanel();
                pan.setLayout(new GridLayout(6, 2));

                pan.add(new JLabel("Enter a value : ", SwingConstants.CENTER));

                txt1 = new JTextField();
                txt1.requestFocusInWindow();
                pan.add(txt1);
                txt1.addKeyListener(new MyKeyListener());

                pan.add(new JLabel("Enter another value : ", SwingConstants.CENTER));

                txt2 = new JTextField();
                pan.add(txt2);
                txt2.addKeyListener(new MyKeyListener());

                pan.add(new JLabel("Result : ", SwingConstants.CENTER));
                txt3 = new JTextField();
                txt3.setEditable(false);
                pan.add(txt3);

                addBut = new JButton("Add");
                pan.add(addBut);
                addBut.addActionListener(this);

                subBut = new JButton("Subtract");
                pan.add(subBut);
                subBut.addActionListener(this);

                mulBut = new JButton("Multiply");
                pan.add(mulBut);
                mulBut.addActionListener(this);

                divBut = new JButton("Divide");
                pan.add(divBut);
                divBut.addActionListener(this);

                setBut = new JButton("Reset");
                pan.add(setBut);
                setBut.addActionListener(this);

                extBut = new JButton("Exit");
                pan.add(extBut);
                extBut.addActionListener(this);

                return pan;
        }

        class MyKeyListener extends KeyAdapter
        {
                public void keyTyped(KeyEvent ke)
                {
                        char ch = ke.getKeyChar();

                        if(ch!='0' && ch!='1' && ch!='2' && ch!='3' &&
                           ch!='4' && ch!='5' && ch!='6' && ch!='7' &&
                           ch!='8' && ch!='9' && ch!='.' && ch!='-')

                           ke.consume();

                }
        }

        public void actionPerformed(ActionEvent ae)
        {

                double val1 = 0.0, val2 = 0.0;

                try
                {

                        Object source = ae.getSource();

                        if(source == extBut)
                        {

                                System.exit(0);
                        }
                        else if(source == setBut)
                        {

                                txt1.setText("");
                                txt2.setText("");
                                txt3.setText("");
                        }
                        else
                        {
                                String  str1 = txt1.getText().trim(),
                                        str2 = txt2.getText().trim();

                                if( isPresent(str1) && isPresent(str2) )
                                {

                                        val1 = Double.parseDouble(str1);
                                        val2 = Double.parseDouble(str2);

                                        if(source == addBut)
                                        {

                                                txt3.setText(String.valueOf(val1 + val2));
                                        }
                                        else if(source == subBut)
                                        {

                                                txt3.setText(String.valueOf(val1 - val2));
                                        }
                                        else if(source == mulBut)
                                        {

                                                txt3.setText(String.valueOf(val1 * val2));
                                        }
                                        else if(source == divBut)
                                        {

                                                txt3.setText(String.valueOf(val1 / val2));
                                        }
                                }
                                else
                                {
                                        displayMessage("Please Enter the Values");
                                }
                        }

                }
                catch(NumberFormatException nfe)
                {

                        displayMessage("Please Enter Valid Input Values");
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }

                txt1.requestFocusInWindow();
        }

     

        public void displayMessage(String message)
        {

                JOptionPane.showMessageDialog(this, message,
                  "Request for Input", JOptionPane.INFORMATION_MESSAGE);
        }

        public boolean isPresent(String str)
        {
                if(str.length() == 0)
                        return false;
                else
                        return true;
        }

        public void centerWindow(Window w)
        {
                Toolkit tk = Toolkit.getDefaultToolkit();
                Dimension d = tk.getScreenSize();
                setLocation( (d.width - w.getWidth())/2,
                                (d.height - w.getHeight())/2 );
        }

public static void main(String[] args)
{
                new Calc().setVisible(true);
}
}