繁体   English   中英

变量不会从JTextField中获取数据

[英]Variable won't take data from 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;
}

}

如果有人能弄清楚我在这种情况下做了什么愚蠢的事情,请告诉我。 如果代码太长且令人讨厌阅读,我深表歉意。

在您的代码中:

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)当文本字段具有焦点时按。


顺便说一句,您的代码会遭受过度使用静态修饰符的困扰,建议您尝试创建Swing GUI 之前先学习如何创建真正的OOP类和Java基础知识。 您不会后悔的。

在侦听器类actionPerformed方法中读取UserN和PassW值。 否则,您将在初始化时读取值为空字符串的值。

您定义的变量

public static String UserN = " ";
public static String PassW = " ";

不会从文本字段中获取最新值。

提议的解决方案(不是理想的,但是对于您的代码而言)

  1. 像在GuiComponents类中一样,将Jtext字段设为静态公共

     public static JTextField user; public static JTextField pass; 

在您的LoginListener

Login.CreateLogin(GUIComponents.user.getText(),GUIComponents.pass.getText());
System.out.println(GUIComponents.user.getText() + GUIComponents.pass.getText());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM