= 自由にレイアウトする = == 自由にレイアウトする == 既存のレイアウタでは不可能な「おかしなこと」をやりたい時、自身でレイアウトする必要があるが、この方法の一つとして以下がある。 以下ではボタンがテキスト領域の一部に入り込んでレイアウトされる。 If you want to do weired things on layouting which can't be achieved by standard layouters, one of the ways is the following. A Button is layouted overlapping TextArea. {{{ import javafx.application.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class FreeLayout extends Application { @Override public void start(Stage stage) throws Exception { TextArea textArea = new TextArea(); Button button = new Button("some button"); Pane pane = new Pane() { @Override protected void layoutChildren() { super.layoutChildren(); double width = getWidth(); double height = getHeight(); textArea.resize(width, height); button.relocate(width - button.getWidth() - 10, height - button.getHeight() - 10); } }; pane.getChildren().addAll(textArea, button); stage.setScene(new Scene(pane)); stage.setWidth(500); stage.setHeight(500); stage.show(); } public static void main(String[] args) throws Exception { launch(args); } } }}} == レイアウトを強制する == レイアウトをわざと強制するには以下。 Forcing layouting on purpose. {{{ import java.util.*; import javafx.application.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class RequestLayout extends Application { @Override public void start(Stage stage) throws Exception { Button button = new Button("layout"); Pane pane = new Pane() { Random r = new Random(); @Override public void layoutChildren() { System.out.println("layoutChildren"); super.layoutChildren(); button.relocate( (getWidth() - button.getWidth()) * r.nextDouble(), (getHeight() - button.getHeight()) * r.nextDouble() ); } }; button.setOnAction(e-> { System.out.println("clicked"); pane.requestLayout(); }); pane.getChildren().add(button); Scene scene = new Scene(pane); stage.setScene(scene); stage.setWidth(400); stage.setHeight(400); stage.show(); } public static void main(String[] args) throws Exception { launch(args); } } }}}