javafX初探(密码域)

52 篇文章 0 订阅
47 篇文章 4 订阅

本章我们介绍密码域的使用,典型的密码域如下图:

创建密码域

 

PasswordField passwordField = new PasswordField();
passwordField.setPromptText("Your password");


 

我们可以使用一个提示性的语句来,标识这是一个密码域,或者我们可以使用一个Label。当然密码域也有setText方法。我们可以使用这个方法为密码域赋值。但是密码域中的内容只显示点,不显示具体内容。

我们可以使用getText方法来获取用户输入的密码;

验证密码

package com.chu.button;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class PasswordFiledTest extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		final Label message = new Label("");

		VBox vb = new VBox();
		vb.setPadding(new Insets(10, 0, 0, 10));
		vb.setSpacing(10);
		HBox hb = new HBox();
		hb.setSpacing(10);
		hb.setAlignment(Pos.CENTER_LEFT);

		Label label = new Label("Password");
		final PasswordField pb = new PasswordField();

		pb.setOnAction((ActionEvent e) -> {
		    if (!pb.getText().equals("aaa")) {
		        message.setText("Your password is incorrect!");
		        message.setTextFill(Color.rgb(210, 39, 30));
		    } else {
		        message.setText("Your password has been confirmed");
		        message.setTextFill(Color.rgb(21, 117, 84));
		    }
		    pb.clear();
		});

		hb.getChildren().addAll(label, pb);
		vb.getChildren().addAll(hb, message);

		
		Scene s  = new Scene(vb,300,200);
		primaryStage.setScene(s);
		
		primaryStage.show();
		
	}

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


输入密码后,按回车键,如下图所示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值