我對編碼相當陌生,正在努力通過首先檢查索引文件是否存在(其中包含密碼)來獲取便宜且易於使用的登錄代碼,以及如果是這樣,請閱讀它說的內容並要求用戶輸入用戶名和密碼。如果文件不存在或爲空,它應該提示用戶從此開始使用兩個字段並將它們寫入索引文件。變量不會從JTextField獲取數據
然而,我遇到的問題是,我設置的運行代碼將這兩個字段寫入文件的按鈕似乎沒有在字符串中找到任何內容,因此無法寫入任何內容。我將下面的代碼包括在內,如果有什麼我忘記的話,可以添加更多的代碼。
以下是GUI窗口和動作偵聽器的代碼。
public class GUIComponents extends JFrame{
public static String UserN = " ";
public static String PassW = " ";
public static void LoginScreen(){
JFrame LoginFrame = new JFrame("Log in Menu");
JPanel LoginInfo = new JPanel();
JPanel LoginAttempt = new JPanel();
JButton LoginOkay = new JButton("Log in");
LoginListenerClass loginListen = new LoginListenerClass();
LoginOkay.addActionListener(loginListen);
JButton LoginCancel = new JButton("Cancel");
CancelListenerClass cancelListen = new CancelListenerClass();
LoginCancel.addActionListener(cancelListen);
JLabel usern = new JLabel("Username:");
JLabel passw = new JLabel("Password:");
JTextField user = new JTextField(15);
JTextField pass = new JTextField(15);
LayoutManager LoginLayout = new GridLayout(2,2,5,10);
LayoutManager OkayCancel = new FlowLayout();
LoginInfo.setLayout(LoginLayout);
LoginAttempt.setLayout(OkayCancel);
LoginInfo.add(usern);
LoginInfo.add(user);
LoginInfo.add(passw);
LoginInfo.add(pass);
LoginAttempt.add(LoginOkay);
LoginAttempt.add(LoginCancel);
if(Login.firstTimeCheck()==true){
JOptionPane.showMessageDialog(null, "This is your first time using this program. \nPlease enter the Username"
+ " and Password\n you wish to set for all users.");
}
LoginFrame.add(LoginInfo, BorderLayout.CENTER);
LoginFrame.add(LoginAttempt, BorderLayout.SOUTH);
LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LoginFrame.setLocationRelativeTo(null);
LoginFrame.pack();
LoginFrame.setVisible(true);
UserN = user.getText();
PassW = pass.getText();
}
}
class LoginListenerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(Login.firstTimeCheck()==false){
Login.passCheck();
}
else{
try {
Login.CreateLogin(GUIComponents.UserN, GUIComponents.PassW);
System.out.println(GUIComponents.UserN+" "+ GUIComponents.PassW);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
class CancelListenerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
下面這是對數函數的代碼:
public class Login {
private static String UserName; //true username
private static String Password; //true password
private static boolean accepted = false;
private static String user="";
private static String pass="";
private static boolean firstTime = true;
Login(){
}
public static void CreateLogin(String user, String pass) throws IOException{
UserName = user;
Password = pass;
try{
FileWriter FW = new FileWriter("Index.txt");
PrintWriter output = new PrintWriter(FW);
output.println(UserName);
output.println(Password);
output.close();
FW.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public static void LogIn(){
try{
FileReader FR = new FileReader("Index.txt");
firstTime = false;
Scanner sc = new Scanner(FR);
user = sc.nextLine();
pass = sc.nextLine();
if(user.equalsIgnoreCase("")){
firstTime = true;
}
if(pass.equals("")){
firstTime = true;
}
}
catch(FileNotFoundException e){
firstTime = true;
}
GUIComponents.LoginScreen();
}
public static void passCheck(){
if(UserName.equalsIgnoreCase(user)){
if(Password.equals(pass)){
accepted = true;
}
}
else{
JOptionPane.showMessageDialog(null,"Your Username and Password do not /n match our files."
+ "Please try again.");
}
}
public static boolean firstTimeCheck(){
return firstTime;
}
}
如果任何人都可以找出我做了什麼愚蠢的事這種情況下,請讓我知道。我很抱歉,如果代碼是漫長的和煩人的閱讀。
請縮短代碼,向我們顯示錯誤,並僅使用相關數據。 – iWumbo