JAVA操作註冊表

來源:互聯網
上載者:User

標籤: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() {@Overridepublic 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操作註冊表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.