【问题标题】:Auto Close Java FX App likely Sleep mode of window自动关闭 Java FX App 可能是窗口的睡眠模式
【发布时间】:2019-09-09 06:03:04
【问题描述】:

我是 Java FX 的新手。如果用户在一段时间内不活动,我希望关闭我的 JavaFX 应用程序。换句话说,如果持续时间内没有任何鼠标事件或按键事件,应用程序会自动关闭。这可能是窗口的睡眠模式

我确实尝试了Auto close JavaFX application due to innactivity 的代码。但是我的程序不起作用

我从https://www.callicoder.com/javafx-fxml-form-gui-tutorial/ 得到一个例子。 我在 RegistrationFormApplication 类上进行了编辑

public class RegistrationFormApplication extends Application {
 private Timeline timer;
 Parent root ;
@Override
public void start(Stage primaryStage) throws Exception{
     timer = new Timeline(new KeyFrame(Duration.seconds(3600), new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            // TODO Auto-generated method stub
            root = null;
            try {
                root = FXMLLoader.load(getClass().getResource("/example/registration_form.fxml"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                primaryStage.setTitle("Registration Form FXML Application");
                primaryStage.setScene(new Scene(root, 800, 500));
                primaryStage.show();        

        }
     }));

     timer.setCycleCount(Timeline.INDEFINITE);
     timer.play();

     root.addEventFilter(MouseEvent.ANY, new EventHandler<Event>() {
         @Override
         public void handle(Event event) {
             timer.playFromStart();
         }
     });

感谢您的帮助

【问题讨论】:

  • 使用您分享的链接中的@fabian 回答。
  • 我看不到你在哪里打电话给Platform.exit()。您的代码看起来只是一直在尝试加载 FXML 文件。
  • 我使用了@fabian 编写的代码。但是,无论鼠标事件或按键按下,该程序都被计时器关闭。这是我没想到的。我花了很多时间在互联网上寻找某个地方但是这件事似乎很少被发布
  • 请仔细阅读@Sedrick 的两个cmets,按照建议进行操作,如果您对fabian 的解决方案有疑问,请返回一个真正使用它的示例(如minimal reproducible example) :) 并重复强调: 你为什么一遍又一遍地重新加载内容?
  • 您发布的代码将在执行start 方法期间产生异常,因为当您调用addEventFilterrootnull(除非您隐藏了一些代码在构造函数/初始化程序或init 方法中)...此外,您可以在事件处理程序(System.out.println/Platform.exit)中做更简单的事情,关键帧的时间也不适合测试。让我们尽可能简单地查找/复制问题是一个好主意。

标签: java events javafx timer javafx-2


【解决方案1】:

获取RxJavaFx 并运行代码。在 4 秒不活动(没有任何事件)后,它将关闭应用程序。

    import java.util.concurrent.TimeUnit;

    import io.reactivex.Observable;
    import io.reactivex.schedulers.Schedulers;
    import io.reactivex.subjects.PublishSubject;
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.input.InputEvent;
    import javafx.stage.Stage;
    import javafx.stage.WindowEvent;

    public class CloseAfterApp extends Application {


        public static void main(String[] args) {
            launch(args);
        }

        @Override
        public void start(Stage stage) throws Exception {
            Scene scene = new Scene(new TextField());

            PublishSubject<InputEvent> sceneEventPublishable = PublishSubject.create();
            PublishSubject<WindowEvent> windowEventPublishable = PublishSubject.create();

            scene.addEventFilter(InputEvent.ANY, sceneEventPublishable::onNext);
            stage.addEventFilter(WindowEvent.ANY, windowEventPublishable::onNext);

            Observable.merge(sceneEventPublishable, windowEventPublishable)
            .switchMap(event -> Observable.just(event).delay(4, TimeUnit.SECONDS, Schedulers.single()))
            .subscribe(event -> Platform.exit());

            stage.setScene(scene);
            stage.show();
        }
    }

【讨论】:

  • 我正在使用 Eclipse 平台,并将 rxjava-2.2.12.jar 添加到引用的库中。它显示 java.lang.ClassNotFoundException: org.reactivestreams.Publisher 。你能帮我解决一下吗?
  • 获取 RxJavaFx.jarRxJava.jar 并作为引用库添加到您的项目中。
  • 我把rxjava-2.1.6.jarrxjavafx-2.2.2.jar然后执行程序我还是发现错误导致作者:java.lang.ClassNotFoundException:org.reactivestreams.Publisher 在控制台
  • 我忘记了 RxJava 依赖项。请将reactive-streams.jar 添加到您的库中。如果使用 maven 或 gardle 会容易得多:)
  • :) 好吧,我工作得很好。这真的是我今天所期望的。非常感谢@Przemek Krysztofiak。我希望每个人都能自己找到这篇文章。它实际上很有价值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 2012-04-08
  • 1970-01-01
相关资源
最近更新 更多