码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA操作注册表

时间:2015-04-01 15:30:40      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:swt   java   

public class Register {
//将一个Map信息记录到注册表中
public void recordRegistration(Map<String,String>entry) throws BackingStoreException{
 // 如果选的是systemNode...则保存在[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs]
        // Preferences pre = Preferences.systemNodeForPackage(RegistrationOp.class);
        // 如果选的是userNode.... 则保存在[HKEY_CURRENT_USER\Software\JavaSoft\Prefs]
        Preferences pre = Preferences.userNodeForPackage(Register.class);
        //也可以使用流的形式将一个xml导入进来
        //Preferences.importPreferences(is);
if(entry!=null){
Set<String> keys=entry.keySet();
Iterator<String> its=keys.iterator();
while(its.hasNext()){
String key=its.next();
String value=entry.get(key);
pre.put(key,value);
}
}
pre.flush();
}
//查询是否有信息记录在注册表中
public String findInfo(String key){
Preferences now=Preferences.userNodeForPackage(Register.class);
String result=now.get(key, null);
return result;
}
//删除注册表中对应键的信息
public void delInfo(String key){
Preferences now =Preferences.userNodeForPackage(Register.class);
now.remove(key);
}
}



public class QQ {
Register rg=new Register();
public String user;
public String pwd;
protected Shell shell;
private Text text;
private Text text_1;

public static void main(String[] args) {
try {
QQ window = new QQ();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
Composite composite = new Composite(shell, SWT.NONE);
Label label = new Label(composite, SWT.NONE);
label.setBounds(29, 28, 61, 17);
label.setText("\u8D26\u53F7\uFF1A");
Label label_1 = new Label(composite, SWT.NONE);
label_1.setBounds(29, 92, 61, 17);
label_1.setText("\u5BC6\u7801\uFF1A");
text = new Text(composite, SWT.BORDER);
text.setBounds(97, 25, 170, 23);
text_1 = new Text(composite, SWT.BORDER | SWT.PASSWORD);
text_1.setBounds(97, 89, 170, 23);
final Button button = new Button(composite, SWT.CHECK);
button.setBounds(33, 163, 98, 17);
button.setText("\u8BB0\u4F4F\u5BC6\u7801");
Button button_1 = new Button(composite, SWT.NONE);
button_1.setBounds(187, 158, 80, 27);
button_1.setText("\u767B\u9646");
String loginname=rg.findInfo("user");
String loingpwd=rg.findInfo("pwd");
//判断注册表中是否存在登录名
if(loginname!=null&&!"".equals(loginname)){
text.setText(rg.findInfo("user"));
if(loingpwd!=null&&!"".equals(loingpwd)){ //如果用户存在,则读取该用户的密码
text_1.setText(rg.findInfo("pwd"));
}
}
button.setSelection(true);
    //登陆事件
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Map<String,String> mapUser=new HashMap<String,String>();
Map<String,String> mapPwd=new HashMap<String,String>();
String uname=text.getText().toString().trim();
String upwd=text_1.getText().toString().trim();
if(uname==null||"".equals(uname)){
showMessage("用户名不能为空...");
text.setFocus();
return;
}
if(upwd==null||"".equals(upwd)){
showMessage("密码不能为空...");
text_1.setFocus();
return;
}
mapUser.put("user",uname);
mapPwd.put("pwd",upwd);
String str=rg.findInfo("user");
if(button.getSelection()==true){ // 如果用户选择保存密码,则写入注册表
if(str==null||str.length()==0){//如果user键对应的值为空
try {
rg.recordRegistration(mapUser); // 写入注册表
rg.recordRegistration(mapPwd);
} catch (BackingStoreException e1) {
e1.printStackTrace();
}
shell.close();
Login l=new Login();
l.open();
}else{
rg.delInfo("user");
rg.delInfo("pwd");
try {
rg.recordRegistration(mapUser);
rg.recordRegistration(mapPwd);
} catch (BackingStoreException e2) {
e2.printStackTrace();
}
shell.close();
Login l=new Login();
l.open();
}
}
}
});
}
public void showMessage(String message){
MessageBox mb=new MessageBox(shell,SWT.NONE);
mb.setText("错误提示");
mb.setMessage(message);
mb.open();
}
}

JAVA操作注册表

标签:swt   java   

原文地址:http://blog.csdn.net/qfxsxhfy/article/details/44806025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!