Java语言程序设计 第十六章 (16.17、16.18、16.19、16.20、16.21)

程序小白,希望和大家多交流,共同学习
Java语言程序设计 第十六章 (16.17、16.18、16.19、16.20、16.21)_第1张图片
Java语言程序设计 第十六章 (16.17、16.18、16.19、16.20、16.21)_第2张图片
16.17

//使用滚动条调节字体颜色
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;

public class ChangeTextColorUseScrollBar extends Application
{
    //初始化各个滚动条的
    private ScrollBar sbRed = new ScrollBar();
    private ScrollBar sbBlue = new ScrollBar();
    private ScrollBar sbGreen = new ScrollBar();
    private ScrollBar sbOpacity = new ScrollBar();
    private Text text = new Text("Show Color");

    @Override
    public void start(Stage primaryStage)
    {
        VBox vbForRBGA = new VBox(10);
        vbForRBGA.getChildren().addAll(sbRed, sbBlue, sbGreen, sbOpacity);
        //功能实现
        sbRed.valueProperty().addListener(e -> {
            setTextColor();
        });

        sbBlue.valueProperty().addListener(e -> {
            setTextColor();
        });

        sbGreen.valueProperty().addListener(e -> {
            setTextColor();
        });

        sbOpacity.valueProperty().addListener(e -> {
            setTextColor();
        });
        //排盘
        BorderPane pane = new BorderPane();
        pane.setCenter(text);
        pane.setBottom(vbForRBGA);

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ChangeTextColorUseScrollBar");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    //设置颜色
    public void setTextColor()
    {
        double redValue = sbRed.getValue() / 100;
        double blueValue = sbBlue.getValue() / 100;
        double greenValue = sbGreen.getValue() / 100;
        double opacityValue = sbOpacity.getValue() / 100;

        text.setFill(Color.color(redValue, blueValue, greenValue, opacityValue));
    }
}

16.18、16.19

//旋转的风扇
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.scene.shape.Arc;
import javafx.scene.shape.Circle;
import javafx.scene.shape.ArcType;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.beans.property.DoubleProperty;

public class FanPane extends Pane
{
    private Timeline fanAnimation;
    private double LEN;
    private double W;
    private double H;
    private boolean direction = true;
    private double rotate = 10.0;
    private Arc a1;
    private Arc a2;
    private Arc a3;
    private Arc a4;

    public FanPane()
    {
        fanAnimation = new Timeline();
        W = H = 250.0;
        LEN = 250.0;
        drawFanAnimation();
    }
    //设置速率
    public void setRate(double rate)
    {
        fanAnimation.rateProperty().setValue(rate);
    }
    //动画停止
    public void pause()
    {
        fanAnimation.pause();
    }
    //动画播放
    public void  resume()
    {
        fanAnimation.play();
    }
    //动画反向
    public void reverse()
    {
        direction = !direction;
    }
    public void fitLen(double w, double h)
    {
        this.W = w;
        this.H = h;
        this.LEN = Math.min(w, h);
        drawFanAnimation();
    }
    //制作动画
    public void drawFanAnimation()
    {
        this.getChildren().clear();
        Circle circle = new Circle(W / 2, H / 2, LEN * 0.5);
        circle.setFill(Color.WHITE);
        circle.setStroke(Color.BLACK);
        a1 = new Arc(W / 2, H / 2, LEN * 0.45, LEN * 0.45, 0, 30);
        a1.setFill(Color.BLACK);
        a1.setStroke(Color.BLACK);
        a1.setType(ArcType.ROUND);
        a2 = new Arc(W / 2, H / 2, LEN * 0.45, LEN * 0.45, 90, 30);
        a2.setFill(Color.BLACK);
        a2.setStroke(Color.BLACK);
        a2.setType(ArcType.ROUND);
        a3 = new Arc(W / 2, H / 2, LEN * 0.45, LEN * 0.45, 180, 30);
        a3.setFill(Color.BLACK);
        a3.setStroke(Color.BLACK);
        a3.setType(ArcType.ROUND);
        a4 = new Arc(W / 2, H / 2, LEN * 0.45, LEN * 0.45, 270, 30);
        a4.setFill(Color.BLACK);
        a4.setStroke(Color.BLACK);
        a4.setType(ArcType.ROUND);

        EventHandler eventHandler = e -> {
            setNewRotate();
        };
        //此处设置注意
        fanAnimation.getKeyFrames().add(new KeyFrame(Duration.millis(1000), eventHandler));
        fanAnimation.setCycleCount(Timeline.INDEFINITE);
        fanAnimation.play();

        this.getChildren().addAll(circle, a1, a2, a3, a4);
    }
    //设置风扇旋转
    public void setNewRotate()
    {
        if (direction)
        {
            rotate = 10.0;
        }
        else
            rotate = -10.0;

        a1.startAngleProperty().setValue((a1.startAngleProperty().getValue() + rotate) % 360);
        a2.startAngleProperty().setValue((a2.startAngleProperty().getValue() + rotate) % 360);
        a3.startAngleProperty().setValue((a3.startAngleProperty().getValue() + rotate) % 360);
        a4.startAngleProperty().setValue((a4.startAngleProperty().getValue() + rotate) % 360);
    }
}
//对风扇的工作的控制类
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;

public class ControlFanPane extends BorderPane
{
    private FanPane fan;
    private Button btPause;
    private Button btResume;
    private Button btReverse;
    private ScrollBar sbRate;

    public ControlFanPane()
    {
        fan = new FanPane();
        btPause = new Button("Pause");
        btResume = new Button("Resume");
        btReverse = new Button("Reverse");
        sbRate = new ScrollBar();
        body();
    }
    //提供开始方法
    public void play()
    {
        fan.resume();
    }
    //提供停止方法
    public void stop()
    {
        fan.pause();
    }
    //主体部分,将按钮,滚动条和风扇进行排版,和功能实现,还有这个
    public void body()
    {
        HBox hbForControl = new HBox(10);
        hbForControl.setAlignment(Pos.CENTER);
        hbForControl.getChildren().addAll(btPause, btResume, btReverse);

        btPause.setOnAction(e -> {
            fan.pause();
        });

        btResume.setOnAction(e -> {
            fan.resume();
        });

        btReverse.setOnAction(e -> {
            fan.reverse();
        });

        sbRate.valueProperty().addListener(e -> {
            fan.setRate(sbRate.getValue());
        });

        this.setTop(hbForControl);
        this.setCenter(fan);
        this.setBottom(sbRate);
    }
}
//多个风扇一起控制
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;

public class MoreFans extends Application
{
    @Override
    public void start(Stage primaryStage)
    {
        //三个可控风扇
        ControlFanPane f1 = new ControlFanPane();
        f1.setStyle("-fx-border-color: black");
        ControlFanPane f2 = new ControlFanPane();
        f2.setStyle("-fx-border-color: black");
        ControlFanPane f3 = new ControlFanPane();
        f3.setStyle("-fx-border-color: black");

        HBox hbForFans = new HBox(10);
        hbForFans.setAlignment(Pos.CENTER);
        hbForFans.getChildren().addAll(f1, f2, f3);
        //控制所有风扇
        Button btStartAll = new Button("Start All");
        Button btStopAll = new Button("Stop All");
        HBox hbForControl = new HBox(10);
        hbForControl.setAlignment(Pos.CENTER);
        hbForControl.getChildren().addAll(btStartAll, btStopAll);

        btStartAll.setOnAction(e -> {
            f1.play();
            f2.play();
            f3.play();
        });

        btStopAll.setOnAction(e -> {
            f1.stop();
            f2.stop();
            f3.stop();
        });
        //排版
        BorderPane pane = new BorderPane();
        pane.setTop(hbForFans);
        pane.setBottom(hbForControl);

        Scene scene = new Scene(pane);
        primaryStage.setTitle("MoreFans");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

16.20

//计时器
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.animation.KeyFrame;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;

public class Calculagraph extends Application
{
    private int hour = 0;
    private int minute = 0;
    private int second = 0;
    private Text text = new Text();//时间以文本形式显示

    @Override
    public void start(Stage primaryStage)
    {
        //按照格式要求设置时间输出的格式
        String str = String.format("%02d:%02d:%02d", hour, minute, second);
        text.setText(str);
        text.setFont(Font.font(40));

        Button btStart = new Button("Start");
        Button btClear = new Button("Clear");

        HBox hb = new HBox(10);
        hb.setAlignment(Pos.CENTER);
        hb.getChildren().addAll(btStart, btClear);

        BorderPane pane = new BorderPane();
        pane.setCenter(text);
        pane.setBottom(hb);
        //设置时间显示的动画
        EventHandler eventHandler = e -> {
            addTime();
        };

        Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
        animation.setCycleCount(Timeline.INDEFINITE);
        //开始和清零按钮功能实现
        btStart.setOnAction(e -> {
            if (btStart.getText().equals("Start"))//每次点击后按钮显示下一次点击的功能
            {
                animation.play();
                btStart.setText("Pause");
            }
            else if (btStart.getText().equals("Pause"))
            {
                animation.pause();
                btStart.setText("Start");
            }
        });
            //清空按钮将时间设置为零点
        btClear.setOnAction(e -> {
            hour = 0;
            minute = 0;
            second = 0;
            String clear = String.format("%02d:%02d:%02d", hour, minute, second);
            text.setText(clear);
            animation.pause();
            btStart.setText("Start");
        });

        Scene scene = new Scene(pane);
        primaryStage.setTitle("Calculagraph");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    //时间递增,当时间到达24小时清为0点
    public void addTime()
    {
        if (second < 59)
        {
            second++;
        }
        else
        {
            second = 0;
            if (minute < 59)
            {
                minute++;
            }
            else
            {
                minute = 0;
                if (hour < 23)
                {
                    hour++;
                }
                else
                    hour = 0;
            }
        }

        String str = String.format("%02d:%02d:%02d", hour, minute, second);
        text.setText(str);
    }
}

16.21

//倒计时
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.scene.layout.StackPane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.util.Duration;

public class CountDown extends Application
{
    private TextField tfTime = new TextField();
    private String musicPath = getClass().getResource("sound/china.mp3").toString();
    private Media media = new Media(musicPath);
    private MediaPlayer music = new MediaPlayer(media);
    private Timeline animation = new Timeline();

    @Override
    public void start(Stage primaryStage)
    {
        //设置文本域的大小和字体
        tfTime.setAlignment(Pos.CENTER);
        tfTime.setPrefColumnCount(2);
        tfTime.setFont(Font.font("", 40));

        StackPane pane = new StackPane();
        pane.getChildren().add(tfTime);

        //闹钟时间递减事件
        EventHandler eventHandler = e -> {
            changeTime();
        };
        //秒数递减
        animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000), eventHandler));
        animation.setCycleCount(Timeline.INDEFINITE);
        //一旦输入合法,文本域在时间到0之前不允许输入新的数字
        tfTime.setOnAction(e -> {
            if (judgeTime())
            {
                tfTime.setEditable(false);
                music.stop();
                animation.play();
            }
            else
                tfTime.setText("Illegal");
        });

        Scene scene = new Scene(pane);
        primaryStage.setTitle("CountDown");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    //规范输入不是非数字,不是空
    public boolean judgeTime()
    {
        if (tfTime.getText().equals(""))
        {
            return false;
        }
        String in = tfTime.getText();
        int len = in.length();
        for (int i = 0; i < len; i++)
        {
            if (!Character.isDigit(in.charAt(i)))
            {
                return false;
            }
        }

        return true;
    }
    //修改时间,并且在时间为零的时候播放音乐
    public void changeTime()
    {
        int time = Integer.parseInt(tfTime.getText());
        if (time > 0)
        {
            time--;
            tfTime.setText("" + time);
        }
        else if (time == 0)//时间为零的时候播放音乐,文本域允许重新输入,时间递减时间停止。
        {
            tfTime.setText("" + time);
            music.play();
            tfTime.setEditable(true);
            animation.stop();
        }
    }
}

你可能感兴趣的:(JavaFX)