每当按下SHIFT键(Javafx)时,TextField都会丢失焦点?

时间:2016-01-07 05:37:04

标签: javafx javafx-2 javafx-8

我已经尝试了下面给出的代码来改变对按键的关注。

package focusdemo1;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author vikassingh
 */
public class FocusDemo1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField username = new TextField();
        TextField password = new TextField();

        VBox mm = new VBox();
        mm.getChildren().addAll(username, password);

        StackPane root = new StackPane();
        root.getChildren().addAll(mm);

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

        root.requestFocus();
        // Key Listner
        root.setOnKeyPressed(new EventHandler<KeyEvent>() {

            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.DOWN) {
                    username.requestFocus();
                    event.consume();
                } else {
                    try {
                        keyAction(event.getCode().toString(), root, primaryStage);
                    } catch (Exception ex) {
                        Logger.getLogger(FocusDemo1.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }
        });
        username.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.DOWN) {
                    password.requestFocus();
                    event.consume();
                }
            }
        });

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

    }

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

    // F1 & Esc Key Action
    public void keyAction(String keyCode, StackPane root, Stage rootStage) throws Exception {
        root.requestFocus();
        if (keyCode.equalsIgnoreCase("F1")) {
            root.getChildren().removeAll();
            //LoginSettings primaryPage = new LoginSettings(root, rootStage);
            root.requestFocus();
        }
        if (keyCode.equalsIgnoreCase("F2")) {
            root.getChildren().removeAll();
            //ForgotPassword forgotPasswordPage = new ForgotPassword(root, rootStage);
            root.requestFocus();
        }
        if (keyCode.equalsIgnoreCase("ESCAPE")) {
            if (!root.getId().toString().equalsIgnoreCase("LoginPage")) {
                root.getChildren().removeAll();
                // Login loginPage = new Login(root, rootStage);
                root.requestFocus();
            }
        }
    }
    // End F1 & Esc Key Action

}

但我现在面临的问题是,每当我按下SHIFT键时,焦点都会从Textfield中删除。例如。我试图输入“Vikas”,其中Capital“V”(SHIFT + V)。

1 个答案:

答案 0 :(得分:0)

问题是你的keyAction函数中第一次调用root.requestFocus()。尝试对其进行评论并尝试一下。