J2ME---登录框

来源:互联网 发布:windows域的搭建 编辑:程序博客网 时间:2024/05/18 00:48

程序源代码:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ScreenDemo extends MIDlet implements CommandListener
{
    private Display display;//设备的显示器
    private TextField userName;
    private TextField password;
    private Form form;//表单
    private Command cancel;//取消命令
    private Command login;//登陆命令
    public ScreenDemo()
    {
       userName = new TextField("用户名","",10,TextField.ANY);
       password = new TextField("密码","",10,TextField.PASSWORD);
       form = new Form("用户登陆");
       cancel = new Command("取消",Command.CANCEL,1);
       login = new Command("登陆",Command.OK,2);
    }
   
    protected void startApp()
    {
       display = Display.getDisplay(this);
       Ticker ticker = new Ticker("请输入用户名和密码");
       form.setTicker(ticker);
       form.append(userName);
       form.append(password);
       form.addCommand(cancel);
       form.addCommand(login);
       form.setCommandListener(this);
       display.setCurrent(form);//显示表单
    }
   
    protected void pauseApp(){}//重载抽象类MIDlet的抽象方法pauseApp()
    protected void destroyApp(boolean u)
    {
       notifyDestroyed();
    }
    protected void validateUser(String name,String password)
    {
        if(name.equals("1")&&password.equals("1"))
        {
           passed();
        }
        else
        {
           tryAgain();
        }
    }
    protected void passed()
    {//登陆成功时显示登陆成功信息
       //登陆成功警报
       Alert pass = new Alert("登陆信息","你已经成功登陆",null,AlertType.ERROR);
       pass.setTimeout(Alert.FOREVER);
       display.setCurrent(pass,form);
    }
    protected void tryAgain()
    {//登陆错误时,显示错误信息
    //登陆错误警报
    Alert errors = new Alert("登陆错误","请重新输入用户名和密码",null,AlertType.ERROR);
    errors.setTimeout(Alert.FOREVER);
    userName.setString("");
    password.setString("");
    display.setCurrent(errors,form);
    }
   
    public void commandAction(Command c,Displayable d)
    {
       if(c == cancel)
       {
          destroyApp(false);
          notifyDestroyed();
       }
       else if(c == login)
       {
          validateUser(userName.getString(),password.getString());
       }
    }

程序运行结果:
} 

原创粉丝点击