2013-02-18 64 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); 
}