gpt4 book ai didi

javafx - 如何使用复选框显示 javafx 中密码字段的内容

转载 作者:行者123 更新时间:2023-12-01 17:49:59 41 4
gpt4 key购买 nike

我是一名学习 java 和 javafx 的学生,如何使用复选框在密码字段中显示密码?我使用 gluon scenebuilder 作为我的 fxml 编辑器

最佳答案

上面列出了重复项,以提供正确但更复杂的方法。在这个答案中,我展示了两个例子。一个带有 CheckBox,另一个带有全视之眼。重点是使用 StackPane 对节点进行分层。对于 CheckBox 解决方案,在 StackPane 中放置一个 TextField,然后放置一个 PasswordField。当选中 CheckBox 时,将 TextField toFront 引入,并使用 PasswordField 设置其文本。当 CheckBox 未选中时清除 TextField,并将 PasswordField 移动到 toFront。对于全视之眼示例,使用相同的想法,但添加一个ImageView并始终保留ImageViewtoFront .

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class TestingGround extends Application
{
Image image = new Image("https://previews.123rf.com/images/andrerosi/andrerosi1905/andrerosi190500216/123158287-eye-icon-vector-look-and-vision-icon-eye-vector-icon.jpg");

@Override
public void start(Stage primaryStage)
{
HBox passwordControl1 = createPasswordFieldWithCheckBox();
HBox passwordControl2 = createPasswordFieldWithCheckBox();
StackPane passwordControl3 = createPasswordFieldWithEye();
StackPane passwordControl4 = createPasswordFieldWithEye();

VBox root = new VBox(passwordControl1, passwordControl2, passwordControl3, passwordControl4);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}

HBox createPasswordFieldWithCheckBox()
{
PasswordField passwordField = new PasswordField();
passwordField.setPrefHeight(50);
TextField textField = new TextField();
textField.setPrefHeight(50);
passwordField.textProperty().bindBidirectional(textField.textProperty());

StackPane stackPane = new StackPane(textField, passwordField);
CheckBox checkBox = new CheckBox();
checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (newValue) {
textField.toFront();
}
else {
passwordField.toFront();
}
});

HBox root = new HBox(stackPane, checkBox);
root.setSpacing(5);
root.setAlignment(Pos.CENTER);

return root;
}

StackPane createPasswordFieldWithEye()
{
PasswordField passwordField = new PasswordField();
passwordField.setPrefHeight(50);
TextField textField = new TextField();
passwordField.textProperty().bindBidirectional(textField.textProperty());
textField.setPrefHeight(50);
ImageView imageView = new ImageView(image);
imageView.setFitHeight(32);
imageView.setFitWidth(32);
StackPane.setMargin(imageView, new Insets(0, 10, 0, 0));
StackPane.setAlignment(imageView, Pos.CENTER_RIGHT);
imageView.setOnMousePressed((event) -> {
textField.toFront();
imageView.toFront();
});

imageView.setOnMouseReleased((event) -> {
passwordField.toFront();
imageView.toFront();
});

StackPane root = new StackPane(textField, passwordField, imageView);

return root;
}
}

关于javafx - 如何使用复选框显示 javafx 中密码字段的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60815695/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com