Mar*_*son 12 utf-8 password-protection textfield javafx-2
在我的用户界面中,我有一个像这样的密码字段(urm底部的那个!):
我希望用户能够检查您在图片中看到的复选框,并显示所有"秘密"密码字符.与我们从许多现代密码请求UI中获得的选项没什么不同:s漂浮在周围.但是,我在JavaFX API中找不到任何可以让我这样做的东西?
如果我的担心成立,那么我想用一个TextField
显示最后一个键只按下半秒或直到按下下一个键,然后他将屏蔽所有以前的用户输入.这将产生一种很酷的动画效果,人们可以在现代UI中看到它们:s.但是,有没有办法让我掌握OS依赖(我认为它是操作系统依赖??)我应该使用的密码回音字符?
如果无法获得与操作系统相关的字符,那么我很乐意使用您在图片上看到的字符(Windows 8计算机上的JavaFX).这个陌生人的UTF-8代码点是什么?
Ulu*_*Biy 31
>但是,我在JavaFX API中找不到任何可以让我这样做的东西?
PasswordField
默认情况下,组件不显示蒙版文本.然而,你可以使用PasswordField
与TextField
和切换分别使用这些组件屏蔽/非屏蔽文字.未显示文本的位置TextField
,如下面的示例演示.
>我想使用一个TextField来显示最后一个按键仅半秒或按下下一个键,然后他将屏蔽所有以前的用户输入.
因为PasswordField
,本身就是一个扩展版本TextField
.您始终可以使用您提到的属性构建自己的自定义密码文本框.
>有没有办法让我掌握OS依赖(我认为这是OS依赖?)我应该使用密码回音字符?
坦率地说,没有抓住你在这里说的话.您可以通过添加更改侦听器PasswordField.textPrperty()
和执行动画,计时器等来跟踪文本更改.您可以通过扩展PasswordFieldSkin
和使用CSS 来覆盖默认子弹掩码-fx-skin
.在这里查看bullet 源代码的定义:
public class PasswordFieldSkin extends TextFieldSkin {
public static final char BULLET = '\u2022';
public PasswordFieldSkin(PasswordField passwordField) {
super(passwordField, new PasswordFieldBehavior(passwordField));
}
@Override protected String maskText(String txt) {
TextField textField = getSkinnable();
int n = textField.getLength();
StringBuilder passwordBuilder = new StringBuilder(n);
for (int i=0; i<n; i++) {
passwordBuilder.append(BULLET);
}
return passwordBuilder.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这里是使用绑定显示密码字符的启动演示应用程序:
@Override
public void start(Stage primaryStage) {
// text field to show password as unmasked
final TextField textField = new TextField();
// Set initial state
textField.setManaged(false);
textField.setVisible(false);
// Actual password field
final PasswordField passwordField = new PasswordField();
CheckBox checkBox = new CheckBox("Show/Hide password");
// Bind properties. Toggle textField and passwordField
// visibility and managability properties mutually when checkbox's state is changed.
// Because we want to display only one component (textField or passwordField)
// on the scene at a time.
textField.managedProperty().bind(checkBox.selectedProperty());
textField.visibleProperty().bind(checkBox.selectedProperty());
passwordField.managedProperty().bind(checkBox.selectedProperty().not());
passwordField.visibleProperty().bind(checkBox.selectedProperty().not());
// Bind the textField and passwordField text values bidirectionally.
textField.textProperty().bindBidirectional(passwordField.textProperty());
VBox root = new VBox(10);
root.getChildren().addAll(passwordField, textField, checkBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)
您需要创建三个元素:
您将密码字段放在相同的位置 (x, y):
<PasswordField fx:id="pass_hidden" layoutX="X" layoutY="Y" />
<TextField fx:id="pass_text" layoutX="X" layoutY="Y"/>
<CheckBox fx:id="pass_toggle" onAction="#togglevisiblePassword" .... />
Run Code Online (Sandbox Code Playgroud)
注:替代的价值X
和Y
。
在您的控制器中添加:
@FXML
private TextField pass_text;
@FXML
private CheckBox pass_toggle;
@FXML
private Button btn_start_stop;
/**
* Controls the visibility of the Password field
* @param event
*/
@FXML
public void togglevisiblePassword(ActionEvent event) {
if (pass_toggle.isSelected()) {
pass_text.setText(pass_hidden.getText());
pass_text.setVisible(true);
pass_hidden.setVisible(false);
return;
}
pass_hidden.setText(pass_text.getText());
pass_hidden.setVisible(true);
pass_text.setVisible(false);
}
//Run
@Override
public void initialize(URL location, ResourceBundle resources) {
this.togglevisiblePassword(null);
}
Run Code Online (Sandbox Code Playgroud)
如果你想知道密码的值,你可以创建一个返回它的方法:
private String passwordValue() {
return pass_toggle.isSelected()?
pass_text.getText(): pass_hidden.getText();
}
Run Code Online (Sandbox Code Playgroud)