2016-04-16 122 views
0

我正在制作这个javaFX应用程序,但是在给定时刻我想从场景中的某个方法获取文本,如何将该值传递给我的文本框在FXML文件中?如何将值传递给fxml场景

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

回答

0

在控制器类中创建一个方法来接受文本值。您可以利用TexttextProperty如果你想:

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