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 mapUser=new HashMap();
Map mapPwd=new HashMap();
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();
}
}