Thursday, May 23, 2013

alculator using swing and also add image on button



Aim: An application for calculator using swing and also add image on button.
           
package bc;
public class SwingCal extends javax.swing.JApplet
{
                        public SwingCal()
            {
            }
            private String str1,str2;
            private  double a,b,ans;
            char choice;
            public void SwingCal()
            {
                        initComponents();
            }
            private void actionf(String s)
            {
                        str1=str2="";
                        str1=s;
                        str2=text1.getText();
                        if(str2.equals("0") || str2.equals("") || str2==null)
                        {
                                                text1.setText(str1);
                        }
                        text1.setText(str2+str1);
            }
            private void optn(char op)
            {
                        if(op!= '=')
                        {
                                    choice=op;
                                    a=Double.parseDouble(text1.getText().toString());
                                    text1.setText("");
                        }
                        else
                        {
                                    b=Double.parseDouble(text1.getText().toString());
                                    switch(choice)
                                    {         
                                                            case '+':
                                                            ans=a+b;
                                                            break;

                                                            case '-':
                                                            ans=a-b;
                                                            break;

                                                            case '*':
                                                            ans=a*b;
                                                            break;

                                                            case '/':
                                                            ans=a/b;
                                                            break;
                                    }
                                    text1.setText(String.valueOf(ans));
                         }
            }
            public void init()
            {         
                        setSize(215,350);
                        try
{
                                                java.awt.EventQueue.invokeAndWait(new Runnable()
                                                {
                                                public void run()
                                                {
                                                            initComponents();
                                                }
                                                });
                        }
                        catch (Exception ex)
                        {
                                                ex.printStackTrace();
                        }
            }
             private void jButton19ActionPerformed(java.awt.event.ActionEvent evt)
{
                        actionf("0");
            }
                        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{                                                              
                        actionf("1");
} 
            private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{                                        
                        actionf("2");
} 
            private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{                                        
                        actionf("3");
}                                                                                                                   
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{                                        
                        actionf("4");
}    
            private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
                        {                                        
                        actionf("5");
}    
            private void jButton6ActionPerformed(java.awt.event.ActionEvent evt)
{                                        
                        actionf("6");
}         
            private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)
{                                        
                        actionf("7");
}  
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)
{                                        
        actionf("8");
}        
            private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
                        actionf("9");
}                                                                                                                                                                           
             private void jButton17ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
                        optn('-');
}                                        
            private void jButton16ActionPerformed(java.awt.event.ActionEvent evt)
{                                          
                        text1.setText("");
}                                        
            private void jButton15ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
                        optn('=');
}                                         
            private void jButton20ActionPerformed(java.awt.event.ActionEvent evt)
{
                        optn('/');
            }
            private void jButton13ActionPerformed(java.awt.event.ActionEvent evt)
{                                          
                        actionf(".");
}                                        
            private void jButton12ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
                        optn('+');
}                                         
            private void jButton11ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
                        optn('*');
}                                        

Output:

No comments:

Post a Comment