【问题标题】:Reusable Delete Confirmation with JTextField使用 JTextField 的可重用删除确认
【发布时间】:2013-04-15 20:25:01
【问题描述】:

我正在尝试创建一个删除对象的确认窗口,该窗口要求用户在文本字段中输入“删除”一词以确认他们的操作,然后单击标有“删除”的按钮。此外,它还有标准的“取消”按钮。

以下是我想要的基本概念,但我不确定如何正确返回布尔值:

public static boolean confirmDelete(String msg) {
    JPanel panel = new JPanel();
        JPanel sPanel1 = new JPanel();
            JPanel ssPanel1 = new JPanel();
                ssPanel1.setLayout(new BoxLayout(ssPanel1, BoxLayout.Y_AXIS));
                    JLabel lbl = new JLabel(msg);
                    confirm = new JTextField(10);
                ssPanel1.add(lbl);
                ssPanel1.add(confirm);
            JPanel ssPanel2 = new JPanel();
                ssPanel2.setLayout(new BoxLayout(ssPanel2, BoxLayout.Y_AXIS));
                    JButton ok = new JButton("Delete");
                        ok.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent arg0) {
                                if(confirm.getText().toLowerCase().equals("delete")) {
                                    //Set the returned value to true;
                                } else {
                                    alertMsg("Invalid input. Please try again.");
                                }
                            }
                        });
                    JButton cancel = new JButton("Cancel");
                        cancel.addActionListener(new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent arg0) {
                                //Set the returned value to false;
                            }
                        });
                ssPanel2.add(ok);
                ssPanel2.add(cancel);
            sPanel1.add(ssPanel1);
            sPanel1.add(ssPanel2);
        panel.add(sPanel1);

    JFrame deleteFrm = new JFrame("Confirm Delete");
    deleteFrm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    //Add content to the window.
    deleteFrm.setContentPane(panel);

    //Display the window.
    deleteFrm.pack();

    deleteFrm.setVisible(true);
}

基本上,我想使用boolean deleteItem = ClassName.confirmDelete(msg); 行调用此窗口并让它返回布尔值以说明他们是否正确确认了删除状态。如何设置它以返回代码示例中描述的布尔值(这是一种伪代码,因为它显然不正确)。像上面提到的单行调用,这甚至可能吗?

【问题讨论】:

  • 对代码块使用一致且符合逻辑的缩进。代码的缩进是为了帮助大家理解程序流程!
  • 我在使用 UI 时使用缩进来显示嵌套对象,以便在对象位于另一个对象内部时更容易跟踪对象的放置位置,并且更容易找到我需要添加/编辑的位置/删除一个项目(尽管我也可能对缩进的项目属性感到厌烦)。这是我为自己做的事情,以使将来的编辑更容易,但我忘记为帖子删除它,抱歉。

标签: java swing user-input confirmation


【解决方案1】:

使用 JOptionPane 代替.. 将更容易获得用户选择结果 然后决定逻辑代码.. 看这里例如JOptionPane YES/No Options Confirm Dialog Box Issue -Java 或在这里 google search results: JOPtionPane examples

【讨论】:

    【解决方案2】:

    我找到了我的问题here 的答案,它比我想象的要简单得多。结果,我想出了以下代码来返回一个我想要的布尔值,而无需构建我自己的窗口所涉及的所有工作:

    public static boolean confirmDelete(String msg) {
        String str = JOptionPane.showInputDialog(msg);
        if(str != null && str.toLowerCase().equals("delete")) return true;
        return false;
    }
    

    Elior 的回答虽然不是我想要的,但确实为我指明了正确的方向,因此 +1。不知道我是怎么错过 JOptionPane 的 showInputDialog() 方法的,但它基本上就是我要找的(不仅仅是确认是/否;数据太敏感了,不能这么简单)。

    编辑:添加str != null &&,否则在取消时会抛出nullPointerException

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-11
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多