2014-11-04 32 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