2013-02-23 70 views
0

只是一個簡單的問題,如果我把大寫鎖定在它上面會出現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(); 
     } 
    } 
} 
+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-02-23 16:31:30

+0

*「Warning message 3 times」* users.size()'的值是多少?我敢打賭它有3個元素。 – 2013-02-23 16:33:30

回答

2

你做

check = true; 

,但它從來沒有檢查,將其添加到if

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

,或者更好,把它放在循環外因爲if的條件不使用迭代的數據...