2016-03-15 66 views
1

我正在尝试执行反复加法计算。这应该从“数量”文本框中获取当前数量和保存的数量(要保存的总数量),并在“结果”标题下输出总计结果。 View of tabbed page for calculation.代号1:为流体跟踪器执行计算

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

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; 

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

回答

0

以目前它在一个表,你可以用这种方式很容易地使用网格布局:

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); 

但我不能为你的代码错误什么也不做,因为我不能看到完整的代码在这里