有什么方法可以防止新舞台从主舞台窃取焦点吗?
我的意思是,每stage.show();
都会从我的主舞台上抢走焦点。
我不想将我的JavaFX与Swing混合使用,因此没有将内容嵌入JFrame的选项。 此外,不使用任何弹出窗口,只是使用纯舞台会很棒。
是否有任何外部库允许我这样做?
您可以将侦听器添加到主阶段的focusedProperty
,并在侦听器失去焦点时请求焦点。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class StageFocus extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
final Button button = new Button("New Stage");
button.setOnAction(e -> {
final Stage stage = new Stage();
stage.setWidth(200);
stage.setHeight(200);
stage.setTitle("New Stage");
stage.show();
});
final Scene scene = new Scene(new StackPane(button), 300, 300);
primaryStage.focusedProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue) {
primaryStage.requestFocus();
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
}