Codename One:实现流体跟踪器的计算

时间:2016-03-15 03:08:33

标签: java codenameone

我正在尝试实施经常性的加法计算。这应该获取当前数量(来自数量文本字段)和保存的数量(要保存的总数量),输出“结果”标题下方的总结果。 View of tabbed page for calculation.

我尝试过以下代码,但它包含错误并使用Dialog输出结果(这不是我想要的)。

addItem.addComponent(selectItem);
    TextField quantity = new TextField("", "Quantity (ml or g)", 4, TextArea.NUMERIC);
    addItem.addComponent(quantity);
    Button add = new Button("Add");
    addItem.addComponent(add);       
    TextArea results = new TextArea("Results");
    addItem.addComponent(results);
    //TextArea total = new TextArea("Add Item");
    //addItem.addComponent(total);

//--------------------------------------------------------------
    add.addActionListener((ActionEvent ev) -> {
        Storage s = Storage.getInstance();
Integer addition = 0;
// Read my "Hello World" string back from storage
addition = (Integer)s.readObject("total");
int d = Int.parseInt(quantity.getText());
Integer total = addition + quantity;
// Save the "Hello World" string to storage
s.writeObject("total", total);
Dialog.show("", total, "OK", "Cancel");
    });
//--------------------------------------------------------------

    return addItem;

因此,我很感激有关如何在我的代码中实现这一点的任何建议和指导。

1 个答案:

答案 0 :(得分:0)

要将其呈现在表格中,您可以通过以下方式轻松使用GridLayout:

Container ctrResultTable = new Container(new GridLayout(1,3)); 
//Define a new container associated to a gridlayout with 1 row and 1 column
ctrResult.addComponent(new Label(d.toString()));
ctrResult.addComponent(new Label(total.toString()));
ctrResult.addComponent(new Label((total/target*100).toString()));
//Because you use a value inside a function, addItem should be initialized as final
addItem.addComponent(ctrResult);

但我无法解决您的代码错误,因为我无法在此处查看完整代码