有没有办法在javafx中取消焦点? [英] Is there a way to take away focus in javafx?

查看:966
本文介绍了有没有办法在javafx中取消焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以通过执行 node.requestFocus(); 来关注javafx中的节点,但是有没有办法从javafx中的节点中取消焦点或防止焦点对象解决方案我不认为有任何保证,这将始终工作,但你可以尝试把重点放在一个固有的东西不接受键盘输入(例如布局窗格):

  import javafx.application.Application; 
导入javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class NoFocusTest extends Application {

@Override
public void start(Stage primaryStage){
TextField tf1 = new TextField();
tf1.setPromptText(Enter something);
TextField tf2 = new TextField();
tf2.setPromptText(Enter something else);
VBox root = new VBox(5,tf1,tf2);
primaryStage.setScene(new Scene(root,250,150));
primaryStage.show();
root.requestFocus();
}
}


I know that your can give focus to a node in javafx by doing node.requestFocus(); but is there a way to take away focus from a node in javafx or prevent focus on an object?

解决方案

I don't think there's any guarantee this will always work, but you can try setting focus to something that inherently doesn't accept keyboard input (such as a layout pane):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class NoFocusTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField tf1 = new TextField();
        tf1.setPromptText("Enter something");
        TextField tf2 = new TextField();
        tf2.setPromptText("Enter something else");
        VBox root = new VBox(5, tf1, tf2);
        primaryStage.setScene(new Scene(root, 250, 150));
        primaryStage.show();
        root.requestFocus();
    }
}

这篇关于有没有办法在javafx中取消焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆