[英]Is there a way to implement classes from a file into a new file?
我有一個非常基本的登錄程序:
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();
}
當然,這在語法上可能並不正確,但這就是要點。
謝謝,如果我需要改寫或修改問題/格式,請告訴我! :)
編輯將當前main方法設置為常規公共類,並由新main從新公共類調用
public class gameLogin
{
public static void main(String[]args)
{
userLogin();
}
public class userLogin()
{
// current code, evidently seen in the current main
}
// rest of code
因此,為了引用原始文件userLog,我必須(在新文件中:gameLogin)使用userLog();。
還是更好用
userLog.userLogin("Munkeeface", "password");
最簡單的方法是將所有代碼從您的main
移至靜態實用程序類函數,然后從其他類main
調用該函數。 例如:
public class LoginToWebsiteUtil {
public static final void login(String username, String password, ...) {
//CODE GOES HERE
}
}
並用於:
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, ...) {
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
}
}
但是,正如您所看到的,您仍然擁有“什么保持狀態?” 問題。
(這是一個好問題。聽起來這個登錄代碼將來可能對您有用。)
從您的代碼開始,第一步(盡管不是最好的一步)可能是:
public class NewGame{
public static void main(String[] args) {
UserLog.main();
}
如果將UserLog.main()
方法的簽名更改為具有UserLog.main()
的non-static
方法,則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傳遞任何參數,則可以在方法定義和調用userLog.newMethod()
刪除params String[] args
。
在登錄類中使用公共方法,該方法返回用戶是否已登錄。 在調用它的類中,使用userLog log = new userLog(),然后重復調用該方法。 如果返回true,則表明用戶已成功登錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.