Results 1 to 3 of 3
  1. #1
    Nawaz is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default How to enter data using buttons in a text field like " 1+3 " like calculator

    there is code below i want to enter data in a textfield like 1+4 using buttons......calculator application plz help


    import com.sun.lwuit.Button;
    import com.sun.lwuit.Container;
    import com.sun.lwuit.Display;
    import com.sun.lwuit.Form;
    import com.sun.lwuit.Label;
    import com.sun.lwuit.TextField;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.geom.Dimension;
    import com.sun.lwuit.layouts.GridLayout;
    import com.sun.lwuit.layouts.Layout;
    import javax.microedition.midlet.MIDlet;

    /**
    * @author Nawaz Baig
    */
    public class Midlet extends MIDlet implements ActionListener {

    Button b1;
    Button b2;
    Button b3;
    Button b4;
    Button b5;
    Button b6;
    Button b7;
    Button b8;
    Button b9;

    Button b10;

    Button b11;

    Button b12;

    Button b13;

    TextField my;
    Layout l;
    public void startApp() {
    Display.init(this);
    Form mainForm = new Form("Telculator");


    my = new TextField("");

    mainForm.setLayout(new GridLayout(3,3));
    b1= new Button("0");
    b2= new Button("1");
    b3= new Button("2");
    b4= new Button("3");
    b5= new Button("4");
    b6= new Button("5");
    b7= new Button("6");
    b8= new Button("7");
    b9= new Button("8");
    b10= new Button("9");
    b11=new Button("+");
    b12=new Button("=");
    b13 = new Button("Clear");



    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    b10.addActionListener(this);
    b11.addActionListener(this);
    b12.addActionListener(this);
    b13.addActionListener(this);

    mainForm.addComponent(my);
    mainForm.addComponent(b1);
    mainForm.addComponent(b2);
    mainForm.addComponent(b3);
    mainForm.addComponent(b4);
    mainForm.addComponent(b5);
    mainForm.addComponent(b6);
    mainForm.addComponent(b7);
    mainForm.addComponent(b8);
    mainForm.addComponent(b9);
    mainForm.addComponent(b10);
    mainForm.addComponent(b11);
    mainForm.addComponent(b12);
    mainForm.addComponent(b13);
    mainForm.show();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void actionPerformed(ActionEvent ae) {

    if(ae.getSource()==b1)
    {
    my.setT9Text("0");
    }
    if(ae.getSource()==b2)
    {
    my.setText("1");

    }
    if(ae.getSource()==b3)
    {
    my.setText("2");

    }
    if(ae.getSource()==b4)
    {
    my.setText("3");
    }
    if(ae.getSource()==b5)
    {
    my.setText("4");
    }

    if(ae.getSource()==b6)
    {
    my.setText("5");
    }

    if(ae.getSource()==b7)
    {
    my.setText("6");
    }

    if(ae.getSource()==b8)
    {
    my.setText("7");


    }


    if(ae.getSource()==b9)
    {
    my.setText("8");

    }

    if(ae.getSource()==b10)
    {
    my.setText("9");

    }


    if(ae.getSource()==b11)
    {
    my.setText("+");
    }

    if(ae.getSource()==b12)
    {
    int i = my.getText().indexOf("+");
    String before =my.getText().substring(0, i);
    String after =my.getText().substring(i,+1);


    my.setText(Integer.parseInt(before)+Integer.parseI nt(after)+"" );
    }
    if(ae.getSource()==b13)
    {
    my.clear();
    }


    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to enter data using buttons in a text field like " 1+3 " like calculator

    Please go through BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Nawaz is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: How to enter data using buttons in a text field like " 1+3 " like calculator

    there is code below i want to enter data in a textfield like 1+4 using buttons......calculator application in lwuit plz help
    Java Code:
    import com.sun.lwuit.Button;
     import com.sun.lwuit.Container;
     import com.sun.lwuit.Display;
     import com.sun.lwuit.Form;
     import com.sun.lwuit.Label;
     import com.sun.lwuit.TextField;
     import com.sun.lwuit.events.ActionEvent;
     import com.sun.lwuit.events.ActionListener;
     import com.sun.lwuit.geom.Dimension;
     import com.sun.lwuit.layouts.GridLayout;
     import com.sun.lwuit.layouts.Layout;
     import javax.microedition.midlet.MIDlet;
     
    /**
     * @author Nawaz Baig
     */
     public class Midlet extends MIDlet implements ActionListener {
     
    Button b1;
     Button b2;
     Button b3;
     Button b4;
     Button b5;
     Button b6;
     Button b7;
     Button b8;
     Button b9;
     
    Button b10;
     
    Button b11;
     
    Button b12;
     
    Button b13;
     
    TextField my;
     Layout l;
     public void startApp() {
     Display.init(this);
     Form mainForm = new Form("Telculator");
     
    
    my = new TextField("");
     
    mainForm.setLayout(new GridLayout(3,3));
     b1= new Button("0");
     b2= new Button("1");
     b3= new Button("2");
     b4= new Button("3");
     b5= new Button("4");
     b6= new Button("5");
     b7= new Button("6");
     b8= new Button("7");
     b9= new Button("8");
     b10= new Button("9");
     b11=new Button("+");
     b12=new Button("=");
     b13 = new Button("Clear");
     
    
    
    b1.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     b4.addActionListener(this);
     b5.addActionListener(this);
     b6.addActionListener(this);
     b7.addActionListener(this);
     b8.addActionListener(this);
     b9.addActionListener(this);
     b10.addActionListener(this);
     b11.addActionListener(this);
     b12.addActionListener(this);
     b13.addActionListener(this);
     
    mainForm.addComponent(my);
     mainForm.addComponent(b1);
     mainForm.addComponent(b2);
     mainForm.addComponent(b3);
     mainForm.addComponent(b4);
     mainForm.addComponent(b5);
     mainForm.addComponent(b6);
     mainForm.addComponent(b7);
     mainForm.addComponent(b8);
     mainForm.addComponent(b9);
     mainForm.addComponent(b10);
     mainForm.addComponent(b11);
     mainForm.addComponent(b12);
     mainForm.addComponent(b13);
     mainForm.show();
     }
     
    public void pauseApp() {
     }
     
    public void destroyApp(boolean unconditional) {
     }
     
    public void actionPerformed(ActionEvent ae) {
     
    if(ae.getSource()==b1)
     {
     my.setT9Text("0");
     }
     if(ae.getSource()==b2)
     {
     my.setText("1");
     
    }
     if(ae.getSource()==b3)
     {
     my.setText("2");
     
    }
     if(ae.getSource()==b4)
     {
     my.setText("3");
     }
     if(ae.getSource()==b5)
     {
     my.setText("4");
     }
     
    if(ae.getSource()==b6)
     {
     my.setText("5");
     }
     
    if(ae.getSource()==b7)
     {
     my.setText("6");
     }
     
    if(ae.getSource()==b8)
     {
     my.setText("7");
     
    
    }
     
    
    if(ae.getSource()==b9)
     {
     my.setText("8");
     
    }
     
    if(ae.getSource()==b10)
     {
     my.setText("9");
     
    }
     
    
    if(ae.getSource()==b11)
     {
     my.setText("+");
     }
     
    if(ae.getSource()==b12)
     {
     int i = my.getText().indexOf("+");
     String before =my.getText().substring(0, i);
     String after =my.getText().substring(i,+1);
     
    
    my.setText(Integer.parseInt(before)+Integer.parseI nt(after)+"" );
     }
     if(ae.getSource()==b13)
     {
     my.clear();
     }
     
    
    }
     }

Similar Threads

  1. Replies: 7
    Last Post: 04-05-2012, 07:15 AM
  2. Simple "Refresh" and "Forward" buttons
    By needSolution in forum AWT / Swing
    Replies: 3
    Last Post: 12-12-2011, 02:15 AM
  3. The field "name" doesnt apparently exsist :/
    By Addez in forum New To Java
    Replies: 6
    Last Post: 10-23-2010, 12:15 AM
  4. output "left click"/enter command?
    By Arsenic in forum New To Java
    Replies: 2
    Last Post: 06-04-2009, 06:01 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •