我有两个场景。第一个场景使用以下代码调用第二个场景。
@FXML
private void confirmation(ActionEvent event) throws IOException{
Stage confirmation_stage;
Parent confirmation;
confirmation_stage=new Stage();
confirmation=FXMLLoader.load(getClass().getResource("Confirmation.fxml"));
confirmation_stage.setScene(new Scene(confirmation));
confirmation_stage.initOwner(generate_button.getScene().getWindow());
confirmation_stage.show();
}
“Confirmation.fxml”中有一个名为“Proceed”的标签。
我需要在此函数中更改该标签的内容并返回结果(真/假)。帮忙?
最佳答案
为 FXML 创建一个 ConfirmationController
。从 Controller 中,公开一个允许您传递数据(字符串)以设置标签的方法。
public class ConfirmationController implements Initializable {
...
@FXML
private Label proceed;
...
public void setTextToLabel (String text) {
proceed.setText(text);
}
...
}
在加载 FXML 的方法中,您可以:
...
FXMLLoader loader = new FXMLLoader(getClass().getResource("Confirmation.fxml"));
confirmation = loader.load();
ConfirmationController controller = (ConfirmationController)loader.getController();
controller.setTextToLabel("Your Text"); // Call the method we wrote before
...
关于Java FX从不同场景更改标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31083113/