我尝试转换数据但没有单击按钮, 当我在第一个文本字段中输入数据时,没有任何反应
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("");
}
}};
最佳答案
您应该将 ActionListener
添加到您的 JTextField
对象。
textC.addActionListener(textFieldCListener);
关于java - 两个文本字段相互更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41607753/