2014-11-04 79 views
2

我想有一個StackPane作爲根節點,它使覆蓋效果變得很容易。JavaFX調整大小的根堆棧窗格

但是,通過使用堆疊面板作爲根,內部控件可以移出窗口。 在以下示例中,如果將窗口縮小到足夠小,您可以看到控件移出窗口, 。菜單欄和列表視圖都顯示在左側。我想要防止這種情況,我該怎麼做?

package test; 

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.collections.FXCollections; 
import javafx.scene.Scene; 
import javafx.scene.control.ListView; 
import javafx.scene.control.Menu; 
import javafx.scene.control.MenuBar; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class App extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("controls move out of window :("); 

     Menu menu = new Menu("Menu"); 
     MenuBar menubar = new MenuBar(menu); 

     ListView<String> listView = new ListView<>(FXCollections.observableArrayList("Args, it moves away.")); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setTop(menubar); 
     borderPane.setLeft(listView); 

     StackPane rootStackPane = new StackPane(borderPane); 
     Scene scene = new Scene(rootStackPane); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

} 

更新:

設置爲StackPane內BorderPane排列似乎幫助:

public void start(Stage stage) throws Exception { 
    stage.setTitle("controls stay in window :)"); 

    Menu menu = new Menu("Menu"); 
    MenuBar menubar = new MenuBar(menu); 

    ListView<String> listView = new ListView<>(FXCollections.observableArrayList("it stays!")); 

    BorderPane borderPane = new BorderPane(); 
    borderPane.setTop(menubar); 
    borderPane.setLeft(listView); 

    StackPane rootStackPane = new StackPane(borderPane); 
    StackPane.setAlignment(borderPane, Pos.TOP_LEFT); 
    Scene scene = new Scene(rootStackPane); 
    stage.setScene(scene); 

    stage.show(); 
} 

回答

3

將你的籌碼在Group和堆的首選大小結合現場的首選尺寸。

centered larger centered smaller

你可以從第二圖像看,覆蓋不居中可見屏幕上,它的中心位於StackPane。 StackPane的最小尺寸將是其最大組件的最小尺寸(即使它溢出屏幕),所以疊加層以此爲中心。要找出最小尺寸的東西,您可以使用設計SceneBuilder中的UI或使用ScenicView來調試UI。

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 

public class App extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     stage.setTitle("controls stay in window :)"); 

     Menu menu = new Menu("Menu"); 
     MenuBar menubar = new MenuBar(); 
     menubar.getMenus().add(menu); 

     ListView<String> listView = new ListView<>(FXCollections.observableArrayList("Args, it moves away.")); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.setTop(menubar); 
     borderPane.setLeft(listView); 
     borderPane.setStyle("-fx-background-color: palegreen;"); 

     Node overlayContent = new Label("centered"); 
     StackPane stack = new StackPane(borderPane, overlayContent); 

     Scene scene = new Scene(new Group(stack)); 
     stage.setScene(scene); 
     stage.show(); 

     stack.prefWidthProperty().bind(scene.widthProperty()); 
     stack.prefHeightProperty().bind(scene.heightProperty()); 
    } 

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

是的,這是有效的。在StackPane上讀取您的中心後,我有一個想法。 將StackPane的對齊設置爲Pos.TOP_LEFT如何? 查看更新的問題。 這裏它工作,沒有一個組節點和StackPane的綁定。 – coder 2014-11-05 17:41:09

+1

'StackPane.setAlignment(borderPane,Pos.TOP_LEFT);'工作,是一個更好的解決方案國際海事組織。 – jewelsea 2014-11-05 18:10:10

+1

如果您願意,您可以編輯您的問題,刪除更新並[發表自我回復](http://stackoverflow.com/help/self-answer)標記爲正確。 – jewelsea 2014-11-05 18:11:37