2012-05-27 59 views

回答

20

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

+0

謝爾蓋,你可以說,如果有一個解決方案,使用戶時釋放鼠標左鍵階段Resize事件監聽器,寬度和高度屬性? Chris提供了一個解決方案,但它只允許減少事件數量,但不允許只獲取一個事件。 –

+0

@PavelK請提出一個單獨的問題與更多細節。我不明白你想達到什麼目的。 –

+0

謝謝你的回答。請參閱https://stackoverflow.com/questions/44159794/get-single-stage-resize-event-when-user-releases-left-mouse-button –

9

A之後重新上漿一個場景結束,你可以做到這一點執行操作方式做到這一點,對我來說,做的工作)

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); 
+0

加上一個解決方案與計時器任務。非常好;) – stviper

+0

這會不會讓應用程序在關閉之後仍然運行? – Jacks

+0

感謝您發佈您的解決方案。 –

0

這是太舊,基本的,但它可以幫助一個noob像我

您可以添加

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

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