【问题标题】:Entering and exiting application with JavaFX使用 JavaFX 进入和退出应用程序
【发布时间】:2017-04-05 03:16:22
【问题描述】:

JavaFX 有一些奇怪的问题,看起来很像一个错误。我想做以下事情:

  • 启动我的应用程序时进入全屏模式
  • 按退出键退出应用程序(不是全屏,整个应用程序)

到目前为止,我有以下代码:

public class AppTest extends Application {

  public static void main(String[] args) {
      launch(args);
  }

  public void start(Stage stage) {
    stage.setOnCloseRequest(t -> {
      Platform.exit();
      System.exit(0);
    });

    stage.setFullScreenExitHint("Press ESCAPE to exit");
    stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    stage.setFullScreen(true);

    Rectangle2D screenBounds = Screen.getPrimary().getBounds();
    stage.setX(screenBounds.getMinX());
    stage.setY(screenBounds.getMinY());

    double screenWidth = screenBounds.getWidth();
    double screenHeight = screenBounds.getHeight();

    stage.setWidth(screenWidth);
    stage.setHeight(screenHeight);

    Group root = new Group();
    Scene scene = new Scene(root);
    stage.setScene(scene);

    scene.setOnKeyTyped(event -> {
      if(event.getCode() == KeyCode.ESCAPE) {
        stage.close();
      }
    });

    Canvas canvas = new Canvas(screenWidth, screenHeight);
    root.getChildren().add(canvas);

    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.setFill(Color.BLUE);
    gc.fillRect(0,0, screenWidth, screenHeight);

    stage.show();
  }

}

我在 macOS 上。

通常它会全屏显示。我之所以这么说是因为这段代码的真实版本并不总是如此。有时,它只是一个最大化的窗口。

然后,当按下退出时,我得到一个最大化的窗口而不是退出应用程序。

我该如何解决这个问题?

【问题讨论】:

    标签: java javafx javafx-8


    【解决方案1】:

    变化:

    scene.setOnKeyTyped
    

    收件人:

    scene.setOnKeyReleased
    

    This 解释了原因。

    【讨论】:

    • 我是如此接近 :-) 对于其他人,我将确认 NO_MATCH 是必需的。否则,它不会退出应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2011-01-24
    • 2016-05-08
    • 2015-05-27
    相关资源
    最近更新 更多