2017-01-12 70 views
0

我嘗試轉換數據,但沒有點擊一個按鈕, 當我在第一個文本框沒有輸入數據恰好兩個TextField更新對方

JTextField textC = new JTextField() ; 
    JTextField textF = new JTextField() ; 
    labelC.setText("Celsius"); 
    labelF.setText("Fahrenheit"); 

ActionListener textFieldCListener = new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
    String value = textC.getText(); 
    try { 
     float valC = new Float(value); 
     float valF = valC * 1.8f + 32; 
     textF.setText(Float.toString(valF)); 
    } catch (Exception exp) { 
     textF.setText(""); 
     textC.setText(""); 
    } 
}}; 
+0

你需要添加textFieldCListener到您的文本字段 – Jerry06

回答

1

嘗試: textC.addActionListener(textFieldCListener);