专栏目录
1.JavaFx实现闹钟小程序2.银行账户管理员
3.大数字
4.购物车
5.文本编辑器
6.乌龟图
所有程序皆使用JDK8
JavaFX
JavaFx菜鸟教程
JavaFx哔哩哔哩教程
JavaFx是什么
JavaFx是java实现图形界面的一种方式,其他还有java的awt、swing,但是逐渐被淘汰。
awt --> swing --> JavaFx
javafx可以实现逻辑和样式的分离,可以使用xml和css来编写样式。
在学习之前请确保你已经熟练掌握面向对象、包装类、枚举、注解、匿名对象等内容的概念和使用
JavaFx使用注意事项
自从java11以后,jdk已经不内置javafx库,已交给开源社区管理,所以我们需要自己导入
可以到网站去下载 jar 包。注意下载的类型是sdk
若无法访问建议使用科学上网,或自行选取其他方式下载
或者使用maven引入依赖
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19-ea+8</version>
</dependency>
JavaFx实现闹钟小程序
项目描述
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.
项目目录
– src
-------- image
------------sound
-------- AlarmMain.java文章来源:https://www.toymoban.com/news/detail-830587.html
gitee地址
程序代码
/**
* @projectName: Alarm
* @package: javaFx
* @className: AlarmMain
* @author: Jiabao Yu
* @date: 2023/1/7 16:29
*/
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(48));
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(48));
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);
}
}
}
}
运行截图
文章来源地址https://www.toymoban.com/news/detail-830587.html
到了这里,关于【JavaFx】1.JavaFx实现闹钟小程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!