密码字段圆圈非常大

huangapple go评论62阅读模式
英文:

Password field circles very large

问题

当我在JavaFx中使用密码字段时,遮挡输入的圆圈非常大。可以调整大小吗?是哪个属性?

密码字段圆圈非常大

英文:

When I use the password field in JavaFx, the circles masking the input are very large. Can the size be adjusted? Which property is it?

密码字段圆圈非常大

答案1

得分: 4

掩码字符被定义为TextInputControlSkin类中的一个最终静态字段(定义为static final char BULLET = ''\u25cf''),并调用maskText()方法来确定要显示的文本。这是一个protected方法,因此您可以覆盖它以更改掩码字符(或整个策略,如果您希望)。

最简单的方法可能是:

package org.jamesd.examples.password;

import javafx.scene.control.PasswordField;
import javafx.scene.control.skin.TextFieldSkin;

public class PasswordFieldSkin extends TextFieldSkin {

    private static final String BULLET = "\u2022";

    public PasswordFieldSkin(PasswordField control) {
        super(control);
    }

    @Override
    protected String maskText(String text) {
        return BULLET.repeat(text.length());
    }
}

这使用了较小的黑点\u2022,对应HTML实体为•。默认值是\u25cf,对应HTML实体为●。当然,也可能有其他选项。

一个示例用法是:

package org.jamesd.examples.password;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) {
        PasswordField standardPasswordField = new PasswordField();
        PasswordField smallBulletPasswordField = new PasswordField();
        smallBulletPasswordField.setSkin(new PasswordFieldSkin(smallBulletPasswordField));
        VBox fields = new VBox(5, standardPasswordField, smallBulletPasswordField);
        fields.setAlignment(Pos.CENTER);
        fields.setPadding(new Insets(40));
        Scene scene = new Scene(fields);
        stage.setScene(scene);
        stage.show();
    }

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

如果您想在应用程序中的所有密码字段上使用这个(这是有道理的),您可以在CSS中指定皮肤:

.password-field {
    -fx-skin: "org.jamesd.examples.password.PasswordFieldSkin";
}

然后上面的皮肤类将变为默认值(显然根据需要调整为您自己的包名)。

英文:

The masking character is defined as a final static field in the TextInputControlSkin class (it is defined as static final char BULLET = '\u25cf';), and the maskText() method is called to determine the text to be displayed. This is a protected method, so you can override it to change the masking character (or entire strategy, if you wish).

A minimal approach would be something like:

package org.jamesd.examples.password;

import javafx.scene.control.PasswordField;
import javafx.scene.control.skin.TextFieldSkin;

public class PasswordFieldSkin extends TextFieldSkin {

    private static final String BULLET = "\u2022";


    public PasswordFieldSkin(PasswordField control) {
        super(control);
    }

    @Override
    protected String maskText(String text) {
        return BULLET.repeat(text.length());
    }
}

This uses a smaller bullet, \u2022, which is •. The default is \u25cf, which is ●. Other options are of course possible.

An example usage is:

package org.jamesd.examples.password;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) {
        PasswordField standardPasswordField = new PasswordField();
        PasswordField smallBulletPasswordField = new PasswordField();
        smallBulletPasswordField.setSkin(new PasswordFieldSkin(smallBulletPasswordField));
        VBox fields = new VBox(5, standardPasswordField, smallBulletPasswordField);
        fields.setAlignment(Pos.CENTER);
        fields.setPadding(new Insets(40));
        Scene scene = new Scene(fields);
        stage.setScene(scene);
        stage.show();
    }

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

If you want to use this for all password fields in the application (which would make sense), you can specify the skin in CSS:

.password-field {
    -fx-skin: "org.jamesd.examples.password.PasswordFieldSkin";
}

and then the skin class above will become the default (obviously adjust to your own package name as needed).

huangapple
  • 本文由 发表于 2023年7月6日 20:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定