Javafx 8 单击选项卡内容时,如何停止焦点转到选项卡?

Javafx 8 单击选项卡内容时,如何停止焦点转到选项卡?,javafx-8,Javafx 8,下面的程序在选项卡中显示按钮和标签。该按钮的焦点是启动。如果单击内容区域中除按钮外的任何位置,按钮将失去焦点,选项卡将获得焦点 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.input.MouseEvent; import javafx.scene.layout.FlowPane; import javafx.

下面的程序在选项卡中显示按钮和标签。该按钮的焦点是启动。如果单击内容区域中除按钮外的任何位置,按钮将失去焦点,选项卡将获得焦点

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class FocusTest  extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        TabPane tabPane = new TabPane();
        tabPane.setFocusTraversable(false);
        Scene scene = new Scene(tabPane, 500, 500);
        stage.setScene(scene);
        Tab tab = new Tab("Tab 1");
        FlowPane contentPane = new FlowPane();
        /*contentPane.setOnMousePressed(MouseEvent::consume);*/
        tab.setContent(contentPane);
        tabPane.getTabs().addAll(tab, new Tab("Tab 2"));
        Button button = new Button("Button");
        Label label = new Label("Label");
        contentPane.getChildren().addAll(button, label);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
我认为当点击标签内容区域时,按钮不应该失去焦点。我仍然希望能够通过单击选项卡的名称/文本位并使用快捷键更改选项卡来聚焦选项卡

我找不到使用Tab或TabPane的方法,但如果我在内容窗格上添加一个鼠标按下的侦听器(请参阅上面的注释代码),我确实可以获得我需要的行为,但感觉有点像黑客。我必须在选项卡窗格中的每个项目上设置一个侦听器

所以我想我的问题是,有没有更好的方法


我为此提出了一个问题:


谢谢

单击contentPane(FlowPane)时,您可以请求对按钮进行聚焦。这里是您的代码的修改版本:

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.FlowPane;
    import javafx.stage.Stage;

    public class FocusTest  extends Application {
@Override
public void start(Stage stage) throws Exception {
    TabPane tabPane = new TabPane();
    tabPane.setFocusTraversable(false);
    Scene scene = new Scene(tabPane, 500, 500);
    stage.setScene(scene);
    final Button button = new Button("Button");// I moved this here and added final
    Tab tab = new Tab("Tab 1");
    FlowPane contentPane = new FlowPane();
    contentPane.setOnMouseClicked(new EventHandler<MouseEvent>() {//I think this is what you are looking for
        public void handle(MouseEvent event) {
            button.requestFocus();
        }
    });                          
    tab.setContent(contentPane);
    tabPane.getTabs().addAll(tab, new Tab("Tab 2"));

    Label label = new Label("Label");
    contentPane.getChildren().addAll(button, label);
    stage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}
}
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.FlowPane;
导入javafx.stage.stage;
公共类FocusTest扩展了应用程序{
@凌驾
public void start(Stage)引发异常{
TabPane TabPane=新建TabPane();
tabPane.setFocusTraversable(false);
场景=新场景(选项卡窗格,500500);
舞台场景;
final Button Button=new Button(“Button”);//我把这个移到这里并添加了final
Tab Tab=新选项卡(“选项卡1”);
FlowPane contentPane=新的FlowPane();
setOnMouseClicked(neweventHandler(){//我想这就是您要查找的内容
公共无效句柄(MouseeEvent事件){
requestFocus()按钮;
}
});                          
tab.setContent(contentPane);
tabPane.getTabs().addAll(制表符,新制表符(“制表符2”);
标签=新标签(“标签”);
contentPane.getChildren().addAll(按钮、标签);
stage.show();
}
公共静态void main(字符串[]args){
应用程序启动(args);
}
}
如果您有任何问题或想知道如何使用多个按钮或其他对象注释执行相同操作

我希望这对你有帮助

感谢您的帮助:-)只是,这并不能解决问题(按钮根本不应该失去焦点)。在OPs代码中注册mouseListener可以很好地实现这一效果,如果Focus从未丢失,则无需显式地请求Focus返回。。。