JavaFX控制元件的layoutChildren方法
阿新 • • 發佈:2019-01-01
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(); } } }