User interface 如何将值传递到fxml场景
我正在制作这个javaFX应用程序,但在给定的时刻,我想从类中的一个方法获取场景中存在的文本,我如何将该值传递到FXML文档中的文本框User interface 如何将值传递到fxml场景,user-interface,methods,javafx,fxml,User Interface,Methods,Javafx,Fxml,我正在制作这个javaFX应用程序,但在给定的时刻,我想从类中的一个方法获取场景中存在的文本,我如何将该值传递到FXML文档中的文本框 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.effect.*?> <?import javafx.scene.image.*?> <?import javafx.scene.layout.*?> <?import javafx.s
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.72" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.crapgames.calourosimulator.views.pcna.intro.Introduction">
<children>
<ImageView fx:id="imgClick" fitHeight="600.0" fitWidth="800.0" onMouseClicked="#nextScene" pickOnBounds="true">
<image>
<Image url="@../../assets/pnca-monitor-text.png" />
</image>
</ImageView>
<Text fx:id="text1" fill="#f5eded" layoutX="8.0" layoutY="452.0" strokeType="OUTSIDE" strokeWidth="0.0" text="where i want my string variable" wrappingWidth="787.0">
<font>
<Font name="Comic Sans MS" size="25.0" />
</font></Text>
</children>
</AnchorPane>
在控制器类中创建一个方法以接受文本值。如果需要,您可以利用
Text
的textProperty
:
public class Introduction {
@FXML
private Text text1 ;
public StringProperty textProperty() {
return text1.textProperty();
}
public final void setText(String text) {
textProperty().set(text);
}
public final String getText() {
return textProperty().get();
}
// existing code ...
}
现在您可以执行以下操作:
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml/file.fxml"));
Parent root = loader.load();
Introduction controller = loader.getController();
controller.setText("Hello World!");
Scene scene = new Scene(root);
// etc...