2016-12-07 67 views
2

我正在使用Javafx(不使用FXML),並且我將舞臺傳遞給控制器​​,以在單擊按鈕時更改舞臺上的場景。場景變化正確,但舞臺和場景的大小增加。它的大小增加約0.1(寬度),高度有時也增加(不是每次)。Javafx場景切換導致舞臺大小增加

這是使用的控制器。

public class Controller { 

public Controller(){ 

} 
public static void homeButtonhandler(Stage stage){ 
     stage.close(); 

    } 
public static void adminButtonhandler(Stage stage){ 




    adminPane adminPane1 = new adminPane(stage); 

    Scene adminScene = new Scene (adminPane1); 

    stage.setScene(adminScene); 
    }} 

adminPane擴展了我創建的另一個名爲mainPane的類,它們都擴展了Pane類。其中有其他窗格用於創建GUI結構。主面板的大小設置像這樣:

top = createTop(stage); 

    this.getChildren().addAll(top); 
    this.setWidth(stage.getWidth()); 
    this.setPrefWidth(stage.getWidth()); 
    this.setMaxWidth(stage.getWidth()); 
    this.setMinWidth(stage.getWidth()); 

    this.setHeight(stage.getHeight()); 
    this.setMaxHeight(stage.getHeight()); 
    this.setMinHeight(stage.getHeight()); 
    this.setPrefHeight(stage.getHeight()); 

我使用的測試類:

public class test extends Application { 

@Override 
public void start(Stage primaryStage) throws Exception { 
    //primaryStage.setResizable(true); 

    mainPane myPane = new mainPane(primaryStage); 
    Scene homeScene = new Scene (myPane); 

    primaryStage.setScene(homeScene); 
    primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/resources/icons/joulesIcon.png"))); 
    primaryStage.show(); 


    } 


public static void main(String[] args) { 
    launch(args); 
} 

} 我相信這是與我擦肩而過的階段,任何指針將不勝感激。

+0

創建一個新的舞臺。 – Sedrick

+0

如果我創建一個新的階段,窗口關閉並重新打開片刻。這不是我要去的簡化的設計。 – user2370794

+0

我的猜測是組件不適合舞臺原始尺寸。我認爲這會導致舞臺增長以適應新的組件。 – Sedrick

回答

1

當我使用primaryStage.setScene(new Scene(root, primaryStage.getWidth(), primaryStage.getHeight()))時,我還發現東西正在窗口底部下方繪製。我的情況是通過使用舊場景的尺寸而不是初級階段的尺寸來解決的。

public void setScene(Parent root) { 
    Scene oldScene = primaryStage.getScene(); 
    primaryStage.setScene(oldScene == null 
      ? new Scene(root, primaryStage.getMinWidth(), primaryStage.getMinHeight()) 
      : new Scene(root, oldScene.getWidth(), oldScene.getHeight())); 
}