Hi all
Can anyone help me in making the code in the comment area work related to lwuit...............................
The code is as follows
public class CalForm extends Form implements ActionListener
{
private TextField display = new TextField("");
protected MIDlet parentMidlet;
private String buttonText = "C/*-+9876543210.=";
private double result = 0;
private String operator = "=";
private boolean calculating = true;
protected String operation = "+-/*";
protected Button b;
protected Command exit;
protected Float res;
protected String valone,valtwo;
public CalForm(MIDlet parent)
{
super();
parentMidlet = parent;
init();
}
public void init()
{
getStyle().setBgColor(0x2f2f32);
setLayout(new BorderLayout());
exit = new Command("Exit");
addCommand(exit);
addCommandListener(this);
display.setEditable(false);
display.setSingleLineTextArea(true);
display.setConstraint(TextField.UNEDITABLE);
addComponent(BorderLayout.NORTH,display);
Container p = new Container();
p.setLayout(new GridLayout(4, 4));
for (int i = 0; i < buttonText.length(); i++)
{
b = new Button(buttonText.substring(i, i + 1));
b.getStyle().setBgColor(0x25f5644);
p.addComponent(b);
b.addActionListener(this);
}
addComponent(BorderLayout.CENTER,p);
}
public void actionPerformed(ActionEvent ae)
{
String Clear = ("C");
if(ae.getSource() instanceof Command)
{
if(ae.getSource()== exit)
{
parentMidlet.notifyDestroyed();
}
}
if(ae.getSource() instanceof Button)
{
Button btn = (Button) ae.getSource();
if (buttonText.indexOf(btn.getText()) > -1)
{
display.setText(display.getText() + btn.getText());
}
if (Clear.indexOf(btn.getText()) > -1)
{
resetCalculator();
}
}
// String cmd = (String) ae.getSource();
//
// if ('0' <= cmd.charAt(0) && cmd.charAt(0) <= '9' || cmd.equals("."))
// {
// if (calculating)
// {
// display.setText(cmd);
// }
// else
// {
// display.setText(display.getText() + cmd);
// }
// calculating = false;
// }
// else
// {
// if (calculating)
// {
// if (cmd.equals("-"))
// {
// display.setText(cmd);
// calculating = false;
// }
// else
// operator = cmd;
// }
// else
// {
// double x = Double.parseDouble(display.getText());
// calculate(x);
// operator = cmd;
// calculating = true;
// }
// }
// }
// private void calculate(double n)
// {
// if (operator.equals("+"))
// {
// result += n;
// }
//
// else if (operator.equals("-"))
// {
// result -= n;
// }
//
// else if (operator.equals("*"))
// {
// result *= n;
// }
// else if (operator.equals("/"))
// {
// result /= n;
// }
// else if (operator.equals("="))
// {
// result = n;
// }
// display.setText("" + result);
}
private void resetCalculator()
{
display.setText("");
}
}
Thank you

Reply With Quote




