2014-04-05 78 views
0

應用程序將要求用戶的ID和密碼,以及他們是現有的還是新的用戶,如何創建一個GUI java的密碼和ID。如果您是新用戶,則會提示您重新輸入您的詳細信息以確認ID和密碼,然後將它們存儲在陣列中。 一旦用戶點擊登錄按鈕,應用程序將搜索一系列ID和密碼進行驗證。 (將您的陣列設置爲包含最多10個用戶 - 因爲它是一個小陣列可以使用線性搜索) 然後應用程序將顯示適當的消息。如何使用陣列

到目前爲止我的代碼

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

public class Log extends JFrame 
{ 
    public static void main(String[] args) 
    { 
     Log frameTabel = new Log(); 
    } 


     JLabel title = new JLabel("Please type your ID and Password"); 
     JLabel id = new JLabel("ID:"); 
     JLabel psword = new JLabel("Password:"); 
     JButton blogin = new JButton("Login"); 
     JPanel panel = new JPanel(); 
     JTextField txuser = new JTextField(15); 
     JPasswordField pass = new JPasswordField(15); 

     JRadioButton radNew = new JRadioButton("New", true); 
     JRadioButton radExisting = new JRadioButton("Existing", false); 

     ButtonGroup radioGroup1 = new ButtonGroup(); 

     Log() 
     { 
      super("Week 9 Question 3"); 
      setSize(300,250); 
      setLocation(600,250); 
      panel.setLayout (null); 

      title.setBounds(40,5,300,20); 
      id.setBounds(52,40,150,20); 
      txuser.setBounds(70,40,150,20); 
      psword.setBounds(7,80,150,20); 
      pass.setBounds(70,80,150,20); 
      blogin.setBounds(180,130,80,20); 

      radNew.setBounds(30, 130, 50, 20); 
      radExisting.setBounds(90, 130, 90, 20); 

      panel.add(title); 
      panel.add(id); 
      panel.add(psword); 
      panel.add(blogin); 
      panel.add(txuser); 
      panel.add(pass); 
      add(radNew); 
      add(radExisting); 
      radioGroup1.add(radNew); 
      radioGroup1.add(radExisting); 

      map.put("test", "12345"); 


      getContentPane().add(panel); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setVisible(true); 
      actionlogin(); 
     } 

     public void actionlogin() { 
      blogin.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent ae) { 
        String puname = txuser.getText(); 
        String ppaswd = new String(pass.getPassword()); 

         JOptionPane.showMessageDialog(null, "Correct Information", "Correct", 
           JOptionPane.INFORMATION_MESSAGE); 
        } else { 
         JOptionPane.showMessageDialog(null, "No Password/ID Found on System", 
           "Incorrect", JOptionPane.INFORMATION_MESSAGE); 
         txuser.setText(""); 
         pass.setText(""); 
         txuser.requestFocus(); 

         if (radNew.isSelected()) { 

          } 

          JOptionPane.showMessageDialog(null, 
            "Re-enter your password and ID to confirm", "Information Saved", 
            JOptionPane.INFORMATION_MESSAGE); 
         } 
        } 
} 

      }); 
     } 
} 
+0

刪除'map.put( 「」, 「」);'。如果用戶沒有在ID和密碼中輸入任何值,仍會顯示正確的信息。 – Braj

+1

什麼問題? – MadProgrammer

回答

1

使用

char[] ppaswd = pass.getPassword(); 

,而不是

String ppaswd = pass.getText(); 

因爲

的方法得到來自類型JPasswordField的Text()已被棄用。

由於@MadProgrammer說:

密碼存儲在字符數組,你永遠不將其轉換回字符串。

字符串生活在JVM的JVM生活中,使討厭的人更容易檢查字符串池並找到密碼。


使用Map存儲用戶名/密碼。

Map<String, char[]> map = new HashMap<String, char[]>(); 

放一些初始值

map.put("test", "12345".toCharArray()); 
    map.put("admin", "admin".toCharArray()); 

這裏是修改後的代碼

public void actionlogin() { 
    blogin.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
      String puname = txuser.getText(); 
      char[] ppaswd = pass.getPassword(); 
      if (map.keySet().contains(puname) && Arrays.equals(map.get(puname), ppaswd)) { 
       JOptionPane.showMessageDialog(null, "Correct Information", "Correct", 
         JOptionPane.INFORMATION_MESSAGE); 
      } else { 
       JOptionPane.showMessageDialog(null, "No Password/ID Found on System", 
         "Incorrect", JOptionPane.INFORMATION_MESSAGE); 
       txuser.setText(""); 
       pass.setText(""); 
       txuser.requestFocus(); 

       if (radNew.isSelected()) { 
        if (!puname.equals("") && ppaswd.length > 0) { 
         map.put(puname, ppaswd); 
        } 

        JOptionPane.showMessageDialog(null, 
          "Re-enter your password and ID to confirm", "Information Saved", 
          JOptionPane.INFORMATION_MESSAGE); 
       } 
      } 
     } 
    }); 
} 
+0

如果您想根據您的要求更改邏輯,請讓我知道。 – Braj

+0

非常感謝您的幫助,我非常感謝。你回答的方式清晰,準確,110%謝謝。這裏是更新後的代碼,現在唯一的問題是我的問題是:如果您是新用戶,您將提示重新輸入您的詳細信息以確認ID和密碼,然後將它們存儲在數組中。 一旦用戶點擊登錄按鈕,應用程序將搜索一系列ID和密碼進行驗證。 (將您的陣列設置爲包含最多10個用戶 - 因爲它是一個小陣列可以使用線性搜索) –

+0

然後應用程序將顯示適當的消息。它如何存儲到數組中,或者它只在程序運行一次時纔會生效? –