JavaFX控件的layoutChildren方法是继承自Parent的方法,可监听控件大小改变,在窗口展示后触发两次,最大化最小化时各触发一次,height和width改变会触发,焦点离开和回到当前窗口也会触发。
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class LayoutChildrenTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
MyButton myButton = new MyButton();
myButton.setText("Test");
myButton.setTranslateX(0);
myButton.setTranslateY(0);
Timeline timeline = new Timeline();
timeline.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(myButton.prefWidthProperty(), 50)), new KeyFrame(new Duration(3000), new KeyValue(myButton.prefWidthProperty(), 200)));
timeline.play();
Pane pane = new Pane(myButton);
primaryStage.setScene(new Scene(pane,200,200));
primaryStage.show();
}
class MyButton extends Button{
@Override
protected void layoutChildren() {
System.out.println("layoutChildren is using!");
super.layoutChildren();
}
}
}