JavaFX VBox create traffic light
import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; public class Main extends Application { @Override /*from w w w.j av a2 s . c o m*/ public void start(Stage primaryStage) { // Create a vbox VBox paneForCircles = new VBox(5); paneForCircles.setAlignment(Pos.CENTER); // Create three circles Circle c1 = getCircle(); Circle c2 = getCircle(); Circle c3 = getCircle(); c1.setFill(Color.RED); paneForCircles.getChildren().addAll(c1, c2, c3); // Create a rectangle Rectangle rectangle = new Rectangle(); rectangle.setFill(Color.WHITE); rectangle.setWidth(30); rectangle.setHeight(100); rectangle.setStroke(Color.BLACK); rectangle.setStrokeWidth(2); StackPane stopSign = new StackPane(rectangle, paneForCircles); // Create a hbox HBox paneForRadioButtons = new HBox(5); paneForRadioButtons.setAlignment(Pos.CENTER); // Create radio buttons RadioButton rbRed = new RadioButton("Red"); RadioButton rbYellow = new RadioButton("Yellow"); RadioButton rbGreen = new RadioButton("Green"); // Create a toggle group ToggleGroup group = new ToggleGroup(); rbRed.setToggleGroup(group); rbYellow.setToggleGroup(group); rbGreen.setToggleGroup(group); rbRed.setSelected(true); paneForRadioButtons.getChildren().addAll(rbRed, rbYellow, rbGreen); // Create a border pane BorderPane pane = new BorderPane(); pane.setCenter(stopSign); pane.setBottom(paneForRadioButtons); // Create and register handlers rbRed.setOnAction(e -> { if (rbRed.isSelected()) { c1.setFill(Color.RED); c2.setFill(Color.WHITE); c3.setFill(Color.WHITE); } }); rbYellow.setOnAction(e -> { if (rbYellow.isSelected()) { c1.setFill(Color.WHITE); c2.setFill(Color.YELLOW); c3.setFill(Color.WHITE); } }); rbGreen.setOnAction(e -> { if (rbGreen.isSelected()) { c1.setFill(Color.WHITE); c2.setFill(Color.WHITE); c3.setFill(Color.GREEN); } }); // Create a scene and place it in the stage Scene scene = new Scene(pane, 200, 150); primaryStage.setTitle("java2s.com"); primaryStage.setScene(scene); primaryStage.show(); } private Circle getCircle() { Circle c = new Circle(10); c.setFill(Color.WHITE); c.setStroke(Color.BLACK); return c; } }