我有一个非常基本的登录程序:
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方法成为常规公共类,并从新公共类调用,通过新的主
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.userLogin("Munkeeface", "password");
答案 0 :(得分:1)
最简单的方法是简单地将所有代码从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
}
}
但是,正如你所看到的,你仍然拥有“什么状态?”问题。
(这是一个很好的问题。听起来这个登录代码将来可能对您有用。)
答案 1 :(得分:0)
从你的代码中,第一步(尽管不是最好的一步)可能是:
public class NewGame{
public static void main(String[] args) {
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传递任何参数,则可以删除方法定义和调用String[] args
userLog.newMethod()
答案 2 :(得分:0)
在登录类中使用公共方法,该方法返回用户是否已登录。在调用它的类中使用userLog log = new userLog(),然后重复调用该方法。如果返回true,则表示用户已成功登录。