在java中,传递文本文件中的值以创建基本登录系统的其他方法是什么?

在java中,传递文本文件中的值以创建基本登录系统的其他方法是什么?,java,swing,io,awt,project,Java,Swing,Io,Awt,Project,我需要使用java.AWT和/或javax.SWING中的文本文件创建一个基本的登录系统 在控制台应用程序中使用BufferedReader可以让它工作,但是当我在GUI(Swing)中使用相同的过程时,它完全是一团糟。我的JTextFields值与文本文件中的值不匹配 除了使用io类之外,是否有其他方法可以使用文本文件创建登录系统。这是我的密码 public class VotingSystemLogin extends JFrame implements ActionListener {

我需要使用
java.AWT
和/或
javax.SWING
中的文本文件创建一个基本的登录系统

在控制台应用程序中使用
BufferedReader
可以让它工作,但是当我在GUI(
Swing
)中使用相同的过程时,它完全是一团糟。我的
JTextField
s值与文本文件中的值不匹配

除了使用io类之外,是否有其他方法可以使用文本文件创建登录系统。这是我的密码

public class VotingSystemLogin extends JFrame implements ActionListener {
    //initialize io constructors
    static int checkAcc = 0;
    static int checkAcc2 = 0;
    static int checkTotal;
    //initialize and construct components
    JButton loginBtn = new JButton ("LOGIN");
    JButton regBtn = new JButton ("REGISTER");
    JLabel idLbl = new JLabel ("StudentID:");
    JTextField studIdTxt = new JTextField (5);;
    JLabel nameLbl = new JLabel ("StudentName:");
    JTextField studNameTxt = new JTextField (5);
    JLabel nameLayoutLbl = new JLabel ("(e.g. Juan dela Cruz)");
    JLabel idLayoutLbl = new JLabel ("(e.g. 063-2013-0001)");
    JLabel bgLbl = new JLabel ("bg");
    JPanel panel = new JPanel();
    Container con = getContentPane();

    String sID = studIdTxt.getText();
    String sName = studNameTxt.getText();

    public VotingSystemLogin() {
        super("Login");
        //adjust size and set layout and location
        setSize(380, 230);
        setLocation(530,290);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setLayout (null);
        panel.setBackground(Color.yellow);
        panel.setBorder(BorderFactory.createMatteBorder(10,10,10,10,Color.blue));

        //add components

        panel.add(loginBtn);
        panel.add(regBtn);
        panel.add(idLbl);
        panel.add(studIdTxt);
        panel.add(nameLbl);
        panel.add(studNameTxt);
        panel.add(nameLayoutLbl);
        panel.add(idLayoutLbl);
        //panel.add(bgLbl);
        try{
            BufferedImage loginLogo = ImageIO.read(new File("loginBg.jpg"));
            bgLbl = new JLabel(new ImageIcon(loginLogo));
            panel.add(bgLbl);
            bgLbl.setBounds (15, 10, 340, 180);
        }
        catch(IOException a)
        {
            System.out.println(a);
        }
        con.add(panel);

        //set component bounds (only needed by Absolute Positioning)
        loginBtn.setBounds (70, 140, 115, 40);
        regBtn.setBounds (210, 140, 115, 40);
        idLbl.setBounds (70, 35, 60, 35);
        studIdTxt.setBounds (155, 40, 170, 20);
        nameLbl.setBounds (70, 85, 80, 25);
        studNameTxt.setBounds (155, 85, 170, 20);
        nameLayoutLbl.setBounds (155, 105, 170, 15);
        idLayoutLbl.setBounds (155, 60, 120, 15);

        //add actionlisterner to buttons
        loginBtn.addActionListener(this);

    }


    public static void main (String[] args) {
        VotingSystemLogin vsl = new VotingSystemLogin();

    }

    public void actionPerformed (ActionEvent e){

        if (e.getSource() == loginBtn)
        {
            checkVoters();
            checkVoted();

            checkTotal = checkAcc + checkAcc2;

            System.out.println(checkAcc);
            System.out.println(checkAcc2);
            System.out.println(checkTotal);

            if (checkTotal == 2)
                JOptionPane.showMessageDialog(null,"You have already voted!","Fail",JOptionPane.ERROR_MESSAGE);
            else if (checkTotal == 1)
                JOptionPane.showMessageDialog(null,"You may vote now!","Success",JOptionPane.PLAIN_MESSAGE);
            else
                JOptionPane.showMessageDialog(null,"StudentID or StudentName is incorrect or doesn't exist","Fail",JOptionPane.ERROR_MESSAGE);
        }
    }

    public void checkVoters(){

        BufferedReader br = null;

        try{
            br = new BufferedReader(new FileReader("voters.txt"));

            String id1 = br.readLine();
            String name1 = br.readLine();

            while (id1 != null && name1 != null){
                System.out.println(id1);
                System.out.println(name1);
                id1 = br.readLine();
                name1 = br.readLine();
                if (sID.equals(id1) && sName.equals(name1)){
                    checkAcc = 1;
                }
            }

            System.out.println();

            }
            catch (FileNotFoundException a){
                a.printStackTrace();
            }
            catch (IOException a){
                a.printStackTrace();
            }
            finally{
                if (br != null){
                    try{
                        br.close();
                    }
                    catch(IOException a){
                        a.printStackTrace();
                    }
                }
            }   
    }

    public void checkVoted(){
        BufferedReader br2 = null;

        try{
            br2 = new BufferedReader(new FileReader("voted.txt"));

            String id2 = br2.readLine();
            String name2 = br2.readLine();

            while (id2 != null && name2 != null){
                System.out.println(id2);
                System.out.println(name2);
                if (sID.equals(id2) && sName.equals(name2)){
                    checkAcc2 = 1;
                }
                id2 = br2.readLine();
                name2 = br2.readLine();

            }

            System.out.println();

            }
            catch (FileNotFoundException a){
                a.printStackTrace();
            }
            catch (IOException a){
                a.printStackTrace();
            }
            finally{
                if (br2 != null){
                    try{
                        br2.close();
                    }
                    catch(IOException a){
                        a.printStackTrace();
                    }
                }
            }
    }
}

我不太清楚你说的“使用记事本”是什么意思。你的意思是你需要一种更好的方法来检查选民,而不是读取一个名为“vorters.txt”的文件?如果是这种情况,那么,是的,您可以使用投票者id查询数据库并检查它们的状态

您实际上从未将有效值分配给
sID
sName
…基本上,您只需将创建时设置为
JTextField
的默认值分配给它们

String sID = studIdTxt.getText();
String sName = studNameTxt.getText();
这意味着,当您将值与文件中的值进行检查时,实际上并不是在比较字段中当前的值

public void actionPerformed (ActionEvent e){

    if (e.getSource() == loginBtn)
    {
        sID = studIdTxt.getText();
        sName = studNameTxt.getText();
记住,GUI是事件驱动的环境,发生了一些事情,您会对此做出响应

您应该做的是,当调用
actionPerformed
时,您应该从字段中获取文本

public void actionPerformed (ActionEvent e){

    if (e.getSource() == loginBtn)
    {
        sID = studIdTxt.getText();
        sName = studNameTxt.getText();
如果您的检查方法实际上要求您传递要检查的值,而不是依赖实例值,则更好


是的,有比处理文件更好的解决方案,像(例如)一个独立的数据库会是一个更好的主意。

我的意思是,如果有其他方法可以用来扫描记事本?因为我们还没有被要求使用数据库。记事本将是我们的临时数据库。每行都有一个key=value格式。注释以#开头。您可以轻松地从磁盘加载()文件,然后使用get(key)检索值。如果未找到,则返回Null。我不确定你的投票者.txt文件的格式是什么-我没有仔细查看。我现在的问题是,每当我登录时,它总是落在我的if语句中,即使它应该落在我的else if语句中。是否有其他方法从记事本读取和传递值?谢谢你的帮助。还是我应该这样问?是否有其他方法读取和检查每行文本文件?因为每当我打印变量“id”的值时,它都会打印文件中的全部文本。我将取决于文件的格式,您现在也可以使用XMLNow,因为我已经回答了自己的问题。顺便说一下,非常感谢您的帮助:-)