从另一个场景(具有自己的控制器)打开一个新场景(具有自身的控制器)



我是JavaFX的新手,但仍在学习,我遇到了这个问题,找不到答案。

我有两个名为"one.FXML"one_answers"two.FXML"的FXML文件,它们有自己的控制器类,名为"OneController.java"one_answers"TwoController.java">

我想用onButtonClick从"one.fxml".打开一个"two.fsml">

"one.fxml"中的按钮代码为:

<Button fx:id="clearButton" layoutX="690.0" layoutY="309.0" minHeight="18.0" mnemonicParsing="false" prefHeight="40.0" prefWidth="196.0" text="Clear" onAction="#buttonclearClick"/>

我在"OneController.java"中有这个方法

private void buttonclearClick(final ActionEvent event)
{
//code to be written?
}

以及我如何将某些字段的值从"one.fxml"转移到"two.ffml">

类似:

如果"one.fxml"也有这个标签:

<Text fx:id="textLabel" fill="#001f4b" layoutX="14.0" layoutY="377.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Powered by StackOverflow">

如何将此文本字段值(text="Powered by StackOverflow")转换为"two.fxml"或TwoControlle.java

如何做到这一点?

一个控制器

private void buttonclearClick(final ActionEvent event)
{
try
{
FXMLLoader loader = new FXMLLoader(getClass().getResource("two.fxml"));
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
//stage.initOwner(MainStage.stage);
stage.setScene(new Scene((Pane)loader.load()));
TwoController tsc = loader.<TwoController>getController();
tsc.GettextVal(textLabel.getText()); // Function in TwoController
stage.show();
}
catch(Exception e)
{
e.printStackTrace();
}
}

两个控制器

在两个控制器中生成GettextVal函数

@FXML
void initialize() 
{
//Initialize code here
}
public void GettextVal(String txtval)
{
System.out.println("text value from one controller - "+txtval);
}

最新更新