如何使我自己的文本字段成为永久的键监听器?爪哇

时间:2019-03-21 10:27:57

标签: java javafx keylistener

我有问题,我尝试寻找一些解决方案,但是没有成功。我创建了自己的课Textfield

public class TextField extends Group {
private javafx.scene.control.TextField textField;
private Label label;
private Rectangle border;


TextField(String blowzyText, int width, int height, boolean password){

    String text = addSpaces(blowzyText);

    if(password) textField = new PasswordField();
    else textField = new javafx.scene.control.TextField();
    label = new Label(text);
    border = new Rectangle();

    getChildren().add(border);
    getChildren().add(label);
    getChildren().add(textField);

    textField.setPrefSize(width,height);
    textField.setMaxSize(width,height);
    textField.setMinSize(width,height);
    textField.setFont(Font.font("Open Sans", 25));
    textField.setStyle("-fx-text-fill: white;");

    textField.setBackground(Background.EMPTY);

    textField.setOnKeyPressed(e->{
        if(textField.getCharacters().length() == 1){
            if(e.getCode() == KeyCode.DELETE){
                if(textField.getCaretPosition() == 0){
                    label.setText(text);
                }
            }
            else if(e.getCode() == KeyCode.BACK_SPACE){
                if(textField.getCaretPosition() == 1){
                    label.setText(text);
                }
            }

        }
        else if(textField.getCharacters().length() == 0 && (e.getText().length() > 0)){
            if(e.getCode() != KeyCode.TAB) {
                label.setText(" ");
            }
        }
        else if((textField.getSelectedText().length() == textField.getCharacters().length()) && ((e.getCode() == KeyCode.DELETE) || (e.getCode() == KeyCode.BACK_SPACE))){
            label.setText(text);
        }
    });

    label.setPrefSize(width,height);
    label.setMaxSize(width,height);
    label.setMinSize(width,height);
    label.setTextFill(Color.WHITE);
    label.setFont(Font.font("Open Sans", 25));

    border.setHeight(height);
    border.setWidth(width);
    border.setStroke(Color.WHITE);
    border.setStrokeWidth(1.5);
    border.setFill(Color.TRANSPARENT);
    border.setArcHeight(40);
    border.setArcWidth(40);

}
private String addSpaces(String text){
    StringBuilder str = new StringBuilder(text);
    StringBuilder space = new StringBuilder("  ");
    return space.append(str).toString();
}
public String getText(){
    return textField.getCharacters().toString();
}
public javafx.scene.control.TextField getClickable(){
    return textField;
}

在其他类中,我创建一个对象

TextField name = new TextField("Login", width, height, false);
    name.getClickable().setOnKeyPressed(e1->{
        if(e1.getCode() == KeyCode.ENTER) {
            username = name.getText();
            System.out.println(username);
            password = passField.getText();
            System.out.println(password);
            menu.showScene();
        }
    });

但是当我在此类中制作keylistener时,它将覆盖Tetxfield类中的keylistener,而我在Textfield中创建的函数不起作用。并且我想在Textfield类中创建永久性的keylistener,并且在需要在keylistener的相同动作旁边添加时,将其添加到我创建Textfield对象的类中。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

addEventHandler允许您为一个事件类型添加多个事件处理程序。

textField.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
    if(textField.getCharacters().length() == 1){
        if(e.getCode() == KeyCode.DELETE){
            if(textField.getCaretPosition() == 0){
                label.setText(text);
            }
        }
        else if(e.getCode() == KeyCode.BACK_SPACE){
            if(textField.getCaretPosition() == 1){
                label.setText(text);
            }
        }

    }
    else if(textField.getCharacters().length() == 0 && (e.getText().length() > 0)){
        if(e.getCode() != KeyCode.TAB) {
            label.setText(" ");
        }
    }
    else if((textField.getSelectedText().length() == textField.getCharacters().length()) && ((e.getCode() == KeyCode.DELETE) || (e.getCode() == KeyCode.BACK_SPACE))){
        label.setText(text);
    }
});