Java实验(19) 交通灯

如图,当用户选择交通灯下方的颜色时,自动填充(相当于点亮)上面对应的颜色灯,并注意清空(相当于熄灭)原先点亮的灯。程序开始自动点亮红灯。


Java实验(19) 交通灯_第1张图片



import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class TrafficLights extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        lights lightpane=new lights();
        BorderPane borderpane=new BorderPane();
        HBox hbox=new HBox();
        StackPane pane=new StackPane();
        RadioButton rbred=new RadioButton("Red");
        RadioButton rbyellow=new RadioButton("Yellow");
        RadioButton rbgreen=new RadioButton("Green");
        hbox.getChildren().addAll(rbred,rbyellow,rbgreen);
        hbox.setAlignment(Pos.CENTER);
        
        ToggleGroup group=new  ToggleGroup();
        rbred.setToggleGroup(group);
        rbyellow.setToggleGroup(group);
        rbgreen.setToggleGroup(group);
        
        pane.getChildren().add(lightpane);
        pane.setAlignment(Pos.CENTER);
        borderpane.setCenter(pane);
        borderpane.setBottom(hbox);
        
        rbred.setOnAction(e->{
            if(rbred.isSelected()){
                lightpane.light1();
            }
        });
        rbyellow.setOnAction(e->{
            if(rbyellow.isSelected()){
                lightpane.light2();
            }
        });
        rbgreen.setOnAction(e->{
            if(rbgreen.isSelected()){
                lightpane.light3();
            }
        });
        
        Scene scene = new Scene(borderpane,200,200);
        primaryStage.setTitle("TrafficLights");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
class lights extends VBox{
    public final double side1=30;//长
    public final double side2=90;//宽
    public final double radius=15; //灯半径
    private Circle circle1=new Circle(radius);
    private Circle circle2=new Circle(radius);
    private Circle circle3=new Circle(radius);
    public lights(){
        circle1.setFill(Color.RED);
        circle1.setStroke(Color.BLACK);
        circle2.setFill(Color.WHITE);
        circle2.setStroke(Color.BLACK);
        circle3.setFill(Color.WHITE);
        circle3.setStroke(Color.BLACK);
        getChildren().addAll(circle1,circle2,circle3);
        setStyle("-fx-border-color:black");
        setMaxSize(side1,side2);
    }
    public void light1(){
        circle1.setFill(Color.RED);
        circle2.setFill(Color.WHITE);
        circle3.setFill(Color.WHITE);
    }
    public void light2(){
        circle1.setFill(Color.WHITE);
        circle2.setFill(Color.YELLOW);
        circle3.setFill(Color.WHITE);
    }
    public void light3(){
        circle1.setFill(Color.WHITE);
        circle2.setFill(Color.WHITE);
        circle3.setFill(Color.GREEN);
    }
}


你可能感兴趣的:(Java实验)