如何写lwuit单选按钮代码

时间:2011-07-13 17:23:45

标签: java java-me lwuit state-machine

我有我的表单欢迎在这个表单上我有两个单选按钮 - 验证和注册和一个确定按钮。当用户选择一个单选按钮并按确定然后表单将显示但我无法做到这一点。请帮忙。

这是我的Statemachine类代码:

package userclasses;

import generated.StateMachineBase;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.Form;
import com.sun.lwuit.util.Resources;

public class StateMachine extends StateMachineBase implements ActionListener {

    Resources resources;
    RadioButton Verification = new RadioButton("Verification");
    RadioButton Enrollment = new RadioButton("Enrollment");
    StateMachineBase cl = new StateMachineBase() { };

    com.sun.lwuit.ButtonGroup bg=new ButtonGroup();

    Form fo, f;

    public StateMachine(String resFile) {
        super(resFile);
        // do not modify, write code in initVars and initialize class members there,
        // the constructor might be invoked too late due to race conditions that might occur
    }

    /**
     * this method should be used to initialize variables instead of
     * the constructor/class scope to avoid race conditions
     */
    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);
        f = cl.findWelcome(fo);
        //fo.addCommandListener(this);

        Verification = cl.findRadioButton1(f);
        Enrollment = cl.findRadioButton(f);
        f.addComponent(Verification);
        f.addComponent(Enrollment);
        //fo.addComponent(bg,null);
        bg.add(Enrollment);
        bg.add(Verification);
        Verification.addActionListener(this);
        Enrollment.addActionListener(this);
    }

    protected  void initVars() { }

    protected void onWelcome_OKAction(Component c, ActionEvent event) { }

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

    protected boolean onWelcomeEXIT() {
        // If the resource file changes the names of components this call will break   notifying you that you should fix the code
        boolean val = super.onWelcomeEXIT();
        return val;
    }

    protected void onWelcome_ButtonAction(Component c, ActionEvent event) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code  

        super.onWelcome_RadioButton1Action(c, event);
        super.onWelcome_RadioButtonAction(c, event);

        if(Verification.hasFocus()) {   
            showForm("Login",null);
        }
        else if(Enrollment.hasFocus()) {
            showForm("Authentication",null);
        }
        else {
            Dialog.show("INFORMATION","Please select option","OK","CANCEL");
        }
    }
|

2 个答案:

答案 0 :(得分:3)

当您从GUI构建器生成n​​etbeans项目时,src文件夹现在将包含您需要使用的res文件。每当您修改将重新生成StateMachineBase的GUI代码时,您只需重命名GUI构建器中的组件(可以通过单击树节点并按F2或在属性表中选择name属性来完成此操作)。 属性表允许您为支持它的每个组件分配一个事件(例如单选按钮操作事件),这将在StateMachine类中生成适当的回调方法(仅在StateMachine类中编写代码)。

单选按钮可以通过为它们提供相同的组名来与一个组关联。

答案 1 :(得分:0)

最简单的方法是使用资源编辑器。只需从LWUIT / util目录运行它。

要使用此工具创建项目,请按照此视频中的每一步操作:http://www.youtube.com/watch?v=HOfb8qiySd8。一定要看到它到底。

它将创建4个Netbeans项目(ProjectName,ProjectName_Desktop,ProjectName_MIDP,ProjectName_RIM)。修复依赖关系(对ProjectName和_MIDP最重要),你可以开始编码。

文件StateMachineBase.java将位于“generated”包中,这意味着每次在Resource Editor中更改内容时都会重新生成它。

在StateMachine类('userclasses'包)中实现所有内容,但不要在那里创建新方法,使用资源编辑器为您创建它们:资源编辑器 - > GUI Builder(左侧选项卡) - >选择组件 - >事件(右侧选项卡)。

现在,如果您想要做某些事情,例如,您想要更改TextField值,您将编写如下内容:

protected boolean onUstawieniaKontoZapisz() {
// If the resource file changes the names of components this call will break notifying you that you should fix the code //this comment was generated
boolean val = super.onUstawieniaKontoZapisz(); //generated

Form current = Display.getInstance().getCurrent();

TextField login = findUstawieniaKontoLoginTextField(current); //TextField name in Editor is: 'UstawieniaKontoLoginTextField' - a bit long I know, but it's unique
TextField password = findUstawieniaKontoHasloTextField(current); //same here, 'UstawieniaKontoHasloTextField' is second's TextField name

Configuration.setEmail(login.getText()); //Configuration class is used to store preferences
Configuration.setPassword(password.getText());

return val; //generated
}

您可以在StateMachineBase类中找到所有'find *'方法。使用资源编辑器(GUI Builder选项卡)添加的每个组件都有一个。

要将单选按钮分组到组中,请使用资源编辑器,选择每个单选按钮,然后在“属性”选项卡上找到“组”属性。将它设置为您希望在同一组中的每个单选按钮上的相同单词。