当前位置: 首页 > 知识库问答 >
问题:

JavaFX2BorderPane使用完整空间

孟乐逸
2023-03-14

我只是面临一个我自己无法解决的小问题。我试图在BorderPane中放置一个vBox,其中包括一个TextField和一个HTML-Editor,但是没有使用全部空间。另一个问题是,如果我收缩窗口,html-editor与我的左侧选项窗口重叠。

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

共有1个答案

姜振濂
2023-03-14

好的,这里有一些错误,我将试着解决它们,提供一些建议和一个示例解决方案。

我试图在BorderPane中放置一个vBox,其中包括一个TextField和一个HTML-Editor,但是没有使用全部空间。

您需要使用vbox.setvgrow(editor,priority.Always)方法来让HTMLEditor占用Vbox中的任何额外空间。此外,请确保HTMLeditor具有无界的最大高度,以便它可以增长以适应可用区域,例如editor.setmaxsize(double.max_value,double.max_value)。调用editorbox.setfillwidth(true)是多余的,因为fillwidth属性的默认值为true

要解决这个问题,可以在WebView添加到活动场景后通过css查找来查找WebView,并手动调整GridPane约束。下面是一些代码:

stage.show();
...
WebView webview = (WebView) editor.lookup("WebView");
GridPane.setHgrow(webview, Priority.ALWAYS);
GridPane.setVgrow(webview, Priority.ALWAYS);

我收缩窗口,HTML编辑器与我的左侧选项窗口重叠。

显式设置BorderPane中中心窗格的最小宽度设置,以便它不会溢出外缘窗格。

editorBox.setMinSize(0, 0);

这里有一个完整的例子来生成一个类似于您的问题中链接的图像的布局,但没有您提到的问题。

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();
  }
}
 类似资料:
  • 我遇到了以下问题:当我解马歇尔对象时,xml返回如下所示: 相反 我已经尝试更改我的包信息(类在多个包上),可以添加@xmln表示法和前缀="",但是命名空间在其他节点上移动。 有没有一个选项可以强制jaxb使用不带前缀的本地名称空间?

  • 我正在刮网页使用JSoup库通过选择类属性,其中包含"nav"字符串。 这是获取网站超文本标记语言的代码: 正如您所看到的,id=“varPreviewMenu”的ul元素包含Jsoup检索到的HTML不包含的li元素。 我怎样才能得到那些元素?

  • 完整的利用链 至此,我们已经讨论了很多方面来说明如何远程漏洞利用这辆吉普和类似的车型。目前为止,这些信息已经足够你实现完整的漏洞利用,但是我们想要总结一下漏洞链是如何自始至终发挥作用的。 识别目标 你需要车辆的IP地址。你可以随便选择一个或写一个蠕虫来入侵所有的车辆。如果你知道汽车的VIN或GPS,你可以根据你所了解的车辆停留位置来扫描其IP范围,直到发现对应的VIN或GPS。由于Sprint网络

  • 问题内容: 我的目标是在从链接读取href属性时始终获取相同的字符串(在我的情况下为URI)。举例:假设认为,一个HTML文件,它有一个像somany链接 ,但基本域是http://www.domainname.com/index.html 但基本域是http://www.domainname.com/dit/index.html 如何我可以正确获取所有链接意味着完整链接包括域名吗? 我如何在Ja

  • 正如问题所提示的:ATG Rest完整API addItemToOrder不工作 我已经创建了一个组织并在其中添加了用户null指针Expeption已被清除,但是,现在新的错误开始出现在下面是服务器错误 工作流程 添加到购物车[POST方法] Content-Type:Application/JSON http://ip:port/rest/model/atg/commerce/order/pu

  • 本文向大家介绍vue3.0中使用element的完整步骤,包括了vue3.0中使用element的完整步骤的使用技巧和注意事项,需要的朋友参考一下 前言:       在vue3.0中使用element框架,因为element是支持vue2.0的,他推出的支持vue3.0的版本叫 element-plus 官网入口:点我进入 一、个人遇到的问题与解决办法: 遇到的问题: 我直接用cnpm  i e