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

查看:384
本文介绍了当用户拖动调整舞台窗口大小时,如何让JavaFX节点(textarea,textfield)正确调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一段代码可以创建一个舞台VBox有两个节点(TextArea,TextField)。但是,当用户拖动以调整窗口大小时,这些组件不会按比例拖动。请参阅图片:

I have a piece of code that creates a stage VBox with two nodes (TextArea, TextField). However, when the user drags to resize the window, these components are not dragged in proportion. Please see the pictures:

这是我的代码,有关如何实现修复的任何建议,以便文本字段始终位于底部,并且textarea扩展以填充空白区域?谢谢!

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


推荐答案

使用 VBox ,组件将占据足够的空间,垂直,以适应。之后, Stage 的大小增加并没有什么区别。

With a VBox, the components will take just enough space, vertically, to fit. After that, increase in size of the Stage does not make a difference.

使用 BorderPane 。如果您使用过Swing,这就像 BorderLayout 。这样您就可以将组件放在 Stage 的边框上,并在中心放置,这些组件在调整大小后仍将保持原样。

Use BorderPane. If you have used Swing, this is like 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.

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

这篇关于当用户拖动调整舞台窗口大小时,如何让JavaFX节点(textarea,textfield)正确调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆