2014-02-26 26 views
1

我有一個程序,它是一個非常基本的登錄:有沒有辦法將文件中的類實現爲新文件?

import javax.swing.JOptionPane.*; 
import java.lang.Math.*; 
import java.lang.System.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.JFrame; 

public class UserLog extends JFrame 

{ 

public static void main(String[]Args) throws InterruptedException 
    { 
    boolean isValid=false; 
    while(!isValid) 
     { 
    // Components related to "login" field  
    JLabel label_loginname = new JLabel("Enter your login name:");  
    JTextField loginname = new JTextField(15);  
    // loginname.setText("EnterLoginNameHere"); 
    // Pre-set some text  
    // Components related to "password" field  
    JLabel label_password = new JLabel("Enter your password:");  
    JPasswordField password = new JPasswordField();  
    // password.setEchoChar('@'); 
    // Sets @ as masking character  
    // password.setEchoChar('\000'); 
    // Turns off masking  
    JCheckBox rememberCB = new JCheckBox("Remember me"); 

    Object[] array = {label_loginname, 
    loginname,      
    label_password,      
    password,      
    rememberCB}; 
    Object[] options = {"Login", "Cancel"}; 
    int res = JOptionPane.showOptionDialog(null, 
              array, 
              "Login", 
              JOptionPane.YES_NO_OPTION, 
              JOptionPane.QUESTION_MESSAGE, 
              null,  //do not use a custom Icon 
              options, //the titles of buttons 
              options[0]); //default button title 

    // User hit Login  
    if (res == 0) 
     { 
      System.out.println("Login"); 
     }  
    // User hit CANCEL  
    if (res == 1) 
     { 
      System.out.println("Canceled"); 
     }  
    // User closed the window without hitting any button  
    if (res == JOptionPane.CLOSED_OPTION) 
     { 
      System.out.println("CLOSED_OPTION"); 
     } 


    // Output data in "login" field, if any  
    String newloginname = loginname.getText();  
    String newpassword = new String(password.getPassword());  
    if (newloginname.equalsIgnoreCase("Cody_Coulter") && newpassword.equals("cheche1")) 
     { 
      System.out.println("Login Successful!"); 
      boolean selectedCB = rememberCB.isSelected();  
      System.out.println("selectedCB: " + selectedCB); 
      Thread.sleep(3000); 
      Object[] array1= {"It's about time to choose"}; 
      Object[] options1= {"Leave", "Keep Going"}; 
      int res1 = JOptionPane.showOptionDialog(null, 
              array1, 
              "There", 
              JOptionPane.YES_NO_OPTION, 
              JOptionPane.QUESTION_MESSAGE, 
              null,  //do not use a custom Icon 
              options1, //the titles of buttons 
              options1[0]); //default button title 
      if(res1==1) 
       { 
        Object[] options2 = {"Answers for Algebra", 
             "Answers for APUSH", 
             "Answers for Computer Science"}; 
        Object[] array2={"Pick Your Poison:"}; 
        int res2= JOptionPane.showOptionDialog(null, 
               array2, 
               "This", 
               JOptionPane.YES_NO_OPTION, 
               JOptionPane.QUESTION_MESSAGE, 
               null,  //do not use a custom Icon 
               options2, //the titles of buttons 
               options2[0]); //default button title 
        if (res2 == 0) 
        { 
         JOptionPane.showMessageDialog(null, "Nigguh you a cheatuh"); 
        }  
        else 
        if (res2 == 1) 
        { 
         JOptionPane.showMessageDialog(null, "Nigguh, who's dumb enough to need to cheat in APUSH"); 
        } 
        else 
        if (res2 == 2) 
        { 
         JOptionPane.showMessageDialog(null, "Nigguh, you dumb"); 
        }  

        String name1 = JOptionPane.showInputDialog(null, 
                 "What is your name?"); 
        int length = 0; 
        length = newpassword.length(); 
        String Pass = "*"; 
        newpassword =newpassword.replaceAll(".","*"); 
        System.out.println("Username: "+newloginname+"\nPassword: "+ 
             newpassword+"\nName: "+name1); 
       } 

     } 
    else { 
      JOptionPane.showMessageDialog(null,"Wrong Username or Password!"); 
      isValid=false; 
     } 

     } 
    // Output data in "password" field, if any  
    // Output state of "remember me" check box  

    } 

} 

我想要做的就是創建另一個程序,如文件共享,文件訪問,甚至是基本的遊戲,但能夠有此登錄當然爲了登錄而實施。有沒有辦法實現這個代碼,而不必複製和粘貼到另一個代碼作爲該文件中的單獨的類? 例子:

public class NewGame{ 
    public static void main(String[] args) 
    { 
     new UserLog(); 
    } 

當然這可能不是正確的syntactually,但是這是它的要點。

謝謝,如果我需要重新編輯它或編輯問題/格式,請告訴我! :)

編輯 使當前的主要方法的正規公立下課後,和新公共類所以爲了打電話,通過新的主

public class gameLogin 
{ 
public static void main(String[]args) 
{ 
    userLogin(); 
} 
public class userLogin() 
{ 
    // current code, evidently seen in the current main 
} 
// rest of code 

引用原始文件,用戶日誌,我將不得不(在新文件中:gameLogin)使用 userLog();

,或者它會更好地使用

userLog.userLogin("Munkeeface", "password"); 

回答

1

最簡單的方法是將您的main中的所有代碼簡單地移到靜態實用程序類函數中,然後從其他類main中調用該函數。例如:

public class LoginToMyWebsite { 
    public static final void main(String[] ignored) { 
     LoginToWebsiteUtil.login("myname", "password", ...) 
    } 
}   

唯一棘手的事情將回答這個問題:

public class LoginToWebsiteUtil { 
    public static final void login(String username, String password, ...) { 
     //CODE GOES HERE 
    } 
} 

並在使用它「?哪些變量保存狀態」這些變量必須在實用程序類中聲明爲靜態類字段。這是因爲,一旦函數結束,所有狀態(如登錄連接)都將終止。爲了保持它(「保持其狀態」),這些狀態變量需要具有比函數的生命週期更大的範圍。

例如,而不是

public class LoginToWebsiteUtil { 
    public static final void login(String username, String password, ...) { 
     Connection conn = getConnectionFromLogin(username, password); 

     //and so on... 

它必須是

public class LoginToWebsiteUtil { 
    private static Connection conn = null; 
    public static final void login(String username, String password, ...) { 
     conn = getConnectionFromLogin(username, password); 

     //and so on... 

或者,你可以把所有的代碼從原始main功能到一個新的類,這樣的構造作爲

public class UserLogin { 
    private static Connection conn = null; 
    public UserLog(String username, String password, ...) { 
     conn = getConnectionFromLogin(username, password); 
     //CODE HERE 
    } 
} 

但是,正如你所看到的,你仍然有「什麼持有狀態?」問題。

(這是一個很好的問題。這聽起來像這個登錄密碼是在未來你可能有用。)

+0

使當前的主要方法的正規公立下課後,和新公共類調用,通過新的主 'public static void main(String [] args) { userLogin(); } 公共類USERLOGIN(){ // 當前類 }「 OK,所以爲了引用原始文件,用戶日誌,我會(在新文件:gameLogin)使用用戶日誌() ;或者它會更好地使用userLog.userLogin(「Munkeeface」,「密碼」);? – Munkeeface

+0

對不起,我不知道如何使插入的代碼在以前的評論中脫穎而出,但我希望它是有道理的 – Munkeeface

+0

將此代碼和您的問題追加到原始帖子的底部。您可以正確地闡述和格式化代碼。但是如果我理解你,你可以調用'UserLog login = new UserLog(username,password);'。而且,類名實際上應該是大寫的。 'UserLog',而不是'userLog'。 (使用重音標記(在代字號之上:〜))在註釋中格式化代碼。 – aliteralmind

0

從你的代碼的第一步(雖然不是最好的之一)可能是:

public class NewGame{ 
    public static void main(String[] args) { 
     UserLog.main(); 
    } 
如果你改變 UserLog.main()的簽名

更好方法有UserLog的方法,例如

public class UserLog extends JFrame { 
    public void newMethod(String[] args) throws InterruptedException { 
     // your code in old main method 
    } 
} 

,並使用此方法從另一個類,如下所示:

public class NewGame { 
    public static void main(String[] args) { 
     UserLog userLog = new UserLog; 
     userLog.newMethod(args); 
    } 
} 

如果不傳遞任何參數來newMethod可以去除PARAMS String[] args在方法定義和調用userLog.newMethod()

0

在登錄類中使用公共方法,該方法返回用戶是否已登錄。在調用它的類中使用userLog log = new userLog(),然後重複調用該方法。如果它返回true,那麼用戶已成功登錄。

相關問題