2013-02-18 33 views
1

我加載階段方式如下:現場舞臺無法適應

public void start(Stage stage) throws Exception{ 
    stage.setTitle("title"); 
    Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/MainMenu.fxml")); 
    stage.setScene(scene); 
    stage.setWidth(1080); 
    stage.setHeight(720); 
    stage.setFullScreen(false); 
    stage.show(); 
} 

更改場景:

@FXML protected void click(ActionEvent event) throws Exception{ 
    Stage stage = (Stage)menu.getScene().getWindow(); 
    Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/MainMenu.fxml")); 
    stage.setScene(scene); 
    stage.show(); 
} 

FXML:

<Scene fx:controller="controller.CtrlMainMenu" xmlns:fx="http://javafx.com/fxml" stylesheets="view/Style.css"> 
    <AnchorPane fx:id="menu"> 
     <VBox spacing="8" 
       AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" 
       AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"> 
      <Button text="click" onAction="#click"/> 
     </VBox> 
    </AnchorPane> 
</Scene> 

改變現場的時候,問題就來了;新的場景變小了,出現在左上角,並且沒有完全適合窗口(窗口保持其大小,並且在調整窗口大小之後,場景再次改變到窗口,但之前,不是)。

另外,是否有可能發送一個對象到視圖的控制器,因爲我需要使用控制器來處理模型? (使用MVC架構)


立即嘗試這種方法2與FX:根和有以下幾點:

Aplication:

public void start(Stage stage) throws Exception{ 
    FXMLLoader fxmlLoader = new FXMLLoader(); 
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/MainMenu.fxml").openStream()); 
    CtrlMainMenu controller = (CtrlMainMenu) fxmlLoader.getController(); 
    controller.sendString("test");  //send object test 
    stage.setScene(new Scene(p)); 

    stage.setWidth(1080); 
    stage.setHeight(720); 
    stage.setFullScreen(false); 
    stage.show(); 
} 

FXML:

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="controller.CtrlMainMenu"> 
    <children> 
     <AnchorPane fx:id="menu"> 
      <VBox spacing="8" 
        AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" 
        AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"> 
       <Button text="click" onAction="#click"/> 
      </VBox> 
     </AnchorPane> 
    </children> 
</fx:root> 

變化場景:

@FXML protected void click(ActionEvent event) throws Exception{ 
    FXMLLoader fxmlLoader = new FXMLLoader(); 
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream()); 
    Stage stage = (Stage)menu.getScene().getWindow(); 
    stage.setScene(new Scene(p)); 
    stage.show(); 
} 

重疊:

@FXML protected void Options(ActionEvent event) throws Exception{ 
    FXMLLoader fxmlLoader = new FXMLLoader(); 
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream()); 
    menu.getChildren().add(p); 
} 

FXML(舊的沒有足夠的外匯:上根線):

<fx:root type="javafx.scene.layout.AnchorPane" xmlns:fx="http://javafx.com/fxml" 
     fx:controller="controller.CtrlOptions" stylesheets="view/Style.css"> 
    <children> 
     <GridPane xmlns:fx="http://javafx.com/fxml" alignment="center" 
        hgap="10" vgap="10" id="optionBackgorund" 
        AnchorPane.topAnchor="50.0" AnchorPane.bottomAnchor="50.0" 
        AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" 
        fx:id="options"> 
      <!-- the view --> 
     </GridPane> 
    </children> 
</fx:root> 

好了,現在可以發送對象,使與FX的場景:根,但仍然有場景不合適的問題

現在有一個新問題,我沒有沒有FX:根,這是一個使用另一個AnchorPane重疊當前的一個,現在不重疊,它只是出現在中間,但大小RS保持較小(前裝與錨)

+0

您的例子並不爲我工作(點擊按鈕什麼都不做) – Sebastian 2013-02-18 15:41:31

+0

我建議不要改變場景,但每當屏幕將要改變時都會替換它的根。 FXMLs最頂級的paret將是一個帶有控制器類的佈局容器。要向控制器發送對象,請首先在FXML加載完成後獲取控制器實例,然後調用您在控制器中寫入的方法來更新視圖。 – 2013-02-18 18:49:32

+0

可以給我一個替換根和向控制器發送對象的例子嗎?,還沒有找到任何...(或者用於搜索的關鍵字不是正確的...) – user1090694 2013-02-18 22:59:52

回答

0

發現,爲適應場景的舞臺方式:

@FXML protected void click(ActionEvent event) throws Exception{ 
    FXMLLoader fxmlLoader = new FXMLLoader(); 
    Pane p = (Pane) fxmlLoader.load(getClass().getResource("../view/Options.fxml").openStream()); 
    Scene scene= menu.getScene(); 
    scene.setRoot(p); 
}