我有一個程序,它是一個非常基本的登錄:有沒有辦法將文件中的類實現爲新文件?
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");
使當前的主要方法的正規公立下課後,和新公共類調用,通過新的主 'public static void main(String [] args) { userLogin(); } 公共類USERLOGIN(){ // 當前類 }「 OK,所以爲了引用原始文件,用戶日誌,我會(在新文件:gameLogin)使用用戶日誌() ;或者它會更好地使用userLog.userLogin(「Munkeeface」,「密碼」);? – Munkeeface
對不起,我不知道如何使插入的代碼在以前的評論中脫穎而出,但我希望它是有道理的 – Munkeeface
將此代碼和您的問題追加到原始帖子的底部。您可以正確地闡述和格式化代碼。但是如果我理解你,你可以調用'UserLog login = new UserLog(username,password);'。而且,類名實際上應該是大寫的。 'UserLog',而不是'userLog'。 (使用重音標記(在代字號之上:〜))在註釋中格式化代碼。 – aliteralmind