java swing编程记住密码

运行登录界面,勾选记住密码框,下次运行界面如何自动勾选,注:不是默认勾选 请大神解答

try {
            Properties pro=new Properties();
            InputStream is=this.getClass().getResourceAsStream("/Dl.properties");
            pro.load(is);
            String suser =  pro.get("user").toString();
            String spsswd = pro.get("password").toString();
            if(!suser.isEmpty() && !spsswd.isEmpty()){
                comboBox.setSelectedIndex(listuser.indexOf(suser));
                passwordField.setText(spsswd);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        final JButton btnNewButton = new JButton("");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int nub = comboBox.getSelectedIndex();
                String user = loginuser[nub];
                String psswd = passwordField.getText();
                System.out.println("登录名为-"+user+",密码为-"+psswd);
                int flag = dlserversx.checkUser(user, psswd);
                if(flag==1){
                    if(chckbxNewCheckBox.isSelected()){
                        Properties props = new Properties();
                        try {
                            OutputStream fos=new FileOutputStream(this.getClass().getResource("/Dl.properties").getPath());
                            props.setProperty("user", user);
                             props.setProperty("password", psswd);
                             props.store(fos, "Update '" + user + "' value"); 
                             fos.close(); 
                        } catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }else{
                         Properties props = new Properties();
                         OutputStream fos;
                         try {
                            fos = new FileOutputStream(this.getClass().getResource("/Dl.properties").getPath());
                            props.setProperty("user", "");
                             props.setProperty("password", "");
                             props.store(fos, "Update '" + user + "' value"); 
                             fos.close();
                        } catch (FileNotFoundException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                    Dpsk frame = new Dpsk();
                    frame.setVisible(true);
                    Dl.this.hide();
                }else{
                    JOptionPane.showMessageDialog(null, "您输入的账号或密码错误!");
                }
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                btnNewButton.setIcon(new ImageIcon("../dldw/src/image/loginquedingC.png"));
            }
            @Override
            public void mouseExited(MouseEvent e) {
                btnNewButton.setIcon(new ImageIcon("../dldw/src/image/loginqueding.png"));
            }
        });
        btnNewButton.setIcon(new ImageIcon("../dldw/src/image/loginqueding.png"));
        btnNewButton.setBounds(115, 253, 180, 31);
        contentPane.add(btnNewButton);

        chckbxNewCheckBox = new JCheckBox("记住密码");
        chckbxNewCheckBox.setContentAreaFilled(false);
        chckbxNewCheckBox.setBounds(137, 212, 83, 23);
        contentPane.add(chckbxNewCheckBox);
        chckbxNewCheckBox.setOpaque(false);

1个回答

存储一个boolean值到文件中,打开界面的时候读取它,根据它来调用setState(true或者false)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐