JavaFX、交通信号灯

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();
    }
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值