JavaFX使用Window调整TextField大小

时间:2015-06-23 20:53:20

标签: javafx resize textfield

在JavaFX中,当用户调整窗口大小时,如何在hbox(BorderPane布局)中创建文本字段的宽度/长度调整大小?

1 个答案:

答案 0 :(得分:11)

您可以将文本字段的HGROW设置为Priority.ALWAYS

这将使TextFieldHBox更改宽度时缩小/增长。

MCVE:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextField textField = new TextField();

        HBox container  = new HBox(textField);
        container.setAlignment(Pos.CENTER);
        container.setPadding(new Insets(10));

        // Set Hgrow for TextField
        HBox.setHgrow(textField, Priority.ALWAYS);

        BorderPane pane = new BorderPane();
        pane.setCenter(container);
        Scene scene = new Scene(pane, 150, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

输出

enter image description here