繁体   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