【问题标题】:warning appears more than once警告出现多次
【发布时间】:2013-02-24 16:49:34
【问题描述】:

只是一个简单的问题,如果我将大写锁定保持在上面,它会出现 3 次警告消息?我只希望它出现一次

            jButton1.addActionListener(new ActionListener() {
               @SuppressWarnings("deprecation")
               public void actionPerformed(ActionEvent evt) {
                  for( int i=0; i < users.size(); i++ ){
                     temp = (logins)users.elementAt(i);
                     if( Toolkit.getDefaultToolkit().
                        getLockingKeyState(KeyEvent.VK_CAPS_LOCK))
                     {
                        check = "true";
                        {
                           JOptionPane.showMessageDialog(
                              null, "Please turn off the capslock", "Error",
                              JOptionPane.ERROR_MESSAGE );
                        }
                     }
                  }
               }
            });
            pack();
            this.setSize(643, 434);
         }
         catch (Exception e) {
         e.printStackTrace();
      }
   }
}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • “警告信息 3 次” users.size() 的值是多少?我敢打赌它有 3 个元素。

标签: java eclipse swing warnings capslock


【解决方案1】:

你已经完成了

check = true;

但从未检查过,将其添加到if

if( Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)
   && ! check )
{
   check = "true";
   ...

或者,更好的是,将其放在循环之外,因为 if 的条件不使用迭代的数据...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2018-07-09
    相关资源
    最近更新 更多