由于不活动而自动关闭JavaFX应用程序

编程入门 行业动态 更新时间:2024-10-09 08:27:48
本文介绍了由于不活动而自动关闭JavaFX应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果用户一段时间不活动,我想关闭我的JavaFX应用程序.我在Swing中有此代码,并且我想在JavaFX中做同样的事情.如果在指定的时间内未发生任何事件,则此类会将用户重定向到登录面板.

I want to close my JavaFX application if the user is inactive for a period of time. I have this code in Swing and I want to do the same in JavaFX. This class redirect the user to the login panel if no event happens during the time indicated.

import javax.swing.Timer; public class AutoClose { private Timer timer; public AutoClose(JFrame centralpanel) { EventQueue.invokeLater(new Runnable() { @Override public void run() { Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { @Override public void eventDispatched(AWTEvent event) { Object source = event.getSource(); if (source instanceof Component) { Component comp = (Component) source; Window win = null; if (comp instanceof Window) { win = (Window) comp; } else { win = SwingUtilities.windowForComponent(comp); } if (win == centralpanel) { timer.restart(); } } } }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK); timer = new Timer(3600000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { centralpanel.dispose(); //Redirect to the login panel. Login login = new Login(); login.setVisible(true); timer.stop(); JOptionPane.showMessageDialog(null,"Connection closed due to inactivity"); } }); timer.start(); } }); } }

推荐答案

我已经完成了这段代码,现在它可以按照我的要求正确地工作.

I have done this code and now it works correctly as I wanted.

public class AutoClose { private Timeline timer; public AutoClose(VBox mainPanel) { timer = new Timeline(new KeyFrame(Duration.seconds(3600), new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent evt) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Inactivity"); alert.setHeaderText("Connection closed due to inactivity"); alert.show(); try { Stage mainWindowStage = Login.getPrimaryStage(); Parent root = FXMLLoader.load(getClass().getResource("/view/Login.fxml")); Scene scene = new Scene(root); mainWindowStage.setScene(scene); mainWindowStage.show(); timer.stop(); } catch (IOException ex) { } } })); timer.setCycleCount(Timeline.INDEFINITE); timer.play(); mainPanel.addEventFilter(MouseEvent.ANY, new EventHandler<Event>() { @Override public void handle(Event event) { timer.playFromStart(); } }); } }

更多推荐

由于不活动而自动关闭JavaFX应用程序

本文发布于:2023-11-03 05:07:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   自动关闭   JavaFX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!