【发布时间】:2016-01-14 02:00:54
【问题描述】:
JavaFX 存在问题,当我弹出一个新阶段时,该新窗口将从任何具有当前焦点的 Windows 应用程序中获取焦点
我希望它弹出到前面,但不聚焦,所以如果用户在其他地方输入,他们可以继续输入等等。
在 Swing 中,您可以通过以下方式解决此问题:
dialog.setFocusable(false);
dialog.setVisible(true);
dialog.setFocusable(true);
JavaFx 中似乎没有类似的选项。
下面的示例,当您单击按钮时,它将弹出一个新阶段,获取焦点(注意我不想请求焦点,因为在实际应用程序中,用户可能正在写电子邮件或网页时弹出窗口发生,它不需要关注这些活动)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
private Stage stage;
@Override public void start(Stage stage) {
this.stage = stage;
stage.setTitle("Main Stage");
stage.setWidth(500);
stage.setHeight(500);
Button btnPopupStage = new Button("Click");
btnPopupStage.setOnMouseClicked(event -> popupStage());
Scene scene = new Scene(btnPopupStage);
stage.setScene(scene);
stage.show();
}
private void popupStage(){
Stage subStage = new Stage();
subStage.setTitle("Sub Stage");
subStage.setWidth(250);
subStage.setHeight(250);
subStage.initOwner(stage);
subStage.show();
System.out.println("Does main stage have focus : "+stage.isFocused());
System.out.println("Does popup have focus : "+subStage.isFocused());
}
public static void main(String[] args) {
launch(args);
}
}
关于舞台不关注 stage.show() 的任何想法?谢谢
【问题讨论】: