簡體   English   中英

當用戶拖動調整舞台窗口大小時,如何讓JavaFX節點(textarea,textfield)正確調整大小?

[英]How to get JavaFX nodes (textarea, textfield) to resize correctly when user drags to resize the stage window?

當用戶拖動調整舞台窗口大小時,如何讓JavaFX節點(textarea,textfield)正確調整大小?

我有一段代碼創建一個帶有兩個節點的階段VBox(TextArea,TextField)。 但是,當用戶拖動以調整窗口大小時,這些組件不會按比例拖動。 請看圖片:

在調整大小之前 調整大小后

這是我的代碼,關於如何實現修復的任何建議,以便文本字段始終位於底部,並且textarea擴展以填充空白區域? 謝謝!

Stage stage = new Stage();  
VBox root = new VBox();
textArea = new TextArea();
textField = new TextField();
root.getChildren().addAll(textArea, textField);
textArea.setStyle("-fx-background-color: DARKGRAY;"
    + "-fx-text-fill: BLACK;"
    + "-fx-font-size: 14pt;");
textArea.setPrefSize(400, 316);
textArea.setEditable(false);
textArea.setWrapText(true);
textField.setStyle("-fx-background-color: DARKGRAY;"
    + "-fx-text-fill: BLACK;"
    + "-fx-font-size: 14pt;");

使用VBox ,組件將在垂直方向上占據足夠的空間以適應。 之后, Stage大小的增加並沒有什么不同。

使用BorderPane 如果你使用過Swing,這就像BorderLayout 這樣您就可以將組件放置在Stage邊框和中心位置,這些組件即使在調整大小后也會保持原樣。

SSCCE:

package stack;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TextfieldAdjust extends Application {

    Scene scene;
    TextArea area;
    TextField field;
    BorderPane border;

    @Override
    public void start(Stage stage) throws Exception {
        border = new BorderPane();
        scene = new Scene(border);

        area = new TextArea();
        field = new TextField();

        area.setStyle("-fx-background-color: DARKGRAY;"
                + "-fx-text-fill: BLACK;"
                + "-fx-font-size: 14pt;");
        field.setStyle("-fx-background-color: WHEAT;"
                + "-fx-text-fill: BLACK;"
                + "-fx-font-size: 14pt;");

        border.setCenter(area);
        border.setBottom(field);

        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }
    public static void main(String[] args) {
        Application.launch("stack.TextFieldAdjust");
    }
}

暫無
暫無

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

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