JavaFx:12、输入框,密码框,标签,实用技术

package com.fx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField textField = new TextField();
        textField.setPromptText("请输入XXX");
        textField.setLayoutX(100);
        textField.setLayoutY(100);
        textField.setFocusTraversable(false);

        Tooltip tooltip = new Tooltip();
        tooltip.setText("这个文本输入框的提示框");
        textField.setTooltip(tooltip);

        // 监听文本更改
        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if (newValue.length()>7){
                    textField.setText(oldValue);
                }
            }
        });

        // 监听文本选择
        textField.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();

        Label label = new Label();
        label.setText("我是标签:");


        Group group = new Group();

        group.getChildren().addAll(textField,passwordField);
        group.getChildren().add(label);

        Scene scene = new Scene(group);


        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();
        primaryStage.centerOnScreen();
    }
}
posted @ 2020-10-01 21:40  xl4ng  阅读(544)  评论(0编辑  收藏  举报