我对编码还很陌生,我正在努力通过首先检查索引文件是否存在(其中有密码)来获得便宜且简单的登录代码,如果存在,请阅读它所说的内容,然后向用户询问用户名和密码。如果该文件不存在或为空,它应该提示用户从那时起使用这两个字段并将它们写入索引文件。
但是,我遇到的问题是,我设置的用于运行代码以将这两个字段写入文件的按钮似乎没有在字符串中找到任何内容,因此无法写入任何内容。我包含了下面的代码,如果有什么我忘记了,可以添加更多代码。
下面是 GUI 窗口和 Action 监听器的代码。
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;
}
如果有人知道我在这种情况下做了什么蠢事,请告诉我。如果代码很长而且读起来很烦人,我深表歉意。
最佳答案
在您的代码中:
public static void LoginScreen(){
// ............ code deleted to simplify things
JTextField user = new JTextField(15);
JTextField pass = new JTextField(15);
// ..... code removed to simplify things
LoginFrame.pack();
LoginFrame.setVisible(true);
UserN = user.getText();
PassW = pass.getText();
}
您正在尝试在创建 GUI 时以及在用户有机会向其中输入任何内容之前从 JTextField 中提取文本。要解决此问题,请不要在创建 GUI 时提取文本,而是由于事件,例如在按下 JButton 时将激活的 JButton 的 ActionListener 中的事件,或将在 Enter 时激活的 JTextFields 的 ActionListener 中的事件当文本字段具有焦点时按下。
顺便说一句,您的代码严重过度使用了静态修饰符,这表明您需要学习如何创建真正的 OOP 类,以及 尝试创建 Swing GUI 之前的 Java 基础知识。你不会后悔这样做的。
关于java - 变量不会从 JTextField 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20597723/