有没有办法防止新的阶段偷走主阶段的焦点?
我的意思是,每一个新的stage.show();
都抢走了我主要舞台上的焦点。
我不想把我的JavaFX和Swing混在一起,所以在JFrame中没有嵌入内容的选项。此外,如果不使用任何弹出窗口,只使用纯Stage,那就太好了。
有没有什么外部库允许我这样做?
发布于 2018-06-02 04:38:46
您可以将侦听器添加到主阶段的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();
}
}
https://stackoverflow.com/questions/50650542
复制相似问题