JavaFX 尺寸&邊界&位置&座標

尺寸&邊界&位置&座標

min max pre
Button button = new Button("ButtonButtonButtonButton");
button.setMinWidth(100);
button.setPrefWidth(120);
button.setMaxWidth(150);

button.getWidth() //父組件提供的寬度
//類似 wrap_content
button.setPrefWidth(Button.USE_COMPUTED_SIZE);
//Control.USE_COMPUTED_SIZE 類似 wrap_content
//Control.USE_PREF_SIZE 固定值

位置&座標

button.setLayoutX(100);
button.setLayoutY(100);

//父組件中的座標
double layoutX = button.getLayoutX();
double layoutY = button.getLayoutY();

//當前組件邊界框範圍, 不包含 Effect
Bounds layoutBounds = button.getLayoutBounds();
//當前組件寬高
double width = layoutBounds.getWidth();
double height = layoutBounds.getHeight();
//左上角X
double minX = layoutBounds.getMinX();
//左上角Y
double minY = layoutBounds.getMinY();
//右下角X
double maxX = layoutBounds.getMaxX();
//右下角Y
double maxY = layoutBounds.getMaxY();

//當前座標轉父座標
Point2D localToParent = button.localToParent(minX, minY);
double parentX = localToParent.getX();
double parentY = localToParent.getY();
//當前座標轉場景座標
Point2D localToScene = button.localToScene(minX, minY);
double sceneX = localToScene.getX();
double sceneY = localToScene.getY();
//當前座標轉屏幕座標
Point2D localToScreen = button.localToScreen(minX, minY);
double screenX = localToScreen.getX();
double screenY = localToScreen.getY();

邊界

//當前組件邊界框範圍, 不包含 Effect
Bounds layoutBounds = button.getLayoutBounds();
//當前組件包含 Effect 的邊界框範圍
Bounds boundsInLocal = button.getBoundsInLocal();
//當前組件在父組件的邊界框範圍
Bounds boundsInParent = button.getBoundsInParent();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章