JavaFX – BorderPane / StackPane在子项更改后未调整大小
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaFX – BorderPane / StackPane在子项更改后未调整大小,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3642字,纯文字阅读大概需要6分钟。
内容图文
在JavaFX BorderPane中添加和删除内容时,我遇到了调整大小问题.在手动调整窗口大小之前,不会调整BorderPane内容的大小.我写了一个小测试应用程序来模拟这种行为.该应用程序构建一个BorderPane,其中包含嵌入在BorderPane中心的StackPane中的矩形.在BorderPane的底部有一个包含文本和分隔符的VBox和HBox.有一个菜单项可以从BorderPane的底部删除内容(MoveText – > Right),并将类似的内容添加到BorderPane的正确位置.
当文本添加到BorderPane的右侧位置时,矩形与文本重叠.换句话说,BorderPane中心的内容与BorderPane权限中的内容重叠.
我看到以下链接 –
https://stackoverflow.com/questions/5302197/javafx-bug-is-there-a-way-to-force-repaint-this-is-not-a-single-threading-pr
调用requestLayout似乎没有帮助.我也尝试在图中的各个节点上调用impl_updatePG和impl_transformsChanged.我从这个主题得到了这个想法 – https://forums.oracle.com/forums/thread.jspa?threadID=2242083
public class BorderPaneExample extends Application
{
private BorderPane root;
private StackPane centerPane;
@Override
public void start(Stage primaryStage) throws Exception
{
root = new BorderPane();
root.setTop(getMenu());
root.setBottom(getBottomVBox());
centerPane = getCenterPane();
root.setCenter(centerPane);
Scene scene = new Scene(root, 900, 500);
primaryStage.setTitle("BorderPane Example");
primaryStage.setScene(scene);
primaryStage.show();
}
private MenuBar getMenu()
{
MenuBar menuBar = new MenuBar();
MenuItem rightMenuItem = new MenuItem("Right");
rightMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
root.setRight(getRightHBox());
root.setBottom(null);
root.requestLayout();
}
});
MenuItem bottomMenuItem = new MenuItem("Bottom");
bottomMenuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
root.setRight(null);
root.setBottom(getBottomVBox());
}
});
Menu menu = new Menu("Move text");
menu.getItems().add(rightMenuItem);
menu.getItems().add(bottomMenuItem);
menuBar.getMenus().addAll(menu);
return menuBar;
}
private HBox getRightHBox()
{
HBox hbox = new HBox();
VBox vbox = new VBox(50);
vbox.setPadding(new Insets(0, 20, 0, 20));
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().addAll(new Text("Additional Info 1"),
new Text("Additional Info 2"), new Text("Additional Info 3"));
hbox.getChildren().addAll(new Separator(Orientation.VERTICAL), vbox);
return hbox;
}
private VBox getBottomVBox()
{
VBox vbox = new VBox();
HBox hbox = new HBox(20);
hbox.setPadding(new Insets(5));
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(new Text("Footer Item 1")
, new Text("Footer Item 2"), new Text("Footer Item 3"));
vbox.getChildren().addAll(new Separator(), hbox);
return vbox;
}
private StackPane getCenterPane()
{
StackPane stackPane = new StackPane();
stackPane.setAlignment(Pos.CENTER);
final Rectangle rec = new Rectangle(200, 200);
rec.setFill(Color.DODGERBLUE);
rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
rec.heightProperty().bind(stackPane.heightProperty().subtract(50));
stackPane.getChildren().addAll(rec);
return stackPane;
}
public static void main(String[] args)
{
Application.launch(args);
}
}
任何建议,将不胜感激.
谢谢
解决方法:
为了得到答案:
StackPane需要设置其最小大小才能进行裁剪.所以如果你明确地添加了stackPane.setMinSize(0,0);到getCenterPane()方法,它应该解决你的问题.
所以你的getCenterPane()方法现在看起来像这样:
private StackPane getCenterPane()
{
StackPane stackPane = new StackPane();
stackPane.setMinSize(0, 0);
stackPane.setAlignment(Pos.CENTER);
final Rectangle rec = new Rectangle(200, 200);
rec.setFill(Color.DODGERBLUE);
rec.widthProperty().bind(stackPane.widthProperty().subtract(50));
rec.heightProperty().bind(stackPane.heightProperty().subtract(50));
stackPane.getChildren().addAll(rec);
return stackPane;
}
内容总结
以上是互联网集市为您收集整理的JavaFX – BorderPane / StackPane在子项更改后未调整大小全部内容,希望文章能够帮你解决JavaFX – BorderPane / StackPane在子项更改后未调整大小所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。