2012-07-05 24 views
0

嗨,我已經實現了一個簡單的focusListener方法並註冊了兩個jTextFields。這種方法所做的是在它們中添加數字並將其顯示在JLabel中。如果我在其中鍵入了「2」,它會正確更新以給我4.但是,如果我之後刪除了2,即使我點擊其他地方,也不會激發focusLost事件。如果我在JTextField中輸入0,那麼focusLost事件將正常發生。爲什麼?謝謝!當我刪除Jtextfield中的文本時,lostFocusEvent不會發生?

+0

你需要添加jTextField.getDocument()addDocumentListener(新的DocumentListener(){...})。 –

+0

@李光華你好我已經試過了。然而,即使在我輸入文字時,它也會觸發事件。我怎樣才能讓事件在用戶完成將文本輸入到jtextfield後觸發?(無論是否爲空字段) – Laughy

回答

3

如果沒有SSCCE,很難說您在案例中使用的邏輯。由於在這個例子如下,它的工作,你期待:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class TextFieldExample 
{ 
    private JTextField tfield1; 
    private JTextField tfield2; 
    private JLabel label; 
    private FocusListener tfieldListener = new FocusListener() 
    { 
     @Override 
     public void focusGained(FocusEvent fe) 
     { 
     } 

     @Override 
     public void focusLost(FocusEvent fe) 
     { 
      String num1 = tfield1.getText().trim(); 
      String num2 = tfield2.getText().trim(); 
      if (num1 == null || num1.equals("")) 
       num1 = "0"; 
      if (num2 == null || num2.equals("")) 
       num2 = "0";   
      label.setText(Integer.toString(Integer.parseInt(num1) + Integer.parseInt(num2))); 
     } 
    }; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Text Field Focus Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 

     tfield1 = new JTextField(10); 
     tfield2 = new JTextField(10); 

     tfield1.addFocusListener(tfieldListener); 
     tfield2.addFocusListener(tfieldListener); 

     ((AbstractDocument)tfield1.getDocument()).setDocumentFilter(new MyDocumentFilter()); 
     ((AbstractDocument)tfield2.getDocument()).setDocumentFilter(new MyDocumentFilter()); 

     label = new JLabel("SUM IS"); 

     contentPane.add(tfield1); 
     contentPane.add(tfield2); 
     contentPane.add(label); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    class MyDocumentFilter extends DocumentFilter 
    { 
     @Override 
     public void insertString(FilterBypass fb, int offset 
               , String text 
               , AttributeSet aset) 
     { 
      try 
      { 
       super.insertString(fb, offset, text.replaceAll("\\D++", ""), aset); 
      } 
      catch(BadLocationException ble) 
      { 
       ble.printStackTrace(); 
      } 
     } 

     @Override 
     public void replace(FilterBypass fb, int offset, int len 
              , String text 
              , AttributeSet aset) 
     { 
      try 
      { 
       super.replace(fb, offset, len, text.replaceAll("\\D++", ""), aset); 
      } 
      catch(BadLocationException ble) 
      { 
       ble.printStackTrace(); 
      } 
     }  
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new TextFieldExample().displayGUI(); 
      } 
     }); 
    } 
} 
+0

@Laughy :如果你將'JTextField'留空,那麼你肯定會得到'NumberFormatException',因爲你不能將''「'轉換爲整數值。 –

相關問題