好的,这里出现了一些问题,我将尝试解决它们,并提供一些建议和示例解决方案。
我尝试在 BorderPane 中放置一个包含 TextField 和 HTML 编辑器的 vBox,但未使用全部空间。
您需要使用VBox.setVgrow(编辑器,Priority.ALWAYS) http://docs.oracle.com/javafx/2/api/javafx/scene/layout/VBox.html#setVgrow%28javafx.scene.Node,%20javafx.scene.layout.Priority%29方法让 HTMLEditor 占用 VBox 中的任何额外空间。另外,请确保 HTML 编辑器具有无限制的最大高度,以便它可以增长以适应可用区域,例如editor.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE)
。呼唤editorBox.setFillWidth(true)
作为默认值是多余的fillWidth
财产是true
.
但即使你做了所有这些,(从 2.2b13 开始)WebPane 大小调整中存在一个错误,这会给你带来问题。 WebPane 在内部被实现为包含工具栏和可编辑 WebView 的 GridPane。默认情况下,WebView 的首选尺寸为 800x600。 HtmlEditor 控件不会为 WebView 设置 GridPane 约束以允许其大小超过其首选大小。
要解决此问题,您可以通过 css 查找来查找 WebViewafter它已添加到活动场景并手动调整 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);
您需要这样做是因为BorderPane 文档 http://docs.oracle.com/javafx/2/api/javafx/scene/layout/BorderPane.html states:
默认情况下,BorderPane 不会裁剪其内容,因此,如果子项的最小尺寸使其无法容纳在其空间内,则子项的边界可能会超出其自身的边界。
顺便说一句,代码中的查找调用是可疑的。通常,在节点被添加到显示舞台上的活动场景并且 JavaFX 系统有机会在节点上运行 CSS 布局传递之前,您无法通过 css ID 查找节点 - 否则查找将仅返回 null。
为了调试 JavaFX 布局问题,优美的风景 http://fxexperience.com/scenic-view/应用程序是无价的。
这是一个完整的示例,用于生成与您的问题中链接的图像类似的布局,但没有您提到的问题。
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();
}
}