输入框
创建一个简单文本输入框
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
TextField text = new TextField();
text.setLayoutX(100);
text.setLayoutY(100);
text.setFont(Font.font(14));
text.setText("这是文本");
root.getChildren().add(text);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show( );
}
框框的提示
所有node都有
Tooltip tooltip = new Tooltip();
tooltip.setText("这是提示");
textField.setTooltip(tooltip);
警告
text.setPromptText("请输入7个字已下");
对输入的文本进行监听
text.textProperty().addListener(new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if(newValue.length()>7){ text.setText(oldValue);
}
}
});
对光标进行监听
text.selectedTextProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(newValue);
}
});
密码框
PasswordField passwordField = new PasswordField();
passwordField.setLayoutX(200);
passwordField.setLayoutY(200);
root.getChildren().add(passwordField);
添加标签
Label label = new Label("我是标签");
label.setLayoutX(200);
label.setLayoutY(200);
label.setFont(Font.font(14));
root.getChildren().add(label);
Hbox(水平布局)横着来得
AnchorPane ap = new AnchorPane();
Button b1 = new Button("btn1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
ap.setStyle("-fx-background-color: #AEEEEE");
HBox hBox = new HBox();
hBox.setStyle("-fx-background-color: #E066FF");
hBox.setPrefWidth(400);
hBox.setPrefHeight(400);
hBox.getChildren().addAll(b1,b2,b3);
ap.getChildren().add(hBox);
Scene scene = new Scene(ap);
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show( );
vbox竖着来的
Button b1 = new Button("btn1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
ap.setStyle("-fx-backround-color: #AEEEEE");
VBox hBox = new VBox();
hBox.setStyle("-fx-background-color: #E066FF");
hBox.setPrefWidth(400);
hBox.setPrefHeight(400);
hBox.getChildren().addAll(b1,b2,b3);
ap.getChildren().add(hBox);
Scene scene = new Scene(ap);
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show( );
有很多位置设置
hBox.setAlignment(Pos.CENTER_LEFT);