我知道您可以通过执行 node.requestFocus(); 将焦点放在 javafx 中的节点,但是有没有办法从 javafx 中的节点上移开焦点或防止焦点放在对象上?
最佳答案
我认为不能保证这将始终有效,但您可以尝试将焦点设置到本质上不接受键盘输入的对象(例如布局 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();
}
}
关于java - 有没有办法取消 javafx 中的焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25431959/