這是一個(gè)用SWT做的用戶(hù)登陸程序:
----------------------可視化用戶(hù)登陸系統(tǒng)----------------------------
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Swt_Check {
public static void main(String[] args) {
Display mianWindow = new Display();
//創(chuàng)建一個(gè)主窗口
final Shell window = new Shell();
//打開(kāi)主窗口大小
window.setBounds(30, 75, 300, 400);
//設(shè)置主窗口的標(biāo)題
window.setText("登陸界面");
//創(chuàng)建觸發(fā)按鈕以及按鈕的顯示文字和大小位置
Button surebutton = new Button(window,SWT.NONE);
Button nosurebutton = new Button(window,SWT.NONE);
surebutton.setText("確定");
nosurebutton.setText("取消");
surebutton.setBounds(160, 235, 75, 30);
nosurebutton.setBounds(60, 235, 75, 30);
//創(chuàng)建輸入Text框及其位置
final Text nameText = new Text(window,SWT.NONE);
final Text passNumber = new Text(window,SWT.PASSWORD);
nameText.setBounds(100, 100, 110, 20);
passNumber.setBounds(100, 160, 110, 20);
//創(chuàng)建標(biāo)簽
Label nameLabel = new Label(window,SWT.NONE);
nameLabel.setText("賬號(hào):");
nameLabel.setBounds(60, 105, 40,20);
Label passLabel = new Label(window,SWT.NONE);
passLabel.setText("密碼:");
passLabel.setBounds(60, 165, 40, 20);
//輸入后單擊確定后的操作
surebutton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
//創(chuàng)建一個(gè)adm數(shù)組
Admin[] admin = new Admin[3];
//實(shí)例化三個(gè)賬號(hào)
admin[0] = new Admin("admin", "admin");
admin[1] = new Admin("admin1", "pastNumber");
admin[2] = new Admin("admin2", "pastNumber");
//新建一個(gè)布爾型和整型數(shù)據(jù),布爾值 用于判斷,整型判斷是那個(gè)賬號(hào)登陸
boolean past = false;
int pastN = 0;
//判斷賬號(hào)密碼是否正確
for(int i=0;i<admin.length;i++){
if(admin[i].getName().equals(nameText.getText())&&admin[i].getPastNumber().equals(passNumber.getText())){
past = true;
pastN = i;
break;
}
}
//輸出賬號(hào)密碼是否正確
if(past){
//從新打開(kāi)一個(gè)界面輸出歡迎用戶(hù)
Shell welWindow = new Shell(window);
welWindow.setBounds(120, 130, 450, 200);
Label welText = new Label(welWindow,SWT.NONE);
welText.setText("歡迎 " +admin[pastN].getName() + "用戶(hù)登陸!");
welText.setBounds(140, 60, 180, 90);
welWindow.open();
}
else{
Shell noPassWindow = new Shell(window);
noPassWindow.setBounds(120, 130, 450, 200);
Label noPassText = new Label(noPassWindow,SWT.NONE);
noPassText.setText("對(duì)不起,您輸入的賬號(hào)或密碼有誤!");
noPassText.setBounds(140, 60, 180, 90);
noPassWindow.open();
}
}
}
);
nosurebutton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){{
window.close();
}}});
//打開(kāi)窗體
window.open();
while(!window.isDisposed()){//如果主窗體沒(méi)有關(guān)閉則一直循環(huán)
if(!mianWindow.readAndDispatch()){//如果display不忙
mianWindow.sleep();//休眠
}
}
mianWindow.dispose();
}
}
class Admin{
private String name;
private String pastNumber;
Admin(String name, String pastNumber){
this.name = name;
this.pastNumber = pastNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPastNumber() {
return pastNumber;
}
public void setPastNumber(String pastNumber) {
this.pastNumber = pastNumber;
}
}
=========================程序的運(yùn)行結(jié)果如下==============================
打開(kāi)時(shí),用戶(hù)的登陸界面
當(dāng)輸入的賬號(hào)密碼都正確時(shí):
當(dāng)密碼或者賬號(hào)不正確:
希望這個(gè)程序?qū)Υ蠹矣兴鶐椭?br />
posted on 2010-10-18 01:43
tovep 閱讀(412)
評(píng)論(2) 編輯 收藏