[英]TextField not updating dynamically
这是一个非常简单的代码,其中按钮上的文本被复制到TextField。 代码工作正常,但单击按钮时TextField不会立即更新。 它仅在我单击TextField后或当我拖动表单而不是立即按下按钮时更新。 为什么会发生这种情况,这种行为是出乎意料的。 我正在支持LWUIT的诺基亚501仿真器上测试此代码。
a = new Form("CALCULATOR")
final TextArea data = new TextArea();
final Button ab = new Button("Some Value");
ab.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
data.setText(ab.getText());
}
});
a.addComponent(ab);
a.addComponent(data);
a.show();
}
在文本字段中设置文本后重新绘制它。 这可能有效
data.setText(ab.getText());
data.validate(); or data.repaint();
这是因为你的代码。 我解释:
你调用函数actionPerformed:这是lisitner,当用户执行“我单击TextField后...”之后的操作时调用。
你需要做的很简单:
a = new Form("CALCULATOR")
final TextArea data = new TextArea();
final Button ab = new Button("Some Value");
ab.addActionListener(new ActionListener(){
data.setText(ab.getText());
a.addComponent(ab);
a.addComponent(data);
a.show();
}
a = new Form("CALCULATOR")
final TextArea data = new TextArea();
final Button ab = new Button("Some Value");
ab.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
// data.setText(ab.getText()); // You should not use this
// Use this instead
data.setText(ab.getActionCommand());
}
});
a.addComponent(ab);
a.addComponent(data);
a.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.