Java FX从不同场景改变Label的值

我有两个场景。 第一个场景使用以下代码调用第二个场景。

@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”的标签。

我需要在此函数中更改该标签的内容并返回结果(true / false)。 帮帮我?

为FXML创建一个ConfirmationController 。 从控制器中,公开一种方法,该方法允许您传递数据(字符串)以设置为标签。

 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 ... 

FXML中的标签有一个setText方法。 因此,对于您的情况,“Proceed”标签将类似于:

 Proceed.setText("The new text"); 

至于问题的第二部分,我不是100%肯定你在问什么。 我真的没有看到该函数返回true或false的任何情况。

假设您有一个名为: confirmation_controller.java'.的控制器confirmation_controller.java'. 在该控制器中,您有一个公共方法getProceedLabel() ,它返回名为Proceed的标签的引用。 您可以尝试以下代码:

  Stage confirmation_stage; Parent confirmation; confirmation_stage=new Stage(); FXMLLoader loader = new FXMLLoader(getClass().getResource("Confirmation.fxml")); confirmation = loader.load(); confirmation_controller controller = loader.getController(); Label label = controller.getProceedLabel(); label.setText("..."): confirmation_stage.setScene(new Scene(confirmation)); confirmation_stage.initOwner(generate_button.getScene().getWindow()); confirmation_stage.show();