25, మార్చి 2012, ఆదివారం

CODE FOR THE SIMPLE CALCULATOR IN JAVA A SAMPLE APPLICATION FOR SOFTWARE DEVOLVEMENT IN JAVA



//For any queries u can reach me at      d_ramu2002@yahoo.com
import java.awt.*;               
import java.awt.event.*;
// class-1 my own adapter 
class w extends WindowAdapter
{public void windowClosing(WindowEvent we){
System.exit(0);
}}
//CLASS-2 LOGIN CLASS 
       class login extends Frame implements ActionListener
              {
                     Label un,pwd;
              TextField tfun,tfpwd;
              Button ok,cancel;
login()
{
setLayout(null);
addWindowListener(new w());
un=new Label("username",Label.LEFT);
pwd=new Label("password",Label.LEFT);
un.setBounds(400,100,120,20);
pwd.setBounds(400,120,120,20);
add(un);
add(pwd);
tfun=new TextField();
tfpwd=new TextField();
tfun.setBounds(600,100,100,20);
tfpwd.setBounds(600,120,100,20);
add(tfun);
add(tfpwd);
ok=new Button("ok");
cancel=new Button("cancel");
ok.setBounds(400,180,40,20);
cancel.setBounds(600,180,40,20);
add(ok);
ok.addActionListener(this);
add(cancel);
ok.addActionListener(this);
cancel.addActionListener(this);
setBackground(Color.green);
setForeground(Color.red);
              }
       public void paint(Graphics g)
{
g.drawString("THIS THE LOGIN PAGE",400,60);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();


                if(s.equals("ok"))
                {
                String s1=tfun.getText();
                String s2=tfpwd.getText();
                      System.out.println("   "+s1+" \n  "+s2); 
                        if((s1.equals("ramu"))&&(s2.equals("dayina")))
                                      {  
                                    compute c=new compute();
                                    c.setBounds(0,0,640,400);
                                    c.setVisible(true);
                                    c.setTitle("CALCULATOR");
                                    c.setBackground(Color.yellow);
                                    c.setForeground(Color.pink);
                                    dispose();
                                        }
                                        }
else if(s.equals("cancel"))
System.exit(0);
}
}
//CLASS-3 computational code
class compute extends Frame implements ActionListener
{
Label n1,n2,op,opr;
TextField tfn1,tfn2,tfop;
Choice copr;
Button submit,cancel,exit;
        compute()
        {
        setLayout(null);
        addWindowListener(new w());
        n1=new Label("number1",Label.LEFT);
        n2=new Label("number1",Label.LEFT);
        op=new Label("output",Label.LEFT);
        opr=new Label("operation",Label.LEFT);
        n1.setBounds(100,200,60,10);
        n2.setBounds(100,220,60,10);
        op.setBounds(400,200,60,10);
        opr.setBounds(300,400,80,10);
        add(n1);
        add(n2);
        add(op);
        add(opr);

             tfn1=new TextField();
             tfn2=new TextField();
             tfop=new TextField();
               tfn1.setBounds(160,200,40,20);
               tfn2.setBounds(160,220,40,20);
               tfop.setBounds(380,200,100,20);
                 add(tfn1);
                 add(tfn2);
                 add(tfop);
         copr=new Choice();
         copr.addItem("add");
         copr.addItem("sub");
         copr.addItem("mul");
         copr.addItem("div");
          copr.setBounds(380,400,60,20);
          add(copr);

      submit=new Button("submit");
      cancel=new Button("cancel");
      exit=new Button("exit");
                    submit.setBounds(400,300,50,20);
                    cancel.setBounds(500,300,50,20);
                    exit.setBounds(600,300,50,20);
                        add(submit);
                        add(cancel);
                        add(exit);
       submit.addActionListener(this);
        cancel.addActionListener(this);
        exit.addActionListener(this);
        }


        public void actionPerformed(ActionEvent ae)
     {
              String s=ae.getActionCommand();
                if(s.equals("submit"))

                           {
                           int n1=Integer.parseInt(tfn1.getText());
                           int n2=Integer.parseInt(tfn2.getText());
                               int res=0;
                            String si=copr.getSelectedItem();
                            if(si.equals("add"))
                                    res=(n1+n2);
                             else
                            if(si.equals("sub"))
                                res=(n1-n2);
                            else
                            if(si.equals("mul"))
                                res=(n1*n2);
                            else
                            if(si.equals("div"))
                                res=(int)n1/n2;

                            tfop.setText(""+res);}
                      else
                            if(s.equals("cancel"))
                            {
                                 tfn1.setText("");
                                 tfn2.setText("");
                                 tfop.setText("");
                             }
                             else
                             if(s.equals("exit"))
                                     System.exit(0);
                                  else
                                  System.out.println("");

                                                 }}
//class-4 main controller
class cal extends Frame
{
public static void main(String s[])
{
login l=new login();
l.setVisible(true);
l.setBounds(400,220,40,20);
l.setTitle("This is the Login page ");
}}

Class-1 my own adapter class (for adding window methods to my application i.e. exit minimize and so on)
Class-2 login class (user interface implementation PL)
Class-3 computational code (B.L)
Class-4 main class which assembles the above all classes

* the above code explains u how to create multiple frames in your project and as well as calling one frame from another (i.e communication between frames , data passing from one frame to another
OUTPUT:-

E:\>javac cal.java
E:\>java cal
   ramu
  dayina
  

 NOTE:- in the above code while adding the components to the frame study the locations to be appeared on the frame then set bound to the each components 

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి