zoukankan      html  css  js  c++  java
  • 用JavaFX模拟一个交通信号灯

       ( 交通信号灯)编写一个程序来模拟交通信号灯。 程序可以让用户从红、 黄 、 绿三种顔色灯中选择一种。当选择一个单选按钮后, 相应的灯被打开,并且一次只能亮一种灯。程序开始时所有的灯都是不亮的。

    代码如下:

    package javaseniorprograme;
    
    import javafx.application.Application;
    import javafx.geometry.Insets;
    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.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    /**
     * 交通信号灯
     * @author 李安国
     */
    public class Exercise16_03 extends Application{
        @Override
        public void start(Stage stage){
            // 创建一个矩形,初始化位置
            Rectangle rec = new Rectangle(215,10,70,200);
            rec.setStroke(Color.BLACK);
            rec.setFill(Color.WHITE); 
            // 创建三个圆形,初始化位置
            Circle c1 = new Circle(250,45,30,Color.WHITE);
            Circle c2 = new Circle(250,110,30,Color.WHITE);
            Circle c3 = new Circle(250,175,30,Color.WHITE);
            c1.setStroke(Color.BLACK);
            c2.setStroke(Color.BLACK);
            c3.setStroke(Color.BLACK);
            // 创建三个单选按钮
            RadioButton rb1 = new RadioButton();
            RadioButton rb2 = new RadioButton();
            RadioButton rb3 = new RadioButton();
            
            ToggleGroup tg = new ToggleGroup();
            rb1.setToggleGroup(tg);
            rb2.setToggleGroup(tg);
            rb3.setToggleGroup(tg);
            rb1.setSelected(true);
            
            rb1.setOnMouseClicked(e->{
                c1.setFill(Color.RED); 
                c1.setStroke(Color.WHITE); 
                c2.setFill(Color.WHITE);
                c3.setFill(Color.WHITE);
                c2.setStroke(Color.BLACK);
                c3.setStroke(Color.BLACK);
            });
            rb2.setOnMouseClicked(e->{
                c2.setFill(Color.YELLOW); 
                c2.setStroke(Color.WHITE); 
                c1.setFill(Color.WHITE);
                c3.setFill(Color.WHITE);
                c1.setStroke(Color.BLACK);
                c3.setStroke(Color.BLACK);
            });
            rb3.setOnMouseClicked(e->{
                c3.setFill(Color.GREEN); 
                c3.setStroke(Color.WHITE); 
                c2.setFill(Color.WHITE);
                c1.setFill(Color.WHITE);
                c2.setStroke(Color.BLACK);
                c1.setStroke(Color.BLACK); 
            });
            // 创建三个文本
            Text t1 = new Text("Red");
            Text t2 = new Text("Yellow");
            Text t3 = new Text("Green");
           // 创建一个HBox 
            HBox hbox = new HBox();
            hbox.setSpacing(10); 
            hbox.setAlignment(Pos.CENTER); 
            hbox.getChildren().addAll(rb1,t1,rb2,t2,rb3,t3);
            // 创建一个Pane
            Pane pa = new Pane();
            pa.getChildren().addAll(rec,c1,c2,c3);
            // 创建一个BorderPane
            BorderPane pane = new BorderPane();
            pane.setCenter(pa); 
            pane.setBottom(hbox); 
            Scene scene = new Scene(pane,500,300);
            stage.setTitle("Exercise16_03");
            stage.setScene(scene);
            stage.show();
        }
        /**
         * @param args
         * main方法
         */
        public static void main(String[] args){
            Application.launch(args); 
        }
    }

    爱我没结果!
  • 相关阅读:
    创建授权SQL mysql数据库命令
    [mysql] 无法通过insert 创建用户ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
    MySql 用户管理 中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束):
    mysql 导出数据到文件数据异常 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
    tab切换 js制作 index和this的应用
    mysql中 where in 用法详解
    Mysql Join语法解析与性能分析
    Java 导入导出Excle表格 两种方式
    SQLyog 快捷键
    DnCNN-Beyond a Gaussian Denoiser: Residual Learning of Deep CNN for Image Denoising
  • 原文地址:https://www.cnblogs.com/angoli/p/12727882.html
Copyright © 2011-2022 走看看