Java不同场景标签的FX变化值

Java FX changing value of Label from different scene

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

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

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

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

Proceed.setText("The new text");

至于问题的第二部分,我不是100%确定你问的是什么。我真的没有看到函数 return 真或假的任何情况。

假设您有一个名为:confirmation_controller.java'. 的控制器,在该控制器内,您有一个 public 方法 getProceedLabel() returns 对名为 [= 的标签的引用13=]。你可以试试下面的代码:

 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();

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