随着小编对于Java的学习不断的深入,了解的东西也越来越多了。在小编学习的过程中,小编的老师叫小编就登录操作中的记住密码进行编写。小编在网上寻找了很久,找到的大部分是用JavaWeb中的cookie来编写。但小编还没有进行JavaWeb的学习,所有这些方法不实用。最后,小编根据老师的提示,知道了可以用Java的util包中的Preferences类来实现其功能。其实这个注册表让小编来理解就是一个文件化的Map,可以永久的存放键值对,存的数据也是以键值对的方式存的。对于注册表的讲解,小编就不细讲了。下面来看如何进行记住密码的操作:
package com.yc.ui;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.wb.swt.SWTResourceManager;
import com.yc.dao.UserDAO;
import com.yc.util.YcUtil;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
public class LoginUI {
protected Shell shell;
private Text text_user;
private Text text_pwd;
private Label label_tishi;
private Button btnCheck_jizhumima;
// 创建UserDAO对象,方便调用方法
UserDAO dao = new UserDAO();
//创建注册表对象
Preferences prefer = Preferences.userNodeForPackage(getClass());
//获取到注册表的节点名
String node = getClass().getName();
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
LoginUI window = new LoginUI();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setImage(SWTResourceManager.getImage(LoginUI.class, "/images/1.jpg"));
shell.setSize(772, 514);
shell.setText("用户登录界面");
YcUtil.centerWindow(shell);
Label label = new Label(shell, SWT.CENTER);
label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 20, SWT.NORMAL));
label.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
label.setBounds(278, 26, 175, 55);
label.setText("用户登录");
Label label_user = new Label(shell, SWT.NONE);
label_user.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 13, SWT.NORMAL));
label_user.setBounds(185, 131, 88, 28);
label_user.setText("用户名:");
text_user = new Text(shell, SWT.BORDER);
//在界面上自动显示密码
text_user.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
String uname = text_user.getText().trim();
String pwd = prefer.get(uname, node);
try {
Map<String,Object>map02 = dao.findByPwd(uname);//获取到数据库中用户对应的密码
String p = YcUtil.objToString(map02.get("PWD"));
//这个方法是将注册表中的密码和已存的数据库中的密码进行比对
if(pwd.equals(p)) {
text_pwd.setText(pwd);
btnCheck_jizhumima.setSelection(true);
}else {
btnCheck_jizhumima.setSelection(false);
text_pwd.setText("");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
text_user.setBounds(278, 121, 217, 38);
Label label_pwd = new Label(shell, SWT.NONE);
label_pwd.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 13, SWT.NORMAL));
label_pwd.setBounds(185, 220, 88, 28);
label_pwd.setText("密 码:");
text_pwd = new Text(shell, SWT.BORDER | SWT.PASSWORD);
text_pwd.setBounds(278, 209, 217, 38);
Button btn_denglu = new Button(shell, SWT.NONE);
btnCheck_jizhumima = new Button(shell, SWT.CHECK);
//记住密码
btnCheck_jizhumima.setBounds(263, 275, 121, 20);
btnCheck_jizhumima.setText("记住密码");
//由登录操作来实现记住密码的功能
btn_denglu.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 从界面上获取用户名和密码
String uname = text_user.getText().trim();
String pwd = text_pwd.getText().trim();
// 调用UserDAO里面的login方法
try {
//判断界面上的记住密码是否被选中
//选中
if(btnCheck_jizhumima.getSelection()) {
Map<String, Object> map = dao.login(uname, pwd);
if (null == map) {
label_tishi.setText("用户名或密码错误,请核对后重新输入!!");
return;
}
// 登录成功,将对应的密码和用户名放到注册表中
prefer.put(uname, pwd);
// 储存当前登录用户的信息
YcUtil.userLogin = map;
// 跳转到主界面
MainUI ui = new MainUI();
// 关闭当前页面
shell.dispose();
ui.open();
}else {
//没有选中
Map<String, Object> map = dao.login(uname, pwd);
if (null == map) {
label_tishi.setText("用户名或密码错误,请核对后重新输入!!");
return;
}
//根据注册表的键删除对应的值
prefer.remove(uname);
// 登录成功
// 储存当前登录用户的信息
YcUtil.userLogin = map;
// 跳转到主界面
MainUI ui = new MainUI();
// 关闭当前页面
shell.dispose();
ui.open();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btn_denglu.setBounds(217, 317, 98, 30);
btn_denglu.setText("登录");
Button btn_zhuce = new Button(shell, SWT.NONE);
// 用户注册
btn_zhuce.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 弹出注册对话框//设置对话框模式为窗口未关闭不能进行其他操作
Dialog_Register reg = new Dialog_Register(shell, SWT.CLOSE | SWT.APPLICATION_MODAL);
// 调用open
reg.open();
}
});
btn_zhuce.setBounds(457, 317, 98, 30);
btn_zhuce.setText("注册");
label_tishi = new Label(shell, SWT.NONE);
label_tishi.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
label_tishi.setBounds(253, 378, 302, 17);
}
}
以上是小编模仿写的一个登录的界面,其中有记住密码的功能,小编已经写出了相应的注释,希望对于那些刚刚学完Java想要自己动手做项目的小可爱有帮助,谢谢!!!