JavaFX修改TextField输入文本框的长度

JavaFX:修改TextField输入文本框的长度


使用setPrefWidth(double value)可以实现javaFx内TextField的长度修改,代码如下:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class MyController extends Application {
  public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);

    TextField notification = new TextField();
    notification.setText("Label");
    notification.setPrefWidth(50.0);
  
    notification.clear();

    GridPane grid = new GridPane();
    notification.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
    grid.setVgap(4);
    grid.setHgap(10);
    grid.setPadding(new Insets(5, 5, 5, 5));
    grid.add(new Label("城市: "), 0, 0);
    grid.add(notification, 1, 0);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(grid);
    stage.setScene(scene);
    stage.show();
  }
}

你可能感兴趣的:(桌面应用)