package fx;
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.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Exercise16_03 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Rectangle rectangle = new Rectangle(50, 100);
rectangle.setStyle("-fx-fill: null; -fx-stroke: black;");
Circle redLamp = new Circle(10); //红灯
Circle yellowLamp = new Circle(10); //黄灯
Circle greenLamp = new Circle(10); //绿灯
redLamp.setStyle("-fx-fill: null; -fx-stroke: black;"); //灯设置样式
yellowLamp.setStyle("-fx-fill: null; -fx-stroke: black;");
greenLamp.setStyle("-fx-fill: null; -fx-stroke: black;");
VBox vBox = new VBox(10, redLamp, yellowLamp, greenLamp); //vbox添加灯
vBox.setAlignment(Pos.CENTER);
RadioButton redButton = new RadioButton("Red"); //单选按钮
RadioButton yellowButton = new RadioButton("Yellow");
RadioButton greenButton = new RadioButton("Green");
ToggleGroup toggleGroup = new ToggleGroup(); //开关组
redButton.setToggleGroup(toggleGroup); //单选按钮设置开关组
yellowButton.setToggleGroup(toggleGroup);
greenButton.setToggleGroup(toggleGroup);
HBox hBox = new HBox(10, redButton, yellowButton, greenButton); //hbox添加按钮
hBox.setAlignment(Pos.CENTER);
redButton.setOnAction(event -> { //单选按钮注册动作事件
if (redButton.isSelected()) { //如果按钮被选择
redLamp.setFill(Color.RED); //设置填充
yellowLamp.setFill(null);
greenLamp.setFill(null);
}
});
yellowButton.setOnAction(event -> {
if (yellowButton.isSelected()) {
redLamp.setFill(null);
yellowLamp.setFill(Color.YELLOW);
greenLamp.setFill(null);
}
});
greenButton.setOnAction(event -> {
if (greenButton.isSelected()) {
redLamp.setFill(null);
yellowLamp.setFill(null);
greenLamp.setFill(Color.GREEN);
}
});
BorderPane pane = new BorderPane(new StackPane(rectangle, vBox));
pane.setBottom(hBox);
Scene scene = new Scene(pane, 350, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Exercise16_03");
primaryStage.show();
}
}