JavaFX 2 BorderPane使用全空间

栏目: Java · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/11213079/javafx-2-borderpane-use-full-space

我只是面对一个我自己无法解决的问题.

我尝试在我的BorderPane中放置一个包含TextField和HTML-Editor的vBox,但是

没有使用完整的空间.另一个问题是,如果我收缩窗口,html编辑器

与我的左选项窗口重叠.

private void initEditor()
{
editor = new HTMLEditor();
editor.setId("editor");
editor.lookup(".top-toolbar").setDisable(true);
editor.lookup(".top-toolbar").setManaged(false);
((ToolBar) editor.lookup(".bottom-toolbar")).getItems().addAll(FXCollections.observableArrayList(((ToolBar)editor.lookup(".top-toolbar")).getItems()));

editorBox = new VBox();
TextField field = new TextField();
field.setPrefHeight(36);
field.setId("editor-title");
editorBox.setFillWidth(true);
editorBox.getChildren().addAll(field, editor);
    root.setCenter(editorBox);
}

好的,所以这里有一些问题,我会尝试解决它们提供一些建议和示例解决方案.

I try to place a vBox including a TextField and a HTML-Editor in my BorderPane, but the full space is not used.

您需要使用 VBox.setVgrow(editor, Priority.ALWAYS) 方法让HTMLEditor占用VBox中的任何额外空间.此外,请确保HTMLeditor具有无限制的最大高度,以便它可以增长到适合可用区域,例如editor.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE).调用editorBox.setFillWidth(true)是多余的,因为fillWidth属性的默认值为true.

但即使你做了所有这些,(截至2.2b13),WebPane大小调整中存在一个错误,会导致问题.在内部,WebPane实现为包含 工具 栏和可编辑WebView的GridPane.默认情况下,WebView的首选大小为800×600. HtmlEditor控件不会为WebView设置GridPane约束,以允许它的大小超过其首选大小.

要解决此问题,您可以在将WebView添加到活动场景后通过CSS查找查找WebView并手动调整GridPane约束.这是一些代码:

stage.show();
...
WebView webview = (WebView) editor.lookup("WebView");
GridPane.setHgrow(webview, Priority.ALWAYS);
GridPane.setVgrow(webview, Priority.ALWAYS);
I shrink the window, the html-editor overlaps with my left option window.

在BorderPane中明确设置中心窗格的最小宽度设置,以使其不会溢出外边缘Panes.

editorBox.setMinSize(0, 0);

你需要这样做是因为 BorderPane documentation 声明:

BorderPane does not clip its content by default, so it is possible that childrens’ bounds may extend outside its own bounds if a child’s min size prevents it from being fit within it space.

另外,代码中的查询调用是可疑的.通常,在将节点添加到显示的舞台上的活动场景中并且JavaFX系统有机会在节点上运行CSS布局传递之前,您无法通过css ID查找节点 – 否则查找将返回null.

对于调试JavaFX布局问题, ScenicView 应用程序非常有用.

这是一个完整的示例,用于生成类似于问题中链接的图像的布局,但没有您提到的问题.

import com.javafx.experiments.scenicview.ScenicView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.*;

public class HtmlEditorInBorderPane extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(final Stage stage) throws IOException {
    // option pane.
    VBox optionPane = new VBox(10);
    MenuBar menuBar = new MenuBar();
    menuBar.getMenus().addAll(new Menu("School"), new Menu("Social"), new Menu("Network"));
    TreeItem<String> root = new TreeItem<>("Private Notes");
    root.setExpanded(false);
    root.getChildren().addAll(new TreeItem<>("Layout"), new TreeItem<>("is not"), new TreeItem<>("easy"));
    TreeView<String> notes = new TreeView<>(root);
    optionPane.getChildren().addAll(menuBar, new Label("Kaiser Notes"), notes);
    optionPane.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");

    // editor pane.
    HTMLEditor editor = new HTMLEditor();
    VBox editorBox = new VBox(10);
    TextField textField = new TextField();
    editorBox.getChildren().addAll(textField, editor);
    editorBox.setStyle("-fx-background-color: mistyrose; -fx-padding: 10;");
    editor.setHtmlText(getSampleText());

    // option layout constraints
    VBox.setVgrow(notes, Priority.ALWAYS);

    // editor layout constraints
    textField.setMinHeight(Control.USE_PREF_SIZE); // stop the textfield from getting squashed when the scene is sized small.
    VBox.setVgrow(editor, Priority.ALWAYS);        // tells the editor to fill available vertical space.
    editorBox.setMinSize(0, 0);                    // stops the editor from overflowing it's bounds in a BorderPane.

    // layout the scene.
    BorderPane layout = new BorderPane();
    layout.setLeft(optionPane);
    layout.setCenter(editorBox);
    stage.setScene(new Scene(layout));
    stage.show();

    // addition of static layout grow constraints for the htmleditor embedded webview.
    WebView webview = (WebView) editor.lookup("WebView");
    GridPane.setHgrow(webview, Priority.ALWAYS);  // allow the webview to grow beyond it's preferred width of 800.
    GridPane.setVgrow(webview, Priority.ALWAYS);  // allow the webview to grow beyond it's preferred height of 600.
  }

  // get some dummy lorem ipsum sample text.
  private String getSampleText() throws IOException {
    StringBuilder builder = new StringBuilder();
    try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL("http://www.lorem-ipsum-text.com/").openStream()))) {
      String string;
      while ((string = in.readLine()) != null) {
        builder.append(string);
      }
    }
    return builder.toString();
  }
}

翻译自:https://stackoverflow.com/questions/11213079/javafx-2-borderpane-use-full-space


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

可视化未来

可视化未来

[美] 埃雷兹·艾登、[法] 让-巴蒂斯特·米歇尔 / 王彤彤、沈华伟、程学旗 / 浙江人民出版社 / 2015-9 / 54.90元

科学的传播速度有多快?今时今日我们很少谈论上帝了吗?人们什么时候开始用“having sex” 而不用“making love”? 史上的人是在哪岁成名的?语法的变化速度到底有多快?哪些作家被纳粹审查得最彻底? “donut” 什么时候开始取代“doughnut”? 我 们能否预测人类未来?比尔·克林顿和花椰菜哪个更出名? 《可视化未来》一书的两位作者通过与“谷歌图书”的合作,得以有机会研究......一起来看看 《可视化未来》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具