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();
}
是的,这是有效的。在StackPane上读取您的中心后,我有一个想法。 将StackPane的对齐设置为Pos.TOP_LEFT如何? 查看更新的问题。 这里它工作,没有一个组节点和StackPane的绑定。 – coder 2014-11-05 17:41:09
'StackPane.setAlignment(borderPane,Pos.TOP_LEFT);'工作,是一个更好的解决方案国际海事组织。 – jewelsea 2014-11-05 18:10:10
如果您愿意,您可以编辑您的问题,删除更新并[发表自我回复](http://stackoverflow.com/help/self-answer)标记为正确。 – jewelsea 2014-11-05 18:11:37