如何用java创建一个“不要再问我”对话框弹出框?

时间:2015-06-30 01:01:18

标签: java swing joptionpane

在我的程序的一部分中,我使用JOptionPane询问用户是否确定他将做什么。但是我不想让用户每次尝试都要求这样我喜欢使用android中的一些对话框的功能,这些对话框带有“不要再问”,但不知道如何在我的程序中实现它,有人在这里你可以帮助我吗? (应该有一个西班牙语StackOverflow) 这是我的代码

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 

    }

3 个答案:

答案 0 :(得分:8)

基本思想是利用message参数实际上可以是Component的事实。问题接着是检查用户是否选择了“不要再问我”选项,存储并重新使用它

像...一样的东西。

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等)

然后你就可以使用像...这样的东西了。

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);

如果您在第一个提示中选择“不要再问我”,那么第二个电话将返回先前选择的值

现在,在某个地方,你可能希望能够扭转这些决定;)

答案 1 :(得分:5)

JOptionPane还允许将Component显示为消息。所以这个问题可以通过这种方式解决:

JPanel msgPanel = new JPanel();

JLabel msg = new JLabel("some message");
msgPanel.add(msg);

JCheckBox jcb = new JCheckBox("do not ask me again");
msgPanel.add(jcb);

JOptionPane.showConfirmDialog(null , msgPanel , "some title" , JOptionPane.OK_CANCEL_OPTION);

答案 2 :(得分:2)

在这里使用了一些答案,我设法解决了我的问题,我将在这里分享,以防其他人可能感兴趣:

我的复选框仅在当前会话期间有效以重置聊天窗口,该复选框位于"选项"我的应用程序的一部分,单击重置按钮时会出现另一个应用程序。

按钮的ActionListener的代码如下所示,带有必要的注释:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

jButtonReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //this checkbox is the one available in app settings
                if (!surpassResetConfirmCheckBox.isSelected()) {
                    JPanel msgPanel = new JPanel();

                    // message inside confirmation dialog
                    JLabel msg = new JLabel("Are you sure you wish to clear current conversation?");
                    msgPanel.add(msg);

                    JCheckBox jcb = new JCheckBox("do not ask me again.");
                    jcb.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            // if-else statement below is used to update checkbox in settings whenever this one is changed.
                            if (jcb.isSelected()) {
                                surpassResetConfirmCheckBox.setSelected(true);
                            } else {
                                surpassResetConfirmCheckBox.setSelected(false);
                            }

                        }
                    });
                    msgPanel.add(jcb);

                    // JOptionPane returns an integer value ,we'll use it later
                    int result = JOptionPane.showConfirmDialog(null, msgPanel, "Reset Conversation",
                            JOptionPane.OK_CANCEL_OPTION);

                    // check user's response..
                    if (result == JOptionPane.OK_OPTION) {
                        // if user clicked Ok , call your method here
                        // reset();
                    } else if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.CLOSED_OPTION) {
                        // if user closed the dialog or clicked cancel, uncheck the checkbox in settings 
                        surpassResetConfirmCheckBox.setSelected(false);
                    }
                } else {
                    // if checkbox in settings is checked, call your method immediately 
                    // reset();
                }
            }
        });
    }