2017-02-17 129 views
-1

雖然我試圖加載兩個不同的場景,但它不打開圖像FXML,基本上我想先打開圖像FXML,然後打開歡迎屏幕。一些如何圖像場景沒有顯示。圖像FXML不在JavaFX中打開

Main.java

public class Main extends Application { 

@Override 
public void start(Stage stage) throws Exception { 
    System.out.println("im in main"); 
    Parent root = FXMLLoader.load(getClass().getResource("ImageScreen.fxml")); 


    Scene scene = new Scene(root); 

    stage.setScene(scene); 
    stage.setTitle("test"); 
    stage.show(); 
} 
public static void main(String[] args) { 
    launch(args); 
} 

}

ImageScreen.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.image.Image?> 
<?import javafx.scene.image.ImageView?> 
<?import javafx.scene.layout.Pane?> 
<?import javafx.scene.layout.StackPane?> 

<StackPane fx:id="imagepane" maxHeight="-Infinity" maxWidth="-Infinity" 
minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" 
prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102"  xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="application.ImageScreenController"> 
<children> 
    <ImageView fitHeight="438.0" fitWidth="603.0" pickOnBounds="true" 
     preserveRatio="true"> 
     <image> 
      <Image url="@bamboo-fountain-and-zen-stone.jpg" /> 
     </image> 
    </ImageView> 
    <Pane prefHeight="200.0" prefWidth="200.0" /> 
</children> 

ImageScreenController

public class ImageScreenController implements Initializable { 

@FXML 
private StackPane imagepane; 

public static AnchorPane welcomepane; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 

    AnchorPane pane; 
    try { 
     System.out.println("im in controller"); 
     pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml")); 
     imagepane.getChildren().setAll(pane); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

}

WelcomeFXMLDoc.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.text.Font?> 
<?import javafx.scene.text.Text?> 


<AnchorPane fx:id="welcomepane" maxHeight="-Infinity" 
maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" 
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.102" 
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.WelcomeFXMLController"> 
<children> 
    <Text layoutX="212.0" layoutY="100.0" strokeType="OUTSIDE" 
     strokeWidth="0.0" text="Welcome" wrappingWidth="176.13671875"> 
     <font> 
      <Font size="35.0" /> 
     </font> 
    </Text> 
</children> 

WelcomeFXMLController.java

public class WelcomeFXMLController implements Initializable { 

/** 
* Initializes the controller class. 
* 
*/ 

@FXML 
private AnchorPane welcomepane; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
} 

}

回答

1

ImageScreenController您使用imagepane.getChildren().setAll(pane)setAll()清除集合,然後添加新元素。在這種情況下,您從場景圖中刪除ImageView並僅顯示welcomepane

改爲使用imagepane.getChildren().add(pane);,然後在不需要時再刪除welcomepane

編輯基於您的評論

使用PauseTransition等待WelcomeFXMLController

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    PauseTransition pt = new PauseTransition(Duration.seconds(10)); 
    pt.setOnFinished(e -> { 
     AnchorPane pane; 
     try { 
      System.out.println("im in controller"); 
      pane = FXMLLoader.load(getClass().getResource("WelcomeFXMLDoc.fxml")); 
      imagepane.getChildren().setAll(pane); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    }); 
    pt.play(); 
} 
+0

如果我按照這種方法,歡迎場面將在圖像場景的頂部,我不希望重寫,我想一旦圖像場景被完全加載,圖像場景應該消失,並且應該單獨加載歡迎場景,如果我在圖像場景中添加任何按鈕,並且如果點擊它,它將打開歡迎場景,但是我不想在圖像場景上放置按鈕。 – user2459816

+0

加載圖像場景時是否有一些長時間的運行過程?因爲在提供的代碼中,您的圖像屏幕將立即顯示,因此無法顯示它。 – MBec

+0

我想先加載圖片先等待10秒,然後加載歡迎場景..上面沒有提供等待代碼..如果你能這樣做會有所幫助 – user2459816