Java ME - lwuit单选按钮

时间:2011-07-14 11:45:41

标签: java java-me lwuit

public class StateMachine extends StateMachineBase implements ActionListener {
  Resources resources;
 RadioButton Verifi=new RadioButton("Verification") ;
   RadioButton Enroll=new RadioButton("Enrollment");
StateMachineBase cl=new StateMachineBase()
        {};
ButtonGroup bg=new ButtonGroup();
static Form fo,f;
public StateMachine(String resFile) {
        super(resFile);
    }
  StateMachine()
    {

try{
    resources = Resources.open("/NEW AADHAR.res");
}
catch(java.io.IOException err)
{ err.printStackTrace(); }
cl.setHomeForm("Welcome");
 //fo = (Form)cl.startApp(resources,null,true);
fo=Display.getInstance().getCurrent();
f=cl.findWelcome(fo);
Verifi=cl.findVerification(f);
Enroll=cl.findEnrollment(f);
bg.add(Enroll);
bg.add(Verifi);
//f.addCommandListener(this);
Verifi.addActionListener(listener);Enroll.addActionListener(listener);
}
  protected  void initVars() {
    }

public void actionPerformed(ActionEvent ae) {

    throw new UnsupportedOperationException("Not supported yet.");
}       

ActionListener listener=new ActionListener(){
     protected void onWelcome_ButtonAction(Component c, ActionEvent event)
        {
Verifi.addActionListener(listener);
if(Verifi.hasFocus())
    {
    showForm("Login",null);

    }
    else if (Enroll.hasFocus())
    {
    showForm("Authentication",null);

   }
    else
        Dialog.show("INFORMATION","Select","OK","Cancel");

     }

        public void actionPerformed(ActionEvent ae) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

};
}

1 个答案:

答案 0 :(得分:0)

在ResourceEdit#GUI上为RadioButton提供组名(为每个RadioButton使用相同的名称)。使用以下方法返回RadioButton。所以使用这种方法。

public com.sun.lwuit.RadioButton findRadioButton(Container root) {
   return (com.sun.lwuit.RadioButton)findByName("RadioButton", root);
        //root - Pass the RadioButton added Component.
}

更新1:

使用此代码从生成的类

获取表单
GeneratedClass genClass = new GeneratedClass() { };
final Form form = (Form) genClass.startApp(resources, null, true);

GeneratedClass - 使用你的课程。 resources - 使用您的资源编辑

将该表单传递给findRadioButton(...);


更新2:

你给了2 RadioButton一个名为Verification and Enrollment。 所以使用以下代码,

 GeneratedClass genClass = new GeneratedClass() { };
 Form form = (Form) genClass.startApp(resources, null, true);
 RadioButton rbVerification = genClass.findVerification(form);
 RadioButton rbEnrollment = genClass.findEnrollment(form);

并检查ResourceEdit#GUI上的RadioButton Group名称。并为RadioButton提供相同的名称。


更新3:

StateMachine()课程中致电mainMidlet.java

public class StateMachine extends StateMachineBase implements ActionListener {

    Resources resources;
    RadioButton Verification;
    RadioButton Enrollment;

    public StateMachine(String resFile) {
        super(resFile);
    }

    /**
     * this method should be used to initialize variables instead of
     * the constructor/class scope to avoid race conditions
     */
    StateMachine() {
    }

    protected void initVars() {
    }

    /**
     * @param c
     * @param event
     */
    public void actionPerformed(ActionEvent ae) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    protected boolean onWelcomeEXIT() {
        boolean val = super.onWelcomeEXIT();
        return val;
    }

    protected void onWelcome_ButtonAction(Component c, ActionEvent event) {

        super.onSampleRB_RadioButtonAction(c, event);
        Verification = (RadioButton) c;  // initialize here
        if (Verification.hasFocus()) {
           showForm("Login",null);
        } else {
           Dialog.show("INFORMATION", "Please select option", "OK", "Cancel");
        }
    }
}