这是一个用SWT做的用户登陆程序:
----------------------可视化用户登陆系统----------------------------
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();
//创建一个主窗口
final Shell window = new Shell();
//打开主窗口大小
window.setBounds(30, 75, 300, 400);
//设置主窗口的标题
window.setText("登陆界面");
//创建触发按钮以及按钮的显示文字和大小位置
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);
//创建输入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);
//创建标签
Label nameLabel = new Label(window,SWT.NONE);
nameLabel.setText("账号:");
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){
//创建一个adm数组
Admin[] admin = new Admin[3];
//实例化三个账号
admin[0] = new Admin("admin", "admin");
admin[1] = new Admin("admin1", "pastNumber");
admin[2] = new Admin("admin2", "pastNumber");
//新建一个布尔型和整型数据,布尔值 用于判断,整型判断是那个账号登陆
boolean past = false;
int pastN = 0;
//判断账号密码是否正确
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;
}
}
//输出账号密码是否正确
if(past){
//从新打开一个界面输出欢迎用户
Shell welWindow = new Shell(window);
welWindow.setBounds(120, 130, 450, 200);
Label welText = new Label(welWindow,SWT.NONE);
welText.setText("欢迎 " +admin[pastN].getName() + "用户登陆!");
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("对不起,您输入的账号或密码有误!");
noPassText.setBounds(140, 60, 180, 90);
noPassWindow.open();
}
}
}
);
nosurebutton.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){{
window.close();
}}});
//打开窗体
window.open();
while(!window.isDisposed()){//如果主窗体没有关闭则一直循环
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;
}
}
=========================程序的运行结果如下==============================
打开时,用户的登陆界面
当输入的账号密码都正确时:
当密码或者账号不正确:
希望这个程序对大家有所帮助!
posted on 2010-10-18 01:43
tovep 阅读(407)
评论(2) 编辑 收藏