从GUI读取文本文件

达斯汀

我有一个GUI可以验证用户名和密码。分配的另一部分是从包含用户名和密码的文件中读取内容,并检查其是否与用户在文本字段中输入的内容匹配。如果匹配,则它将隐藏“登录”页面,并且将出现另一个带有“欢迎”消息的页面。我对文本文件零经验,我应该将该代码块放在哪里?我相信它会走在ActionListener的方法,而不是主要方法,但我只是失去了。我只需要向正确的方向稍加推动即可。这是我到目前为止所拥有的。任何帮助将不胜感激。谢谢!

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import javax.swing.*;
import java.io.*;
/**
*/
public class PassWordFrame extends JFrame
{
   private static final int FIELD_WIDTH = 10;
   private static final int FRAME_WIDTH = 500;
   private static final int FRAME_HEIGHT = 300;
   private JLabel fileRead;
   private JLabel instruct;
   private JLabel username;
   private JLabel password;
   private JTextField usertext;
   private JTextField passtext;
   private JButton login;
   private ActionListener listener;
   //String text = "";

   public PassWordFrame()
   {
      createComponents();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
      listener = new ClickListener();
   }
   class ClickListener implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
          String inputFileName = ("users.txt");
          File userFile = new File(inputFileName);

      }
   }
   public void createComponents()
   {
      Color blue = new Color(0,128,155);
      Font font = new Font("Times New Roman", Font.BOLD, 14);

      instruct = new JLabel("Please enter your username and password.");
      instruct.setFont(font);

      username = new JLabel("Username: ");
      username.setFont(font);

      password = new JLabel("Password: ");
      password.setFont(font);

      usertext = new JTextField(FIELD_WIDTH);
      passtext = new JTextField(FIELD_WIDTH);

      login = new JButton("Login");
      login.setFont(font);

      instruct.setForeground(Color.BLACK);
      login.setForeground(Color.BLACK);
      username.setForeground(Color.BLACK);
      password.setForeground(Color.BLACK);

      login.addActionListener(listener);

      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      JPanel panel3 = new JPanel();
      JPanel panel4 = new JPanel();

      panel1.setBackground(blue);
      panel2.setBackground(blue);
      panel3.setBackground(blue);
      panel4.setBackground(blue);

      panel1.add(instruct);
      panel2.add(username);
      panel2.add(usertext);
      panel3.add(password);
      panel3.add(passtext);
      panel4.add(login);

      add(panel1, BorderLayout.NORTH);
      add(panel2, BorderLayout.WEST);
      add(panel3, BorderLayout.CENTER);
      add(panel4, BorderLayout.SOUTH);

      pack();
   }
}      

import javax.swing.*;
import java.awt.*;

/**

*/
public class PassWordFrameViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new PassWordFrame();
      frame.setTitle("Password Verification");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);

   }
}      
Madhawa Priyashantha

假设g驱动程序中有一个名为password.txt的文本文件,并且包含用@符号分隔的用户名和密码。

喜欢以下

password@123

示例代码

package homework;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class PassWordFrame extends JFrame
{
   private static final int FIELD_WIDTH = 10;
   private static final int FRAME_WIDTH = 500;
   private static final int FRAME_HEIGHT = 300;
   private JLabel fileRead;
   private JLabel instruct;
   private JLabel username;
   private JLabel password;
   private JTextField usertext;
   private JTextField passtext;
   private JButton login;
   private ActionListener listener;
   //String text = "";

   public PassWordFrame()
   {
      createComponents();
      setSize(FRAME_WIDTH, FRAME_HEIGHT);
      login.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                String info = ReadFile();
                System.out.println(info);
                String[] split = info.split("@");
                String uname=split[0];
                String pass =split[1];
                if(usertext.getText().equals(uname) && passtext.getText().equals(pass)){
                    instruct.setText("access granted");
                }else{
                    instruct.setText("access denided");
                }
            }
        }); 

   }
   private static  String ReadFile(){
        String line=null;
        String text="";
        try{

            FileReader filereader=new FileReader(new File("G:\\password.txt"));
             //FileReader filereader=new FileReader(new File(path));
            BufferedReader bf=new BufferedReader(filereader);
            while((line=bf.readLine()) !=null){
                text=text+line;

            }
            bf.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return text;

    }


   public void createComponents()
   {
      Color blue = new Color(0,128,155);
      Font font = new Font("Times New Roman", Font.BOLD, 14);

      instruct = new JLabel("Please enter your username and password.");
      instruct.setFont(font);

      username = new JLabel("Username: ");
      username.setFont(font);

      password = new JLabel("Password: ");
      password.setFont(font);

      usertext = new JTextField(FIELD_WIDTH);
      passtext = new JTextField(FIELD_WIDTH);

      login = new JButton("Login");
      login.setFont(font);

      instruct.setForeground(Color.BLACK);
      login.setForeground(Color.BLACK);
      username.setForeground(Color.BLACK);
      password.setForeground(Color.BLACK);

      login.addActionListener(listener);

      JPanel panel1 = new JPanel();
      JPanel panel2 = new JPanel();
      JPanel panel3 = new JPanel();
      JPanel panel4 = new JPanel();

      panel1.setBackground(blue);
      panel2.setBackground(blue);
      panel3.setBackground(blue);
      panel4.setBackground(blue);

      panel1.add(instruct);
      panel2.add(username);
      panel2.add(usertext);
      panel3.add(password);
      panel3.add(passtext);
      panel4.add(login);

      add(panel1, BorderLayout.NORTH);
      add(panel2, BorderLayout.WEST);
      add(panel3, BorderLayout.CENTER);
      add(panel4, BorderLayout.SOUTH);

      pack();
   }
}      

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章