Results 1 to 3 of 3
- 09-29-2012, 09:14 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
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();
}
}
}
- 09-29-2012, 09:21 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-29-2012, 10:25 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
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
-
Trouble getting program to recognize when the "ENTER" key is selected.
By okletsgo in forum New To JavaReplies: 7Last Post: 04-05-2012, 06:15 AM -
Simple "Refresh" and "Forward" buttons
By needSolution in forum AWT / SwingReplies: 3Last Post: 12-12-2011, 01:15 AM -
The field "name" doesnt apparently exsist :/
By Addez in forum New To JavaReplies: 6Last Post: 10-22-2010, 11:15 PM -
output "left click"/enter command?
By Arsenic in forum New To JavaReplies: 2Last Post: 06-04-2009, 05:01 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks