2016-04-16 108 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...