如果我以不同的方式实例化它们 2 次,为什么这 2 个组件具有相同的行为?

Why does these 2 components have the same behavior if i am instantiating them 2 times differently?

声明如下:

@FXML
private CustomTextField txtFieldUsuario;

@FXML
private CustomPasswordField txtFieldPassword;

这里是初始化:

@Override
public void initialize(URL location, ResourceBundle resources) {
    new TextFieldUtil(txtFieldUsuario).clearable();

    new PasswordFieldUtil(txtFieldPassword).clearable();

    adminLogin();

    imageView.setImage(new Image("/foto/logo_fix.png"));

    Icono.agregarImagenBoton(btnIniciarSesion, Icono.INICIAR_SESION);

    enterKeyPressed();
}

这是 TextFieldUtil class:

public class TextFieldUtil extends CustomTextField{
    private static final Logger LOGGER = LogManager.getLogger();

    private CustomTextField[] textFields;

    public TextFieldUtil(CustomTextField... textFields) {
        this.textFields = textFields;
    }

    public void propiedades() {

    }

    public void clearable() {
        for (CustomTextField customTextField : textFields) {
            customTextField.setRight(Icono.BORRAR_TEXTFIELDS);

            customTextField.getRight().autosize();

            customTextField.getRight().setCursor(Cursor.HAND);

            customTextField.textProperty().addListener((observable, oldValue, newValue) -> {
                if (customTextField.getText().isEmpty()) {

                    customTextField.getRight().setVisible(false);

                } else {

                    customTextField.getRight().setVisible(true);

                    customTextField.getRight().addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
                        if (event.isPrimaryButtonDown() && !customTextField.getText().isEmpty() && customTextField.getRight().isVisible()) {
                            customTextField.setText("");
                        }
                    });
                }
            });
        }
    }

    public void mascara(TextFieldMascara textFieldMascara) {
        switch (textFieldMascara) {
            case TELEFONO:
                break;
            case RNC:
                break;
            case PRECIO:
                break;
        }
    }
}

这是 PasswordUtil class:

public class PasswordFieldUtil extends CustomPasswordField{
    private static final Logger LOGGER = LogManager.getLogger();

    private CustomPasswordField[] passwordFields;

    public PasswordFieldUtil(CustomPasswordField... passwordFields) {
        this.passwordFields = passwordFields;
    }

    public void propiedades() {

    }

    public void clearable() {
        for (CustomPasswordField customPasswordField : passwordFields) {
            customPasswordField.setRight(Icono.BORRAR_TEXTFIELDS);

            customPasswordField.getRight().autosize();

            customPasswordField.getRight().setCursor(Cursor.HAND);

            customPasswordField.textProperty().addListener((observable, oldValue, newValue) -> {
                if (customPasswordField.getText().isEmpty()) {

                    customPasswordField.getRight().setVisible(false);

                } else {

                    customPasswordField.getRight().setVisible(true);

                    customPasswordField.getRight().addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
                        if (event.isPrimaryButtonDown() && !customPasswordField.getText().isEmpty() && customPasswordField.getRight().isVisible()) {

                            customPasswordField.setText("");

                        }
                    });
                }
            });
        }
    }
}

这是带有结果的图像,仅包含图像的第二部分。当我在第二个组件中单击它时,被擦除的组件是第一个组件 (CustomTextField)。

Here is the image

如果有人能告诉我为什么会这样,我将不胜感激。

您在此代码中的场景图中设置了相同的 Node 实例。因此,MouseEvent.MOUSE_PRESSED 的事件过滤器设置为相同的图标。

customTextField.setRight(Icono.BORRAR_TEXTFIELDS);

customPasswordField.setRight(Icono.BORRAR_TEXTFIELDS);

尝试更改代码例如:

customTextField.setRight(new ImageView( yourImage ));

customPasswordField.setRight(new ImageView( yourImage ));