You are on page 1of 13

//CALCULATOR

package hello;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
public class HelloMIDlet extends MIDlet implements CommandListener
{
private Form form;
private Display display;
private TextField input1, input2,res;
private Command add, sub, mul,div;
private StringItem item;
public HelloMIDlet()
{
}
public void startApp()
{
display = Display.getDisplay(this);
final Form form = new Form("Calculator");
item = new StringItem("Result", "");
input1 = new TextField("First Number", "", 30, TextField.NUMERIC);
input2 = new TextField("Second Number", "", 30, TextField.NUMERIC);
res= new TextField("Result", "", 30, TextField.ANY);
form.append(input1);
form.append(input2);
form.append(res);

add = new Command("Add", Command.OK, 1);


sub = new Command("Sub", Command.OK, 1);
mul = new Command("Mul", Command.OK, 1);
div = new Command("Div", Command.OK, 1);
form.addCommand(add);
form.addCommand(sub);
form.addCommand(mul);
form.addCommand(div);
//form.append(item);
final StringItem add = new StringItem("", "Add", Item.BUTTON);
Command addCommand = new Command("Add command", Command.ITEM, 1);
add.setDefaultCommand(addCommand);
add.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
calculate();
}
});
form.append(add);
final StringItem sub = new StringItem("", "Sub", Item.BUTTON);
Command subCommand = new Command("Sub command", Command.ITEM, 1);
sub.setDefaultCommand(subCommand);
sub.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
calculate1();
}
});

form.append(sub);
final StringItem mul = new StringItem("", "Mul", Item.BUTTON);
Command mulCommand = new Command("Mul command", Command.ITEM, 1);
mul.setDefaultCommand(mulCommand);
mul.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
calculate2();
}
});
form.append(mul);
final StringItem div = new StringItem("", "Div", Item.BUTTON);
Command divCommand = new Command("Div command", Command.ITEM, 1);
div.setDefaultCommand(divCommand);
div.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
calculate3();
}
});
form.append(div);
final StringItem button = new StringItem("", "Clear", Item.BUTTON);
Command buttonCommand = new Command("Button command", Command.ITEM,
1);
button.setDefaultCommand(buttonCommand);
button.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
input1.setString("");
input2.setString("");

res.setString("");
}
});
form.append(button);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean uncondn)
{
notifyDestroyed();
}
private void calculate()
{int one=Integer.parseInt(input1.getString());
int two= Integer.parseInt(input2.getString());
int result=one+two;
//item.setText( result + "" );
res.setString(Integer.toString(result));
}
private void calculate1()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one - two;
//item.setText(result + "");
res.setString(Integer.toString(result));

}
private void calculate2()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one * two;
//item.setText(result + "");
res.setString(Integer.toString(result));
}
private void calculate3()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
try
{
int result = one / two;
//item.setText(result + "");
res.setString(Integer.toString(result));
}
catch(ArithmeticException e)
{
res.setString("Error");
}
}
public void commandAction(Command c, Displayable d)
{

String label = c.getLabel();


if (label.equals("Add"))
{
calculate();
}
else if (label.equals("Sub"))
{
calculate1();
}
else if (label.equals("Mul"))
{
calculate2();
form.append("The Answer is:");
}
else if (label.equals("Div"))
{
calculate3();
form.append("The Answer is:");
}
}}
OUTPUT:

//CALENDER:
package hello;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import java.util.TimeZone;

public class HelloMIDlet extends MIDlet{


private Form form;
private Display display;
private DateField calender;
private static final int DATE = 0;
public HelloMIDlet(){
calender = new DateField("Date In:", DateField.DATE,
TimeZone.getTimeZone("GMT"));
}
public void startApp(){
display = Display.getDisplay(this);
Form form = new Form("Calender");
form.append(calender);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean destroy){
notifyDestroyed();
}
}
OUTPUT:

//DISPLAY TEXT WITH IMAGE:


package hello;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class HelloMIDlet extends MIDlet implements CommandListener{
private Command exit;
private ImageItem imageItem;
private Image image;
private Display display;
private Form form;
StringItem txtbut = new StringItem("", "Text", Item.BUTTON);
StringItem button,txtbutton ;
public HelloMIDlet(){
//path = "file:///C:/Users/student.admin-PC/Downloads/qqqp.png";
}

public void startApp(){


form = new Form("Image");
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
button = new StringItem("", "Image", Item.BUTTON);
Command buttonCommand = new Command("Button command", Command.ITEM,
1);
button.setDefaultCommand(buttonCommand);
button.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
//System.out.println("Button was clicked");
try{
image = Image.createImage("/images/PNG_transparency_demonstration_1.png");

} catch (Exception e){ }


imageItem = new ImageItem("",image, ImageItem.LAYOUT_SHRINK, "image");
form.deleteAll();
form.append(image);
//form.append(imgbut);
form.append(txtbutton);
form.append(button);}
});
txtbutton = new StringItem("", "Text", Item.BUTTON);
Command txtCommand = new Command("Button command", Command.ITEM, 1);
txtbutton.setDefaultCommand(txtCommand);
txtbutton.setItemCommandListener(new ItemCommandListener() {
public void commandAction(Command c, Item item) {
StringItem imgbut = new StringItem("", "Welcome", Item.BUTTON);
form.deleteAll();
form.append(imgbut);
form.append(txtbutton);
form.append(button);
}
});
form.append(txtbutton);
form.append(button);
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp(){}


public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void commandAction(Command c, Displayable d){
String label = c.getLabel();
if(label.equals("Exit")){
destroyApp(true);
}
}
}

OUTPUT:

You might also like