在我的软件中,当应用程序在该系统中第一次运行时,我想在joptionpane中显示一条消息“welcome”。我不希望在第二次或更长时间内收到此消息。仅在应用程序使用netbeans在该系统中第一次运行时才需要一次。
答案 0 :(得分:1)
您可以在系统中的某个位置创建文件(例如在用户主目录中),只有在不存在时才创建该文件。
File file = new File(System.getProperty("user.dir") +"/.launch_first_time");
if(!file.exist()) {
file.createNewFile();
JOptionPane.showMessageDialog (null, "welcome", "Launch for the first time", JOptionPane.INFORMATION_MESSAGE);
}
每次使用WindowsListener
打开应用程序时,都可以运行此代码答案 1 :(得分:1)
这可能是Preferences的一个很好的用例:
Preferences prefs = Preferences.userNodeForPackage(getClass());
boolean hasRunBefore = prefs.getBoolean("hasRunBefore", false);
if (!hasRunBefore) {
prefs.putBoolean("hasRunBefore", true);
JOptionPane.showMessageDialog(mainWindow,
"Welcome to ExampleApp!", "Welcome",
JOptionPane.INFORMATION_MESSAGE,
applicationIcon);
}