通过将登录代码与JavaGUI链接来创建登录
我一直在尝试将我的登录代码与我创建的java gui链接,但是在运行它时遇到了问题。 1) 代码不读取我创建的文本文件 2) 当我按下gui的登录时,它什么也不做。我希望它检查输入的用户名和密码与文本文件中的用户名和密码 请帮助我,我已经检查了其他解决方案,但我找不到任何与我的问题相关的解决方案。 我认为我没有把代码链接好,所以如果有人能帮上忙,那就太好了 头等舱: 导入java.awt.EventQueue通过将登录代码与JavaGUI链接来创建登录,java,user-interface,login,Java,User Interface,Login,我一直在尝试将我的登录代码与我创建的java gui链接,但是在运行它时遇到了问题。 1) 代码不读取我创建的文本文件 2) 当我按下gui的登录时,它什么也不做。我希望它检查输入的用户名和密码与文本文件中的用户名和密码 请帮助我,我已经检查了其他解决方案,但我找不到任何与我的问题相关的解决方案。 我认为我没有把代码链接好,所以如果有人能帮上忙,那就太好了 头等舱: 导入java.awt.EventQueue import javax.swing.JFrame; import j
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();
}
}
}
好的,有太多的错误需要记住并列出,但这里有几个: 其中一个错误是在类已经扩展了JFrame的情况下创建了JFrame,实际上使其成为JFrame 您也只在
actionlogin()
方法中添加了登录按钮的操作侦听器,但从未调用过该方法
有多个扫描仪指向同一个文件
您尝试从控制台和文本字段读取输入,然后将它们分配给同一个变量,两次
您创建了第二个类,该类具有与“main”类相同的功能,并且都有main方法
我留下了一些你的代码,这样你可以看到一些错误和额外的花絮,你放进去,这是不需要的。我也没有修复您的框架,因为在构建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");
}
}
}