2014-09-20 111 views
0

我一直在嘗試將我的登錄代碼與創建的java gui鏈接,但是我一直在運行它時遇到問題。 1)代碼沒有讀取我創建的文本文件 2)當我按我的gui登錄時,它什麼都不做。我希望它用文本文件中的用戶名和密碼來檢查輸入的用戶名和密碼。通過鏈接登錄代碼與java創建登錄Gui

請幫助我,我檢查了其他解決方案,但我找不到與我的問題相關的任何內容。 我不認爲我把代碼連接起來,所以如果任何人都可以幫忙的話,那將會很棒。

頭等艙: import java.awt.EventQueue;

import javax.swing.JFrame; 
    import javax.swing.JOptionPane; 
    import javax.swing.JTextField; 
    import javax.swing.JPasswordField; 
    import javax.swing.JLabel; 
    import javax.swing.JButton; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.Scanner; 


    public class Login extends JFrame{ 


public JFrame frame; 
public JPasswordField passwordField; 
public JTextField textField; 
public JButton blogin; 
public JButton btnNewUser; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Login window = new Login(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public Login() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
public void initialize() { 
    frame = new JFrame(); 
    frame.getContentPane().setLayout(null); 

    passwordField = new JPasswordField(); 
    passwordField.setBounds(90, 114, 105, 22); 
    frame.getContentPane().add(passwordField); 

    textField = new JTextField(); 
    textField.setBounds(90, 79, 105, 22); 
    frame.getContentPane().add(textField); 
    textField.setColumns(10); 

    JLabel lblUsername = new JLabel("Username"); 
    lblUsername.setBounds(220, 82, 76, 16); 
    frame.getContentPane().add(lblUsername); 

    JLabel lblPassword = new JLabel("Password"); 
    lblPassword.setBounds(220, 117, 66, 16); 
    frame.getContentPane().add(lblPassword); 

    JButton blogin = new JButton("Login"); 
    blogin.setBounds(144, 158, 97, 25); 
    frame.getContentPane().add(blogin); 

    JButton btnNewUser = new JButton("New User ?"); 
    btnNewUser.setBounds(144, 196, 97, 25); 
    frame.getContentPane().add(btnNewUser); 


    frame.add(blogin); 
    frame.add(passwordField); 
    frame.add(textField); 
} 
Logincode lc = new Logincode(); 
public void actionlogin(){ 
    blogin.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae){ 

     Scanner sc; 
     try { 
      sc = new Scanner(new File("Logincode.txt")); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Scanner scan = new Scanner(new File("Logincode.txt")); 
     Scanner keyboard = new Scanner (System.in); 

     String inpUser = keyboard.nextLine(); 
     inpUser = textField.getText(); 
     String inpPass = keyboard.nextLine(); 
     inpPass = passwordField.getText();// gets input from user 

     String user = scan.nextLine(); 
     String pass = scan.nextLine(); // looks at selected file in scan 

     if (inpUser.equals(user)&& inpPass.equals(pass)){ 
      System.out.print("your login message"); 
     }else { 

    JOptionPane.showMessageDialog(null,"Wrong Password/Username"); 
     } 
     } 
     }); 
     } 
     } 

這是第二類:

import java.util.Scanner; // I use scanner because it's easier for me. 
    import java.io.File; 
    import java.io.FileNotFoundException; 

    public class Logincode{ 
    public static void run() throws FileNotFoundException { 
Scanner scan = new Scanner (new File("Logincode.txt")); 
Scanner keyboard = new Scanner (System.in); 

String inpUser = keyboard.nextLine(); 
String inpPass = keyboard.nextLine(); // gets input from user 

String user = scan.nextLine(); 
String pass = scan.nextLine(); // looks at selected file in scan 

if (inpUser.equals(user)&& inpPass.equals(pass)){ 
    System.out.print("your login message"); 
} else { 
    System.out.print("your error message"); 
} 

    } 

public static void main(String[] args){ 

    try { 
     run(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


} 







    } 

回答

0

好了,有太多的錯誤,記住,並列出他們所有,但這裏有幾個:

一個錯誤的是創造一個JFrame,當你的類已經擴展了JFrame時,本質上使它成爲一個JFrame。

您也只是在actionlogin()方法中添加了您的登錄按鈕的動作偵聽器,該方法從未被調用過。

有多個掃描儀指向同一個文件。

您試圖從控制檯和文本字段中讀取輸入,然後將它們分配給相同的變量兩次。

您創建了第二個類,它具有與「主」類相同的功能,並且都具有主要方法。

我留下了一段代碼,以便您可以看到一些您輸入的錯誤和額外消息,這些消息不是必需的。我也沒有修復你的框架,因爲有許多寶貴的經驗教訓只能在構建GUI時通過反覆試驗來學習。

代碼:

import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 


public class Login extends JFrame{ 

    //public JFrame frame; //Extends JFrame already so 'this' IS a frame 
    public JPasswordField passwordField; 
    public JTextField textField; 
    public JButton blogin; 
    public JButton btnNewUser; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        Login window = new Login(); 
        window.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Login() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    public void initialize() { 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //DO NOT forget this, the instance will continue to run if not. 
    setLayout(null); 

    setSize(350,300); // only added so I didn't have to expand window as often 

    passwordField = new JPasswordField(); 
    passwordField.setBounds(90, 114, 105, 22); 
    add(passwordField); 

    textField = new JTextField(); 
    textField.setBounds(90, 79, 105, 22); 
    add(textField); 
    textField.setColumns(10); 

    JLabel lblUsername = new JLabel("Username"); 
    lblUsername.setBounds(220, 82, 76, 16); 
    add(lblUsername); 

    JLabel lblPassword = new JLabel("Password"); 
    lblPassword.setBounds(220, 117, 66, 16); 
    add(lblPassword); 

    JButton blogin = new JButton("Login"); 
    blogin.setBounds(144, 158, 97, 25); 
    blogin.addActionListener(new ActionListener() { //moved from actionlogin() 
     public void actionPerformed(ActionEvent ae){ 
      actionlogin(); 
     } 
    }); 

    add(blogin); 

    JButton btnNewUser = new JButton("New User ?"); 
    btnNewUser.setBounds(144, 196, 97, 25); 
    add(btnNewUser);   

    add(blogin); 
    add(passwordField); 
    add(textField); 
    } 

    //Logincode lc = new Logincode(); Don't know why the second class was created or needed here. 

    public void actionlogin(){ 

     //Scanner sc; not used 
     Scanner scan=null; 

     try { 
      //sc = new Scanner(new File("Logincode.txt")); not used 
      scan = new Scanner(new File("Change to the path where your file is located ofcourse")); //make sure to add your path 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     //Scanner keyboard = new Scanner (System.in);  

     //String inpUser = keyboard.nextLine(); 
     String inpUser; 
     inpUser = textField.getText(); 

     //String inpPass = keyboard.nextLine(); 
     String inpPass; 
     inpPass = passwordField.getText();// gets input from user 

     String user=""; 
     if(scan.hasNextLine()) //added to check if there is another line to read 
      user = scan.nextLine();    

     String pass=""; 
     if(scan.hasNextLine()) 
      pass = scan.nextLine(); // looks at selected file in scan 

      if (inpUser.equals(user)&& inpPass.equals(pass)){ 
       System.out.print("your login message"); 
      }else { 
       JOptionPane.showMessageDialog(null,"Wrong Password/Username"); 
      } 
    }  

} 

希望這有助於。