如何使用Java创建“不再询问我"对话框弹出框? [英] How to make a “do not ask me again” dialog pop-up box with java?

查看:60
本文介绍了如何使用Java创建“不再询问我"对话框弹出框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序的一部分中,我使用JOptionPane询问用户是否确定他会做什么.但是我不想让用户每次尝试时都感到无聊,所以我喜欢使用android中某些对话框的功能,这些对话框带有不再询问",但是不知道如何在我的程序中实现,这里有人可以帮我吗? (应具有西班牙语StackOverflow) 这是我的代码

In one part of my program I use a JOptionPane to ask the user if they are sure of what he will do. But I do not want to bore the user asking that every time you try so I like to use the function of some dialog boxes in android that come with the "Do not ask again", but do not know how to implement that in my program, someone here you could help me? (Should have a Spanish StackOverflow) This is my code

if (jCheckBox2.isSelected() && jCheckBox1.isSelected()){
        JOptionPane.showConfirmDialog(null, "This action can cause problems, want to do it?");
        //here must be something to never ask again this 

    }

推荐答案

基本思想是利用message参数实际上可以是Component的事实.然后问题归结为检查用户是否选择了不再问我"选项,存储并重新使用它

The basic idea is to take advantage of the fact the message parameter can actually be a Component. The problem then comes down to checking to see if the user selected the "Don't ask me again" option, storing and re-using it

类似...

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DontAskMeAgainPanel extends JPanel {

    private JCheckBox dontAskMeAgain;

    public DontAskMeAgainPanel(Object message) {
        setLayout(new BorderLayout());
        if (message instanceof Component) {
            add((Component) message);
        } else if (message != null) {
            add(new JLabel(message.toString()));
        }
        dontAskMeAgain = new JCheckBox("Don't ask me again");
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel.add(dontAskMeAgain);
        add(panel, BorderLayout.SOUTH);
    }

    public boolean dontAskMeAgain() {
        return dontAskMeAgain.isSelected();
    }

    private static Properties settings;

    protected static void loadProperties() {
        if (settings != null) {
            settings = new Properties();
            File source = new File("...");
            if (source.exists()) {
                try (Reader r = new FileReader(source)) {
                    settings.load(r);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        }
    }

    protected static void saveProperties() {
        if (settings != null) {
            settings = new Properties();
            File source = new File("...");
            try (Writer w = new FileWriter(source)) {
                settings.store(w, "Don't prompt for settings");
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }
    }

    public static int showConfirmDialog(Component parent, Object message, String key) {

        loadProperties();

        int result = JOptionPane.NO_OPTION;

        if (settings.containsKey(key + ".prompt") && !Boolean.parseBoolean(settings.getProperty(key + ".value"))) {
            result = Integer.parseInt(settings.getProperty(key + ".value"));
        } else {
            DontAskMeAgainPanel panel = new DontAskMeAgainPanel(message);
            result = JOptionPane.showConfirmDialog(parent, panel);
            if (panel.dontAskMeAgain()) {
                settings.put(key + ".prompt", "false");
                settings.put(key + ".value", Integer.toString(result));

                saveProperties();
            }
        }
        return result;
    }

}

作为基本出发点.为了简单起见,我使用Properties作为后备存储,您可以使用数据库或其他持久性方法(PreferencesXML等)

As a basic starting point. I've used Properties as the backing store for simplicity, you could use a database or other persistent method (Preferences, XML, etc)

那么您就可以使用类似...

Then you could just use it something like...

int result = DontAskMeAgainPanel.showConfirmDialog(null, "This is annoying", "Annoying");
System.out.println("You selected " + result);
result = DontAskMeAgainPanel.showConfirmDialog(null, "This is annoying", "Annoying");
System.out.println("Then you selected " + result);

如果在第一个提示下选择不再询问我",则第二个呼叫将返回先前选择的值

If you select "Don't ask me again" at the first prompt, then the second call will return the previously selected value

现在,在某个地方,您可能会想要有能力逆转这些决定;)

Now, somewhere, you're probably going to want to have the ability to reverse these decisions ;)

这篇关于如何使用Java创建“不再询问我"对话框弹出框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆