简体   繁体   English

只显示一次消息

[英]Displaying a message one time only

I want to find a way to display a notification (like a JOptionpane , a JLabel or any other type) only one time after a user launches my application that is formed in a .jar file.我想找到一种方法,在用户启动我的.jar文件中形成的应用程序后显示一次通知(如JOptionpaneJLabel或任何其他类型)。

By only one time , I mean that the user gets a one-time notification after the first use, then for every following times my application runs, this notification should not appear.只有一次,我的意思是用户在第一次使用后会收到一次性通知,然后在我的应用程序每次运行后,都不应该出现此通知。

My application uses Java Swing .我的应用程序使用Java Swing Is there a hint how to make a message pops up from the main JFrame for example?例如,是否有提示如何从主JFrame弹出消息?

You simply need to know whether this application has already been running in that environment before or not.您只需要知道此应用程序之前是否已经在该环境中运行过。 A simple way to do that is to:一个简单的方法是:

  • Check whether some file with a particular name exists in the working directory检查工作目录中是否存在具有特定名称的文件
    • if it doesn't : show your notification, then create the file如果没有显示您的通知,然后创建文件
    • if it does : don't show your notification如果不显示您的通知

Sample Java code:示例Java代码:

private static void notify() {
    final File file = new File(".launched");

    if(!file.exists()) {

        // show your notification HERE

        file.createNewFile();
    }
}
  1. Check for stored value on disk indicating the message has been shown检查磁盘上的存储值,表明消息已显示
  2. If not, show the message and store the value on disk.如果没有,则显示消息并将值存储在磁盘上。

You can do that by setting up one preference.您可以通过设置一个首选项来做到这一点。 I think is the most straight forward way to do it.我认为这是最直接的方法。 Use the preferences class.使用首选项类。

The preferences are loaded at starting, then you ask if your "boolean_first_use" is false or true.首选项在开始时加载,然后您询问您的“boolean_first_use”是假还是真。 After that you set it to false, as you know that the user is having that first time message.之后,您将其设置为 false,因为您知道用户正在收到第一条消息。 So next time, it will not fire the notification.所以下一次,它不会触发通知。

http://www.vogella.com/tutorials/JavaPreferences/article.html http://www.vogella.com/tutorials/JavaPreferences/article.html

http://www.javaranch.com/journal/2002/10/preferences.html http://www.javaranch.com/journal/2002/10/preferences.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM