您的位置:首页 > 编程语言 > Java开发

javafX初探(密码域)

2015-02-04 15:39 337 查看
本章我们介绍密码域的使用,典型的密码域如下图:



创建密码域

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);
	}
}


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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: