2013-06-25 43 views
3

這是一個非常簡單的代碼,其中按鈕上的文本被複制到TextField。 代碼可以正常工作,但TextField在單擊按鈕時不會立即更新。 只有當我點擊TextField時,或者當我不按即時拖動窗體時,它纔會更新。 爲什麼會發生這種情況,這種行爲是意想不到的。 我在支持LWUIT的諾基亞501模擬器上測試此代碼。TextField未動態更新

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

嗯......現在該框架是什麼呢?看起來至少與Swing無關(Button是桌面環境下的AWT組件) – kleopatra

+0

其LWUIT 它與鞦韆非常相似,這就是爲什麼我添加了鞦韆標籤 – user2497398

回答

0

發生這種情況是因爲你的代碼。 我解釋:

你調用函數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(); 
       } 
3

設置在文本框的文本後。這可能工作

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

像魅力一樣工作 感謝之人 – user2497398

+0

僅供參考僅需要驗證。它不是一個佈局問題的繪畫問題,文本領域需要增長。您還應該接受權威人士的回答,並最好將其投票。 –

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