2014-09-25 37 views
0

我的程序會檢查並確保輸入的密碼正確。如果密碼正確,那麼你就進入下一個窗口。如果它與密碼不匹配,則應該返回到登錄屏幕。我的問題是我不能回到登錄屏幕,我會喜歡一些幫助。如何使我的登錄屏幕是一個int輸入,並在那裏我已經登錄信息如何用Java登錄

package scanlogin; 

//Imports are listed in full to show what's being used 
//could just import javax.swing.* and java.awt.* etc.. 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.JPasswordField; 
import javax.swing.JPanel; 
import javax.swing.JLabel; 
import java.awt.GridLayout; 
import java.util.Arrays; 
import java.awt.EventQueue; 


public class ScanScreen { 

    //Note: Typically the main method will be in a 
    //separate class. As this is a simple one class 
    //example it's all in the one class. 
    public static void main(String[] args) { 

     //Use the event dispatch thread for Swing components 
     EventQueue.invokeLater(new Runnable(){ 
      public void run(){ 
       createGuiFrame();   
      } 
     });  
    } 

    //handles the creation of the JFrame and 
    //all it's components 
    private static void createGuiFrame(){ 
     JFrame guiFrame = new JFrame(); 
     //make sure the program exits when the frame closes 
     guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guiFrame.setTitle("Logging Information"); 
     guiFrame.setSize(1000,600); 

     //This will center the JFrame in the middle of the screen 
     guiFrame.setLocationRelativeTo(null); 
     guiFrame.setVisible(true); 

     //Using a JPanel as the message for the JOptionPane 
     JPanel userPanel = new JPanel(); 
     userPanel.setLayout(new GridLayout(2,2)); 

     JLabel usernameLbl = new JLabel("Username:"); 
     JLabel passwordLbl = new JLabel("Password:"); 
     JTextField username = new JTextField(); 
     JPasswordField passwordFld = new JPasswordField(); 

     userPanel.add(usernameLbl); 
     userPanel.add(username); 
     userPanel.add(passwordLbl); 
     userPanel.add(passwordFld); 

     //As the JOptionPane accepts an object as the message 
     //it allows us to use any component we like - in this case 
     //a JPanel containing the dialog components we want 
     int input = JOptionPane.showConfirmDialog(guiFrame, userPanel, "Login Page",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 

     char[] correctPassword = {'R','a','T'}; 
     // Retrieve password 
     char[] enteredPassword = passwordFld.getPassword(); 

     if (input == 0){ //OK Button = 0 
      if (Arrays.equals(correctPassword, enteredPassword)){ 
       JOptionPane.showMessageDialog(null, "Password is correct!"); 
      }else{ 
       JOptionPane.showMessageDialog(null, "Invalid username or password!"); 
      } 

      //Note: best practice is to zero out the array 
      //that contains the password. (Bit silly here 
      //as I'm printing the password to show how it's retrived 
      //but keep it in mind when using JPasswordFields.) 
      Arrays.fill(enteredPassword, '0'); 
     }else{ 
      //either the cancel button or the 'x' 
      //has been pressed 
      System.exit(0); 
     } 
    } 
} 

回答

1

這只是排序的如何做到這一點的例子。你的情況我會更復雜(即你需要確認額外的領域等),但基本概念仍然

String CORRECT_PASSWORD = ...; do { String pwd = JOptionPane.showInputDialog("Enter Password"); } while (!pwd.equals(CORRECT_PASSWORD));