在JavaFX中,当用户调整窗口大小时,如何在hbox(BorderPane布局)中创建文本字段的宽度/长度调整大小?
答案 0 :(得分:11)
您可以将文本字段的HGROW
设置为Priority.ALWAYS
。
这将使TextField
在HBox
更改宽度时缩小/增长。
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);
}
}
输出