如何使用复选框在 java swing 中实现记住我、密码功能

How to implement remember me, password feature in java swing using checkbox

我有两个相框。一个用于 login_page(用户输入电子邮件和密码)。这里使用 google API 完成身份验证。在另一个框架中(有几个组合框和单选按钮可供选择),但对于一个单选按钮,我希望 JOptionPane 弹出一个密码并将我的选项记住为一个复选框,只有当密码正确时我才能允许他提交。

如何使用“记住我”复选框来存储密码?

如果您使用数据库而不是在数据库中保留复选框选项。否则,如果选中复选框,则将您的密码存储在用户路径下的某个隐藏文件中。下次登录前请查看该密码。

编辑: 基于文件的 rememberme 密码或用户名的示例代码

    String path=System.getProperty("user.home")+"/.myapp";
    FileWriter file=new FileWriter(path);
    file.write("user_password");
    file.close();
  • 第 1 章:

什么是数据库:A large collection of associated data

对于大型应用程序,这种方式根本不存在,您必须使用像 sqloracle 这样的数据库。 .etc 一些开源软件,比如 mysql,miniBase..etc

  • 第 2 章:

我为你写了一个简单的应用程序 needs.It 做你描述的想法并将名称和密码保存到文件[在用户桌面上]。

Class:

 import java.awt.FlowLayout;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.Scanner;

 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JPasswordField;
 import javax.swing.JRadioButton;
 import javax.swing.JTextField;

public class Applic {

//This will be the file where the username and password will be saved
File file = new File(System.getProperty("user.home")+"/Desktop/save.txt");


//Window,buttons etc...
JFrame frame= new JFrame();
JTextField name = new JTextField(20);
JPasswordField password = new JPasswordField(20);
JRadioButton remember = new JRadioButton("Remember me");
JButton Enter = new JButton("Enter");

   public Applic(){


     //Create the Window
       frame.setSize(250,200);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setResizable(false);
       frame.setLayout(new FlowLayout());

       //Add The items to window
       frame.add(name);
       frame.add(password);
       frame.add(remember);
       frame.add(Enter);


       UPDATE(); //Check if password and this User is Saved (very simple for one user)
       frame.setVisible(true);


       //Add A mouseAdapter(or whatever you want
       Enter.addMouseListener(new MouseAdapter(){
           public void mouseReleased(MouseEvent m){

               if(remember.isSelected()){
                  SAVE(); //Save This UserName and his PassWord     
               }

           }//end of mouseReleased
       });
   }


public static void main(String[] args){
    new Applic();
}


  public void SAVE(){      //Save the UserName and Password (for one user)



        try {
            if(!file.exists()) file.createNewFile();  //if the file !exist create a new one

            BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsolutePath()));
            bw.write(name.getText()); //write the name
            bw.newLine(); //leave a new Line
            bw.write(password.getPassword()); //write the password
            bw.close(); //close the BufferdWriter

        } catch (IOException e) { e.printStackTrace(); }        

 }//End Of Save




  public void UPDATE(){ //UPDATE ON OPENING THE APPLICATION

        try {
          if(file.exists()){    //if this file exists

            Scanner scan = new Scanner(file);   //Use Scanner to read the File

            name.setText(scan.nextLine());  //append the text to name field
            password.setText(scan.nextLine()); //append the text to password field
            scan.close();
          }

        } catch (FileNotFoundException e) {         
            e.printStackTrace();
        }                

   }//End OF UPDATE



}//End Of Class [Applic]