【问题标题】:popUp method repeats when I repeat the process Twice or More当我重复该过程两次或更多次时,popUp 方法重复
【发布时间】:2015-03-23 10:34:16
【问题描述】:

当我第一次输入错误密码时,showPopUpPassword 工作正常,但当我第二次或更多次重复密码时。它仍然在 JOptionPane.showMessageDialog(null, "incorrect password"); 之后弹出。

    table.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) 
                {
                    popup.show(table, e.getX(), e.getY());
                    EditProfile.addActionListener(new ActionListener() {            
                        public void actionPerformed(ActionEvent e) {

                            int row = table.getSelectedRow();
                            String name = (String) table.getModel().getValueAt(row, 0);

                            if (!namelist.contains(name)) {

                                String pass =   ctr.getNamebyPassword(name, password);      // get password on database
                                password = showPopUpPassword();                             // get the user input password

                                if(!pass.equals(password)) {
                                    JOptionPane.showMessageDialog(null, "incorrect password");  

                                }else if (pass.equals(password)){
                                    edit =  new editProfileFrame(ctr.getData(name), ctr.getAccount(name));
                                    namelist.add(name);
                                }

                            }else {
                                JOptionPane.showConfirmDialog(null, "Cannot Duplicate Profile Window");
                                }



                        }
                    });
                }
        }
    });

【问题讨论】:

  • 为了获得更好的帮助,请尽快发布 SSCCE/MCVE,简短、可运行、可编译,在局部变量中包含 JTable/XxxTableModel 的硬编码值
  • 不要使用if (e.getButton() == MouseEvent.BUTTON3)。人们可能不知道 button3 是什么。相反,您可以使用:if (SwingUtilities.isRightMouseButton(e).

标签: java swing jtable joptionpane jpopup


【解决方案1】:

试试这个,

while(true){
    password = showPopUpPassword();                         
    if(!pass.equals(password))
        JOptionPane.showMessageDialog(null, "incorrect password");  
    else
        break;
}

edit =  new editProfileFrame(ctr.getData(name), ctr.getAccount(name));
namelist.add(name);

【讨论】:

  • endles loop 在任何编程语言中都不是很好的建议,阅读Oracle教程如何使用JDialog,关于JOptionPane的部分
【解决方案2】:

有人可能会读到这个问题。JoptionPane 重复的问题是因为 EditProfile 按钮位于 MousePressedEvent 内。

只需将按钮放在 MousePressedEvent 之外。

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2014-12-30
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多