JavaFX调整根堆栈大小

时间:2014-11-04 20:52:54

标签: javafx-2 javafx-8

我希望将StackPane作为根节点,它可以轻松实现叠加效果。

但是通过使用堆栈窗口作为根,内部控件可以移出窗口。 在以下示例中,您可以看到控件移出窗口, 如果你缩小窗口足够小,例如菜单栏和列表视图都向左移动。我想阻止这个,我怎么能这样做?

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();
}

1 个答案:

答案 0 :(得分: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);
    }
}