我一直在嘗試將我的登錄代碼與創建的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();
}
}
}