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

时间:2015-03-08 09:54:28

标签: java swing checkbox passwords joptionpane

我有两个框架。一个是login_page(用户输入电子邮件和密码)。这里的身份验证是使用谷歌API完成的。在另一个框架中(选择了很少的组合框和单选按钮),但对于一个单选按钮,我想要一个JOptionPane弹出密码并记住我选项作为复选框,只有密码正确我才能允许他提交。

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

2 个答案:

答案 0 :(得分:0)

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

修改:基于文件的记忆密码或用户名的示例代码

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

答案 1 :(得分:0)

  • 第1章:

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

对于大型应用程序这种方式只是不存在,你必须使用像 sql oracle 等一些开源的数据库,如 mysql ,<强> miniBase ..等

  • 第2章:

我为您的需求编写了一个简单的应用程序。它会让您认为您已经描述并将名称和密码保存到文件[在用户桌面上]。

enter image description here

班级:

 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]