整数值填入文本字段中

huangapple go评论77阅读模式
英文:

Int Value into text Field

问题

在JavaFX中,我有一个名为 "randomIntegerField" 的TextField,我需要在单击按钮时,将这个TextField自动填充为由一个方法返回的整数值。

这里是返回整数值的方法:

private int randomNum(){
     Random random = new Random();
     int integerIntered = Integer.parseInt(numberField.getText()); 
     return random.nextInt(integerIntered - 0 + 1) + 0; 
}

这是按钮的部分:

bntGo.setText("Go");

bntGo.setOnAction(e -> {
   randomIntegerField.setText(String.valueOf(randomNum())); // 我得到的错误是String不能转换为TextField。
});

请帮帮我。

英文:

In JavaFX, I have a TextField Called "randomIntegerField" I need to auto populate this textfield JavaFX with an integer value returned by a method when i clik on a button.

Here is the method which return the int Value

private int randomNum(){
     Random random = new Random();
     int integerIntered = Integer.parseInt(numberField.getText()); 
     return   random.nextInt(integerIntered - 0 + 1) + 0; 
}

Here is the Button

 bntGo.setText("Go");
    
    bntGo.setOnAction(e -> {
       randomIntegerField = String.valueOf(randomNum()); // The error i get is String can not converted into TextField. 
    });

Help me please

答案1

得分: 1

你需要设置randomIntegerFieldtext属性(我假设它是一个TextField),例如...

bntGo.setOnAction(e -> {
       randomIntegerField.setText(String.valueOf(randomNum())); 
});
英文:

You need to set the text property of randomIntegerField (which I assume is a TextField), for example...

bntGo.setOnAction(e -> {
       randomIntegerField.setText(String.valueOf(randomNum())); 
});

huangapple
  • 本文由 发表于 2020年10月12日 01:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64307121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定