英文:
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
你需要设置randomIntegerField
的text属性(我假设它是一个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()));
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论