使用java fx编写小闹钟Alarm Clock

问题描述:

     Create a program which shows the current time using a label. Allow the user to select a given time and specify a message. When the clock reaches that time, have it pop up a message to the user reminding them of their message and/or play a specific sound file.

设计:

UI:

代码: 

import javafx.animation.KeyFrame;

import javafx.animation.Timeline;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.control.*;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.scene.layout.Pane;

import javafx.scene.text.Font;

import javafx.scene.text.Text;

import javafx.stage.Stage;

import javafx.util.Duration;



import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;



public class AlarmMain extends Application {

    private Group group = new Group();

    private Scene scene = new Scene(group, 540, 410);

    //声明

    protected int event = 0;

    protected BackGround backGround = new BackGround();

    protected SetTime setTime = new SetTime();

    protected SetNode setNode = new SetNode();

    protected SetButton setButton = new SetButton();

    protected OutputTime outputTime = new OutputTime();

    protected ArrayList<GetAlarm> saveEvent = new ArrayList<>();

    protected CheckAlarm checkAlarm = new CheckAlarm();



    @Override

    public void start(Stage AlarmClock) {

        //BackGround

        group.getChildren().add(backGround);

        //setTime

        group.getChildren().addAll(setTime.setHour, setTime.setMinute, setTime.setSecond);

        //setNode

        group.getChildren().add(setNode);

        //setButton

        group.getChildren().addAll(setButton.bt1, setButton.bt2);

        //outputTime

        group.getChildren().addAll(outputTime, outputTime.warnTime, outputTime.attentionNode,

                outputTime.h, outputTime.m, outputTime.s);

        //检查

        checkAlarm.Run();

        //执行

        AlarmClock.setTitle("AlarmClock");

        AlarmClock.setScene(scene);

        AlarmClock.show();

    }



    //背景设置

    public class BackGround extends ImageView {

        private Image backGround = new Image("file:src/image/Clock.png");



        public BackGround() {

            this.setImage(backGround);

        }

    }



    //时间设置

    public class SetTime {

        ComboBox setHour = new ComboBox();

        ComboBox setMinute = new ComboBox();

        ComboBox setSecond = new ComboBox();



        public SetTime() {

            SetPoint();

            SetChoose();

        }



        public void SetPoint() {

            setHour.setLayoutX(178.5);

            setHour.setLayoutY(276.0);

            setHour.setPrefWidth(62.0);

            setMinute.setLayoutX(262.5);

            setMinute.setLayoutY(276.0);

            setMinute.setPrefWidth(62.0);

            setSecond.setLayoutX(346.5);

            setSecond.setLayoutY(276.0);

            setSecond.setPrefWidth(62.0);

        }



        public void SetChoose() {

            setHour.setValue("00");

            setMinute.setValue("00");

            setSecond.setValue("00");

            for (int i = 1; i < 24; i++) {

                if (i < 10) setHour.getItems().add("0" + "" + i);

                else setHour.getItems().add("" + i);

            }

            for (int i = 1; i < 60; i++) {

                if (i < 10) setMinute.getItems().add("0" + "" + i);

                else setMinute.getItems().add("" + i);

            }

            for (int i = 1; i < 60; i++) {

                if (i < 10) setSecond.getItems().add("0" + "" + i);

                else setSecond.getItems().add("" + i);

            }

        }

    }



    //备注时间设置

    public class SetNode extends TextField {

        public SetNode() {

            SetPoint();

        }



        public void SetPoint() {

            this.setLayoutX(178.5);

            this.setLayoutY(305.5);

            this.setPrefWidth(252.0);

        }

    }



    //时间文字显示

    public class OutputTime extends Label {

        private SimpleDateFormat ruleDate = new SimpleDateFormat("HH:mm:ss");

        protected Label warnTime = new Label("响铃时间:"),

                attentionNode = new Label("备注事件:");

        protected Label h = new Label("时"),

                m = new Label("分"),

                s = new Label("秒");



        public OutputTime() {

            SetFont();

            SetPoint();

            TimeLine();

        }



        public void TimeLine() {

            EventHandler<ActionEvent> runTime = e -> {

                this.setText(ruleDate.format(new Date()));

            };

            Timeline time = new Timeline(new KeyFrame(Duration.seconds(1), runTime));

            time.setCycleCount(Timeline.INDEFINITE);

            time.play();

        }



        public void SetFont() {

            this.setFont(Font.font(72));

            warnTime.setFont(Font.font(20));

            attentionNode.setFont(Font.font(20));

            h.setFont(Font.font(20));

            m.setFont(Font.font(20));

            s.setFont(Font.font(20));

        }



        public void SetPoint() {

            this.setLayoutX(123.5);

            this.setLayoutY(137.5);

            warnTime.setLayoutX(81.5);

            warnTime.setLayoutY(273.5);

            attentionNode.setLayoutX(81.5);

            attentionNode.setLayoutY(302.5);

            h.setLayoutX(240.5);

            h.setLayoutY(274.75);

            m.setLayoutX(324.5);

            m.setLayoutY(274.75);

            s.setLayoutX(408.5);

            s.setLayoutY(274.75);

        }

    }



    //功能按钮

    public class SetButton {

        protected Button bt1 = new Button("查看闹钟");

        protected Button bt2 = new Button("设置闹钟");



        public SetButton() {

            SetPoint();

            ActionBt1();

            ActionBt2();

        }



        public void SetPoint() {

            //

            bt1.setLayoutX(101.5);

            bt1.setLayoutY(331.5);

            bt1.setPrefWidth(141.5);

            //

            bt2.setLayoutX(265.0);

            bt2.setLayoutY(331.5);

            bt2.setPrefWidth(141.5);

        }



        public void ActionBt1() {

            bt1.setOnAction(e -> {

                ViewEvent();

            });

        }



        public void ActionBt2() {

            bt2.setOnAction(e -> {

                SaveEvent();

                event++;

            });

        }

    }



    //获取设置时间

    public class GetAlarm {

        private final String EventNum = "" + (event + 1);

        private String getTime = "";

        private String getNode = "";



        public GetAlarm() {

            getTime();

            getNode();

        }



        public String getTime() {

            getTime += setTime.setHour.getValue() + ":";

            getTime += setTime.setMinute.getValue() + ":";

            getTime += setTime.setSecond.getValue();

            return getTime;

        }



        public String getNode() {

            getNode += setNode.getText();

            return getNode;

        }

    }



    public void SaveEvent() {

        saveEvent.add(new GetAlarm());

        //

        Stage pre = new Stage();

        Group pr = new Group();

        Label att = new Label("成功设置闹钟~");

        att.setFont(Font.font(35));

        pr.getChildren().add(att);

        pre.setScene(new Scene(pr));

        pre.show();

    }



    public void ViewEvent() {

        Group viewGroup = new Group();

        double startX = 6, startY = 25, jump = 25;

        Stage viewStage = new Stage();

        if (event == 0) {

            Label l = new Label("还没有定闹钟哦");

            l.setFont(Font.font(35));

            viewGroup.getChildren().add(l);

            viewStage.setScene(new Scene(viewGroup));

        } else {

            for (int i = 0; i < event; i++) {

                viewGroup.getChildren().add(new ForViewEvent(startX, startY + i * jump, i));

            }

            viewStage.setScene(new Scene(viewGroup, 360, 205));

        }

        viewStage.setTitle("已定闹钟");

        viewStage.show();

    }



    public class ForViewEvent extends Text {

        public ForViewEvent(double x, double y, int num) {

            setFont(Font.font(24));

            setLayoutX(x);

            setLayoutY(y);

            setText(saveEvent.get(num).EventNum + ". " + saveEvent.get(num).getTime + " " + saveEvent.get(num).getNode);

        }

    }



    public class CheckAlarm {

        public int check() {

            int num = -1;

            for (int i = 0; i < event; i++) {

                if (saveEvent.get(i).getTime.equals(outputTime.ruleDate.format(new Date()))) {

                    num = i;

                    break;

                }

            }

            return num;

        }



        public void Run() {

            EventHandler<ActionEvent> check = e -> {

                if (check() != -1) Ring(check());

            };

            Timeline time = new Timeline(new KeyFrame(Duration.seconds(1), check));

            time.setCycleCount(Timeline.INDEFINITE);

            time.play();

        }



        public void Ring(int x) {

            Group ringGroup = new Group();

            Stage ringStage = new Stage();

            ringStage.setScene(new Scene(ringGroup));

            ringGroup.getChildren().add(new ForRing(x));

            ringStage.setTitle("Ring!Ring!Ring!" + " " + saveEvent.get(x).getTime);

            saveEvent.remove(x);

            event--;

            ringStage.show();

        }



        public class ForRing extends Label {

            public ForRing(int num) {

                setFont(Font.font(48));

                setText(saveEvent.get(num).getNode);

            }

        }

    }

}

运行程序的屏幕截图:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值