java TextField 不动态更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17305584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:38:44  来源:igfitidea点击:

TextField not updating dynamically

javaswingjava-melwuit

提问by user2497398

this is a really simple code in which text on a button is copied to a TextField. The code works fine but the TextField is not updating instantly on clicking the button. It updates only after i click on the TextField or when i drag the form not on pressing the button instantly. why is this happening,this behavior is unexpected. i am testing this code on a nokia 501 emulator which supports LWUIT.

这是一个非常简单的代码,其中将按钮上的文本复制到 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();
                   }

回答by pundit

After setting the text in the textfield repaint it. This may work

在文本字段中设置文本后重新绘制它。这可能有效

  data.setText(ab.getText());
  data.validate(); or data.repaint();

回答by neb1

That happen because your Code. I explain:

那是因为你的代码。我解释:

you call to function actionPerformed: this is lisitner that call when user make action like "after i click on the TextField..".

您调用 actionPerformed 函数:这是当用户执行诸如“在我单击 TextField 之后..”之类的操作时调用的 lisitner。

what you need to do is simple:

你需要做的很简单:

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

回答by smk pobon

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