栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaFX 2用户空闲检测

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaFX 2用户空闲检测

我不太了解您对交易行为的看法。事务关注有关数据的保证,您的事务行为应在数据级别定义,并且不受UI中发生的事情的影响。换句话说,即使UI由于用户闲置而重置,您的原子行为也应完成或回滚。

也许这会有所帮助。(请注意,我在这些示例中使用了Java 8代码,但是如果需要,您可以轻松地使其兼容JavaF 2.2。)这遵循Tomas
Mikula的一般方法,即使用a

Timeline
来实现空闲检查。我没有使用Tomas的FX
Timer包装器,但是如果您愿意,您当然可以使用。此类封装了用于监视用户是否空闲的监视器。您可以注册任何节点(或场景)和事件类型:如果该类型的事件在该节点(或场景)上发生,则确定用户没有空闲。如果经过指定的时间却未发生任何注册事件,则将执行所提供的可运行对象(在FX
Application线程上)。这使您可以灵活地创建多个监视器(如果需要),并为每个监视器注册一个或多个节点。

import javafx.animation.Animation;import javafx.animation.Keyframe;import javafx.animation.Timeline;import javafx.event.Event;import javafx.event.EventHandler;import javafx.event.EventType;import javafx.scene.Node;import javafx.scene.Scene;import javafx.util.Duration;public class IdleMonitor {    private final Timeline idleTimeline ;    private final EventHandler<Event> userEventHandler ;    public IdleMonitor(Duration idleTime, Runnable notifier, boolean startMonitoring) {        idleTimeline = new Timeline(new Keyframe(idleTime, e -> notifier.run()));        idleTimeline.setCycleCount(Animation.INDEFINITE);        userEventHandler = e -> notIdle() ;        if (startMonitoring) { startMonitoring();        }    }    public IdleMonitor(Duration idleTime, Runnable notifier) {        this(idleTime, notifier, false);    }    public void register(Scene scene, EventType<? extends Event> eventType) {        scene.addEventFilter(eventType, userEventHandler);    }    public void register(Node node, EventType<? extends Event> eventType) {        node.addEventFilter(eventType, userEventHandler);    }    public void unregister(Scene scene, EventType<? extends Event> eventType) {        scene.removeEventFilter(eventType, userEventHandler);    }    public void unregister(Node node, EventType<? extends Event> eventType) {        node.removeEventFilter(eventType, userEventHandler);    }    public void notIdle() {        if (idleTimeline.getStatus() == Animation.Status.RUNNING) { idleTimeline.playFromStart();        }    }    public void startMonitoring() {        idleTimeline.playFromStart();    }    public void stopMonitoring() {        idleTimeline.stop();    }}

这是一个测试。“开始”按钮可能是登录的替代方法。主UI具有一个带有两个选项卡的选项卡窗格:每个单独的选项卡均以其自己的“开始”按钮开头,然后主要内容具有标签,文本字段和按钮。

每个选项卡的内容都有一个(简短的,用于测试的)空闲监视器。选项卡内容上的任何事件都将重置空闲监视器,但选项卡内容之外的事件不会将其重置。整个窗口还有一个“全局”空闲监视器,可在30秒后重置整个UI。

请注意,数据将被保留:即,如果由于空闲而超时,则在文本字段中键入的任何文本都会被正确保留。这就是为什么我认为“交易”问题根本不重要的原因。

import javafx.application.Application;import javafx.event.Event;import javafx.geometry.Pos;import javafx.scene.Node;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.control.Label;import javafx.scene.control.Tab;import javafx.scene.control.TabPane;import javafx.scene.control.TextField;import javafx.scene.layout.StackPane;import javafx.scene.layout.VBox;import javafx.stage.Stage;import javafx.util.Duration;public class IdleTest extends Application {    @Override    public void start(Stage primaryStage) {        StackPane root = new StackPane();        Parent mainUI = buildMainUI();        Scene scene = new Scene(root, 350, 150);        Parent startUI = buildStartUI(() -> root.getChildren().setAll(mainUI));        root.getChildren().add(startUI);        IdleMonitor idleMonitor = new IdleMonitor(Duration.seconds(30),     () -> root.getChildren().setAll(startUI), true);        idleMonitor.register(scene, Event.ANY);        primaryStage.setScene(scene);        primaryStage.show();    }    private Parent buildStartUI(Runnable start) {        Button button = new Button("Start");        button.setonAction(e -> start.run());        StackPane root = new StackPane(button);        return root ;    }    private Parent buildMainUI() {        TabPane tabPane = new TabPane();        Tab tab1 = new Tab("One");        Parent tab1Content = buildTabUI("Tab 1");        Parent tab1StartContent = buildStartUI(() -> tab1.setContent(tab1Content));        tab1.setContent(tab1StartContent);        IdleMonitor tab1IdleMonitor = new IdleMonitor(Duration.seconds(5),      () -> tab1.setContent(tab1StartContent), true);        tab1IdleMonitor.register(tab1Content, Event.ANY);        Tab tab2 = new Tab("Two");        Parent tab2Content = buildTabUI("Tab 2") ;        Parent tab2StartContent = buildStartUI(() -> tab2.setContent(tab2Content));        tab2.setContent(tab2StartContent);        IdleMonitor tab2IdleMonitor = new IdleMonitor(Duration.seconds(10),     () -> tab2.setContent(tab2StartContent), true);        tab2IdleMonitor.register(tab2Content, Event.ANY);        tabPane.getTabs().addAll(tab1, tab2);        return tabPane ;    }    private Parent buildTabUI(String text) {        Button button = new Button("Click here");        button.setonAction(e -> System.out.println("Click in "+text));        VBox content = new VBox(10, new Label(text), new TextField(), button);        content.setAlignment(Pos.CENTER);        return content ;    }    public static void main(String[] args) {        launch(args);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/494962.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号