簡體   English   中英

JavaFX 2 BorderPane使用完整空間

[英]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);
}

好的,因此這里有些錯誤,我將嘗試為它們提供一些建議和示例解決方案。

我嘗試在BorderPane中放置一個包含TextField和HTML-Editor的vBox,但是沒有使用全部空間。

您需要使用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被實現為GridPane,其中包含工具欄和可編輯的WebView。 默認情況下,WebView的首選大小為800x600。 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);

我縮小窗口,html編輯器與我的左選項窗口重疊。

在BorderPane中明確設置中心窗格的最小寬度設置,以使其不會溢出到外部邊緣窗格上。

editorBox.setMinSize(0, 0);

您需要這樣做,因為BorderPane文檔指出:

BorderPane默認情況下不會裁剪其內容,因此,如果孩子的最小尺寸使其無法適應其空間,則孩子的邊界可能會超出其自身的邊界。


順便說一句,代碼中的查找調用是可疑的。 通常,您無法通過CSS ID查找節點,直到將該節點添加到顯示的舞台上的活動場景並且JavaFX系統有機會在該節點上運行CSS布局遍歷為止-否則查找將僅返回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();
  }
}
    GridPane gridPane = (GridPane) editor.getChildrenUnmodifiable().get(0);
    RowConstraints row1 = new RowConstraints();
    row1.setVgrow(Priority.NEVER);
    RowConstraints row2 = new RowConstraints();
    row2.setVgrow(Priority.NEVER);
    RowConstraints row3 = new RowConstraints();
    row3.setVgrow(Priority.ALWAYS);
    gridPane.getRowConstraints().addAll(row1, row2, row3);

如何使用FXML設置ScrollPane並調整其大小:

package anchorscroll;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

public class FXMLDocumentController implements Initializable {

    private Label label;
    @FXML
    private VBox vp;
    @FXML
    private ScrollPane sp;
    @FXML

    private Pane panel;
    @FXML
    private double Width;
    @FXML
    private double Height;

    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        Width = panel.getWidth();
        Height = panel.getHeight();
        sp.setPrefWidth(Width - 60);
        sp.setPrefHeight(Height - 60);

        panel.widthProperty().addListener(p -> {
            Width = panel.getWidth();
            Height = panel.getHeight();
            System.out.println("Width " + Width + " Height " + Height);
            sp.setPrefWidth(Width - 60);
            sp.setPrefHeight(Height - 60);
        });

        panel.heightProperty().addListener(p -> {
            Width = panel.getWidth();
            Height = panel.getHeight();
            System.out.println("Width " + Width + " Height " + Height);
            sp.setPrefWidth(Width - 60);
            sp.setPrefHeight(Height - 60);
        });

        VBox v = new VBox();
        for (int i = 0; i < 100; i++) {
            v.getChildren().add(new Button("Hello " + i));
        }
        sp.setContent(v);

        VBox p = new VBox();
        try {
            p = FXMLLoader.load(getClass().getResource("NewMessage.fxml")); // "/fxml/Register.fxml"
            sp.setContent(p);
        } catch (IOException ex) {
            Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

!!! NewMessage.fxml需要從VBox面板開始!

如果您不需要加載FXML,請注釋或刪除以下兩行:

p = FXMLLoader.load(getClass().getResource("NewMessage.fxml"));
sp.setContent(p);

和布局:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<Pane fx:id="panel" prefHeight="400.0" prefWidth="600.0" stylesheets="@Styles.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="anchorscroll.FXMLDocumentController">
    <children>
        <HBox prefHeight="60.0" prefWidth="600.0">
            <children>
                <Label text="Label" />
            </children>
        </HBox>
        <VBox fx:id="vp" layoutY="60.0" prefHeight="540.0" prefWidth="60.0">
            <children>
                <Button mnemonicParsing="false" prefWidth="50.0" text="Button" />
            </children>
        </VBox>
        <ScrollPane fx:id="sp" fitToHeight="true" fitToWidth="true" layoutX="60.0" layoutY="60.0" prefHeight="340.0" prefWidth="540.0" vbarPolicy="ALWAYS" />
    </children>
</Pane>

布局圖像(topBar,leftBar,scrollContentPane)

我也面臨着同樣的問題。 我的borderPane內部的anchorPane沒有裝滿。 為prefSize設置一個較大的值對我有用。 因此,當邊框窗格增大時,它將嘗試為其子級提供盡可能大的尺寸

anchorPane.setMinSize(25, 25);
anchorPane.setPrefSize(25000, 25000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM