【问题标题】:How to listen for resize events in JavaFX如何在 JavaFX 中侦听调整大小事件
【发布时间】:2012-06-02 03:11:48
【问题描述】:

如何在 JavaFX 2.1 中检测场景或舞台的大小何时发生变化?我找不到任何事件处理程序。

【问题讨论】:

    标签: javafx-2


    【解决方案1】:

    这太老了,太基础了,但它可能会帮助像我这样的菜鸟

    您可以为宽度和高度属性添加监听器

    stage.heightProperty().addListener(e ->{
        handle....
    });
    
    stage.widthProperty().addListener(e ->{
        handle....
    });
    

    【讨论】:

    • 正确。但是 Chris 的解决方案也使用了它。此外,他的解决方案设法减少了更改的数量。在您的情况下,侦听器中的代码至少会运行两次,在他的版本中只运行一次。
    • @GunnarBernstein 同意,这似乎是 2 年前在一个简单的 GUI 中的简单方法
    【解决方案2】:

    一种在场景完成调整大小后执行操作的方法,您可以这样做:

    (注意:可能有更好的方法可以做到这一点,对我来说它完成了这项工作)

    final Stage primaryStage = getStage() // get your stage from somewhere
    
    // create a listener
    final ChangeListener<Number> listener = new ChangeListener<Number>()
    {
      final Timer timer = new Timer(); // uses a timer to call your resize method
      TimerTask task = null; // task to execute after defined delay
      final long delayTime = 200; // delay that has to pass in order to consider an operation done
    
      @Override
      public void changed(ObservableValue<? extends Number> observable, Number oldValue, final Number newValue)
      {
        if (task != null)
        { // there was already a task scheduled from the previous operation ...
          task.cancel(); // cancel it, we have a new size to consider
        }
    
        task = new TimerTask() // create new task that calls your resize operation
        {
          @Override
          public void run()
          { 
            // here you can place your resize code
            System.out.println("resize to " + primaryStage.getWidth() + " " + primaryStage.getHeight());
          }
        };
        // schedule new task
        timer.schedule(task, delayTime);
      }
    };
    
    // finally we have to register the listener
    primaryStage.widthProperty().addListener(listener);
    primaryStage.heightProperty().addListener(listener);
    

    【讨论】:

    • 加一个用于定时器任务的解决方案。非常好;)
    • 这不会让应用程序在关闭后仍然运行吗?
    • 感谢您发布解决方案。
    • +Jacks 你可以在构造函数中设置一个Timer作为守护进程,应该可以解决它。顺便说一句,这个答案真的应该在这里发布:stackoverflow.com/questions/44159794/…
    【解决方案3】:

    heightPropertywidthProperty。您可以使用这些属性进行绑定,或向它们添加侦听器。

    public void start(Stage stage) {
        Scene scene = new Scene(new Group(), 300, 200);
        stage.setScene(scene);
    
        stage.titleProperty().bind(
                scene.widthProperty().asString().
                concat(" : ").
                concat(scene.heightProperty().asString()));
    
        stage.show();
    }
    

    或者看下一个例子:https://stackoverflow.com/a/9893911/1054140

    【讨论】:

    • Sergey,你能说是否有解决方案可以在用户释放鼠标左键时获得舞台调整大小事件? Chris 提供了一个解决方案,但它只允许减少事件数量,但不能只获得一个事件。
    • @PavelK 请提出一个单独的问题,提供更多详细信息。我不明白你想达到什么目的。
    • 感谢您的回答。请参阅stackoverflow.com/questions/44159794/…
    猜你喜欢
    • 2018-10-04
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多