swt 做界面時部分要點

//讓窗口居中顯示
		shell.setLocation((display.getClientArea().width-shell.getSize().x)/2, 
				(display.getClientArea().height-shell.getSize().y)/2);
		
		
BackgroundMode  讓標籤中的字顯示在圖片背景上,但字下面只有圖片背景,沒有標籤中的陰影

Link link = new Link(composite, SWT.NONE);//註冊賬號,找回密碼用link
		link.setBounds(396, 261, 63, 17);
		link.setText("<a>註冊賬號</a>");
		
		Link link_1 = new Link(composite, 0);
		link_1.setText("<a>找回密碼</a>");
		link_1.setBounds(396, 308, 63, 27);

Button btnCheckButton = new Button(composite, SWT.CHECK);//記住密碼用
CheckButton

//面板移動
composite.addMouseMoveListener(new MouseMoveListener() {
	public void mouseMove(MouseEvent e) {//鼠標移動的時候
		if(isDown){//說明鼠標按下去的時候,所有當鼠標移動的時候,要跟着走
			 //獲取此時的光標位置
			shell.setLocation(shell.getLocation().x+e.x-x,shell.getLocation().y+e.y-y);
		}
	}
});											composite.addMouseListener(new MouseAdapter() {
			
			public void mouseDown(MouseEvent e) {//鼠標按下的時候
				
				isDown=true;
				x=e.x;
				y=e.y;
			}
			
			public void mouseUp(MouseEvent e) {//鼠標鬆開的時候
				isDown=false;
			}
		});    



//關閉按鈕
lblNewLabel.addMouseListener(new MouseAdapter() {
	public void mouseDown(MouseEvent e) {//鼠標按下
		lblNewLabel.setImage(SWTResourceManager.getImage(qqdenglu.class, "/images/btn_close_down.png"));
			}

	public void mouseUp(MouseEvent e) {//鼠標鬆開
		if(MessageDialog.openConfirm(shell, "關閉提示", "客觀不多玩會")){
			shell.close();//shell.dispose;
		}
	}
});
		
composite.addMouseTrackListener(new MouseTrackAdapter() {
	public void mouseExit(MouseEvent e) {//鼠標移開
		lblNewLabel.setImage(SWTResourceManager.getImage(qqdenglu.class, "/images/btn_close_normal.png"));
	}
			
	public void mouseHover(MouseEvent e) {//鼠標移上
		lblNewLabel.setImage(SWTResourceManager.getImage(qqdenglu.class, "/images/btn_close_highlight.png"));
	}
});




//項目一運行,必須先查看註冊表中有沒有以前記住過的用戶名和密碼
	Map<String,String>map=RegisterUtil.getAll();
	if(map!=null && map.size()>0){//說明有記錄
		Set<String>keys=map.keySet();//用戶名
		for(String key:keys){
			//添加到用戶下拉列表中
			combo.add(key);
		}
		combo.select(0);//默認選擇第一個
		btnCheckButton.setSelection(true);//自動選中記住密碼
		//密碼框顯示第一個賬號的密碼
		text_1.setText(map.get(combo.getItem(0)));
	}
		
		
	//當用戶名輸入框失去焦點時
	combo.addFocusListener(new FocusAdapter() {
		public void focusLost(FocusEvent e){
			//獲取當前的用戶名
			String uname=combo.getText().trim();
			//查看註冊表中是否存在
			if(map!=null && map.size()>0){
				if(map.containsKey(uname)){//如果存在則將用戶的密碼直接顯示在密碼框,且必須將記住密碼勾上
					text_1.setText(map.get(uname));
					btnCheckButton.setSelection(true)
				}else{
					text_1.setText("");
					btnCheckButton.setSelection(false);
				}
			}else{
				text_1.setText("");
				btnCheckButton.setSelection(false);
			}	
		}		
	});
		
	//點擊登錄
btnNewButton.addSelectionListener(new SelectionAdapter() {
			
	public void widgetSelected(SelectionEvent e) {
		String uname=combo.getText().trim();
		String pwd=text_1.getText().trim();
		//查詢數據庫
		DBHelper db=new DBHelper();
		String sql="select aid,aname,pwd,photo from adminInfo where aid=? and pwd=?";
		List<Object>params=new ArrayList<Object>();
		params.add(uname);
		params.add(pwd);
				
		Map <String,String> map=db.find(sql, params);
		if(map!=null&&map.size()>0){//說明根據用戶輸入的用戶名和密碼能夠在數據庫中查到相對應的數據,則說明他已經是註冊的用戶
			//判斷是否需要記住用戶名和密碼
			if(btnCheckButton.getSelection()){//如果需要記住,則寫入註冊表
				Map<String,String> map1=new HashMap<String,String>();
				map1.put(uname, pwd);
				RegisterUtil.add(map1);
			}else{
				RegisterUtil.remove(uname);
			}
			QQMenu menu=new QQMenu();
			shell.close();
			menu.open();
		}else{
			MessageDialog.openError(shell, "失敗提示", "用戶名或密碼錯誤,請確認後重新輸入。。。");
			}
		}
	});
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章