lwuit 使用dialog设计登录界面

废话不多说。直接看效果上源码。

image

 

package com.ceshi.text;

import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;

import com.sun.lwuit.table.TableLayout;


public class LoginDialog extends Dialog {//implements  DataChangedListener{//ActionListener,
    Button cmdLogin;
    Button cmdExit;
    Command clear =new Command ("取消",0);
    TextField userName;
     TextField password;
    public   ButtonGroup group = new ButtonGroup();
    public  LoginDialog(){       
        addCommand(clear);//
        CheckBox cb=new CheckBox("记住用户名密码");
        cb.setSelected(true);
        userName=new TextField("");
        password=new TextField();
        userName.setConstraint(TextField.ANY);
        password.setConstraint(TextField.PASSWORD);
        TableLayout layout = new TableLayout(6, 2); 
        setLayout(layout); 
        Label l=new Label("用户登录");
        l.setAlignment(CENTER);
        TableLayout.Constraint constraint = layout.createConstraint(); 
        constraint.setHorizontalSpan(2);
        addComponent(constraint,l); 
        addComponent(new Label("用户名:"));
        constraint = layout.createConstraint();
        constraint.setWidthPercentage(60);       
         addComponent(constraint,userName);
        addComponent( new Label("密码:")); 
        constraint = layout.createConstraint();    
        constraint.setWidthPercentage(60);     
        addComponent( constraint,password); 
        RadioButton rb1=new RadioButton("信息员");
        RadioButton rb2=new RadioButton("用户");
        cmdExit = new Button("退出"); //退出按钮
        cmdLogin = new Button("登录"); //登录按钮
        constraint = layout.createConstraint(); 
        constraint.setWidthPercentage(30); 
       // rb1.setSelected(true);
        addComponent(rb1);
        addComponent(rb2);
        constraint = layout.createConstraint();
        constraint.setHorizontalSpan(2);
        addComponent( constraint,cb);
        addComponent(cmdExit );
        addComponent(cmdLogin);
        ActionListener listener = new ActionListener() {
               //这里是处理CheckBox、RadioButton 点击事件处理程序加入监听         
        public void actionPerformed(ActionEvent ae) {///////////
                  Object source = ae.getSource();
                  if(source==cmdLogin){
                      String str="ButtonCount:"+group.getButtonCount()+'\n'+
                              "SelectedIndex:"+group.getSelectedIndex()+'\n'+
                              "RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
                   Dialog.show("Rb_Cb_Lb",str, "OK", null);
                  }
                 
             }
        };
       
        cmdExit.addActionListener(listener);
        cmdLogin.addActionListener(listener);
        cb.addActionListener(listener);
        group.add(rb1);
        group.add(rb2);
        group.setSelected(rb2);
       
        rb1.addActionListener(listener);
        rb2.addActionListener(listener);
        setCommandsAsButtons(true);//设置dialog加载command
        setAutoDispose(false);//设置diolog 一直显示
        this.setFocused(userName);
       
    }

   
}

public void startApp() { 
        Display.init(this); 
        Display.init(this);
        Resources rs = null;
       try {
           rs = Resources.open("/dialog.res");   
           } catch (IOException e) { e.printStackTrace();}    
       UIManager.getInstance().setThemeProps(rs.getTheme("DialogTheme"));          
       LoginDialog ld=new LoginDialog();
        ld.show();
    } 

在查看资料时,有发现说如果这样设置setTimeout(0);则会使得Dialog一直显示,直到调用Dispose才会关闭,但是当我应用时发现不是这样的。setTimeout(0)会使得dialog一闪而过。若是要是dialog直到调用dispose才关闭的话应该用setAutoDispose(false);进行设置。

你可能感兴趣的:(设计,border,Alt,target,blank)