简体   繁体   English

当用户拖动调整舞台窗口大小时,如何让JavaFX节点(textarea,textfield)正确调整大小?

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

How to get JavaFX nodes (textarea, textfield) to resize correctly when user drags to resize the stage window? 当用户拖动调整舞台窗口大小时,如何让JavaFX节点(textarea,textfield)正确调整大小?

I have a piece of code that creates a stage VBox with two nodes (TextArea, TextField). 我有一段代码创建一个带有两个节点的阶段VBox(TextArea,TextField)。 However, when the user drags to resize the window, these components are not dragged in proportion. 但是,当用户拖动以调整窗口大小时,这些组件不会按比例拖动。 Please see the pictures: 请看图片:

在调整大小之前 调整大小后

Here is my code, any suggestions on how to implement a fix so that the textfield is always at the bottom, and textarea expands to fill up the white space? 这是我的代码,关于如何实现修复的任何建议,以便文本字段始终位于底部,并且textarea扩展以填充空白区域? Thanks! 谢谢!

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

With a VBox , the components will take just enough space, vertically, to fit. 使用VBox ,组件将在垂直方向上占据足够的空间以适应。 After that, increase in size of the Stage does not make a difference. 之后, Stage大小的增加并没有什么不同。

Use BorderPane . 使用BorderPane If you have used Swing, this is like BorderLayout . 如果你使用过Swing,这就像BorderLayout This will let you place your components on the borders of the Stage and at the center and these components will stay where they are even after resizing. 这样您就可以将组件放置在Stage边框和中心位置,这些组件即使在调整大小后也会保持原样。

SSCCE: 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