按下按钮后TextField不更新

时间:2017-12-12 19:03:55

标签: button javafx textfield

我是JavaFX的初学者,想创建一个简单的Pounds to Ounces转换器。我能够从Pounds获取textField并进行计算,但是当我尝试为Ounces字段设置文本时,我的字段没有更新。我哪里错了?

公共类LbsOzConverter2扩展了Application {

@Override
public void start(Stage stage) throws Exception {
    Parent parent = FXMLLoader.load(getClass().getResource("LbsOzConverter2.fxml"));

    Scene scene = new Scene(parent);

    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

public class LbsOzConverter2Controller {

@FXML
private Button btnToOunces;

@FXML
private TextField tfLBS;
TextField tfOZ;

@FXML
private Label lblLBS;
private Label lblOZ;
private Label label;

public void initialize()
{
    // test text field
    tfOZ = new TextField();
    tfOZ.setText("test value");
}

// Event listener for the btnToOunces button
public void btnToOuncesListener(ActionEvent event)
{
    final double OZ_CONVER_FACTOR = 16;

    // Get the pounds from the TextField
    String str = tfLBS.getText();
    System.out.println(str);

    // Convert pounds to Ounces
    double oz = Double.parseDouble(str) * OZ_CONVER_FACTOR;
    System.out.println(str);

    // Display the converted weight 
    tfOZ.setText(""+oz);
    System.out.println(tfOZ.getText());
}   

}

0 个答案:

没有答案