当前位置: 首页 > wzjs >正文

seo线下培训课程seo策略分析

seo线下培训课程,seo策略分析,上海网站建设套餐,长春网站制作公司javaFX实现倒计时 核心概念 Timeline: Timeline 是JavaFX动画API的核心类,用于创建动画。它可以按照指定的时间间隔(Duration)触发事件(KeyFrame)。 可以将其视为一个定时器,每隔一段时间执行一些操作。 …

javaFX实现倒计时

核心概念

  1. Timeline:

    • Timeline 是JavaFX动画API的核心类,用于创建动画。它可以按照指定的时间间隔(Duration)触发事件(KeyFrame)。

    • 可以将其视为一个定时器,每隔一段时间执行一些操作。

  2. KeyFrame:

    • KeyFrame 定义了在特定的时间点要执行的操作。它包含一个 Duration 和一个 EventHandler<ActionEvent>

    • Duration 指定了从 Timeline 开始到 KeyFrame 执行的时间。

    • EventHandler<ActionEvent> 定义了在该时间点要执行的代码。

  3. Duration:

    • Duration 类用于指定时间间隔。可以指定为秒(seconds)、毫秒(millis)、纳秒(nanos)等。

  4. Label:

    • Label 用于显示倒计时的时间。可以通过 setText() 方法更新 Label 的文本内容。

  5. JavaFX Application Thread:

    • 在JavaFX应用程序中,所有的UI更新都必须在JavaFX Application Thread中进行。可以使用 Platform.runLater() 方法将代码提交到JavaFX Application Thread中执行。

代码实现 

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;import java.time.LocalTime;
import java.time.format.DateTimeFormatter;public class CountdownApp extends Application {private static final int DEFAULT_SECONDS = 60; // 默认倒计时时长,单位秒private Label countdownLabel;private Timeline timeline;private int remainingSeconds = DEFAULT_SECONDS;private Button startButton;private Button stopButton;@Overridepublic void start(Stage primaryStage) {countdownLabel = new Label(formatTime(remainingSeconds));countdownLabel.setStyle("-fx-font-size: 48px;");startButton = new Button("Start");stopButton = new Button("Stop");stopButton.setDisable(true); // 初始状态禁用停止按钮startButton.setOnAction(event -> startCountdown());stopButton.setOnAction(event -> stopCountdown());VBox root = new VBox(10, countdownLabel, startButton, stopButton);root.setAlignment(Pos.CENTER);root.setPadding(new Insets(20));Scene scene = new Scene(root, 400, 300);primaryStage.setTitle("Countdown Timer");primaryStage.setScene(scene);primaryStage.show();}private void startCountdown() {startButton.setDisable(true);  // 禁用启动按钮stopButton.setDisable(false); // 启用停止按钮timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {if (remainingSeconds > 0) {remainingSeconds--;Platform.runLater(() -> countdownLabel.setText(formatTime(remainingSeconds))); // 必须在fx线程更新UI} else {stopCountdown();Platform.runLater(() -> countdownLabel.setText("Time's up!")); // 时间到,显示 Time's up!}}));timeline.setCycleCount(remainingSeconds); // 倒计时指定次数timeline.setOnFinished(event -> {  //倒计时完成stopButton.setDisable(true);startButton.setDisable(false);});timeline.play();}private void stopCountdown() {if (timeline != null) {timeline.stop();}stopButton.setDisable(true);startButton.setDisable(false);remainingSeconds = DEFAULT_SECONDS;Platform.runLater(() -> countdownLabel.setText(formatTime(remainingSeconds)));}private String formatTime(int seconds) {LocalTime time = LocalTime.ofSecondOfDay(seconds);DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); // 格式化为 时:分:秒return time.format(formatter);}public static void main(String[] args) {launch(args);}
}

 代码解释

  • DEFAULT_SECONDS:定义默认的倒计时秒数。

  • countdownLabel:用于显示倒计时的标签。

  • timelineTimeline 对象,用于定时触发事件。

  • remainingSeconds:存储剩余秒数的整数变量。

  • startButton & stopButton: 启动/停止 倒计时按钮

  • startCountdown 方法

    • startButton.setDisable(true) & stopButton.setDisable(false):启动时启动按钮禁用,停止按钮启用

    • KeyFrame中的实现:判断 remainingSeconds > 0,否则停止倒计时,更新UI

    • timeline.setCycleCount(remainingSeconds):设置 Timeline 循环指定次数,次数等于remainingSeconds

    • timeline.setOnFinished(event -> {...}): 设置倒计时结束后的处理,例如启用/禁用 按钮

    • Platform.runLater(() -> countdownLabel.setText(...)) 使用Platform.runLater 更新UI。

  • stopCountdown 方法

    • 停止 timeline, 重置UI,重置remainingSeconds

http://www.dtcms.com/wzjs/273171.html

相关文章:

  • 微网站建设报价表深圳关键词排名seo
  • 做婚姻介绍网站赚钱吗无货源网店怎么开
  • 移动宽带续费网上可以续费嘛广州seo运营
  • 入驻京东需要自己做网站吗咖啡seo是什么意思
  • 怎么做网站弄网盟网站推广软件ky99
  • 网站开发分为深圳网络推广系统
  • edu网站一般谁做的开发一个网站需要多少钱
  • 合肥做网站大概多少钱seo专员的工作内容
  • 给医院做网站赚钱吗seo专员是干嘛的
  • 做网站主题杭州网站seo推广软件
  • 做网站销售 优帮云个人如何推广app
  • 南昌市 做网站的公司2345网址导航智能主板
  • 做外贸建网站外包接单平台
  • python 做视频网站宁波核心关键词seo收费
  • flash 好的网站app开发者需要更新此app
  • 新网网站负责人核验现场拍摄照片电子件枫树seo
  • 北京网站建设公司石家庄新闻最新消息
  • 手机购买网站源码网站优化方案范文
  • 网页特效代码大全什么是seo营销
  • 公司网站制作重庆友情链接赚钱
  • 上海社保网站哪里做转入推广策略包括哪些内容
  • 河南网站建设软件关键词seo公司真实推荐
  • 天津手机网站公司淘宝seo具体优化方法
  • 北京建设执业注册中心网站网上找客户有什么渠道
  • 做外贸网站的都有哪些类型的公司搜索引擎广告投放
  • 公司商标设计丁的老头seo博客
  • 汽车门户网站源码免费域名服务器
  • 桥东网站建设太原百度关键词排名
  • 郑州网站seo服务湖南专业seo推广
  • 购物网站建设模板图片爱链