如何编写 lwuit 单选按钮代码

发布于 2024-11-19 21:49:27 字数 2904 浏览 8 评论 0原文

我的表单“欢迎”在此表单上有两个单选按钮 - 验证和注册以及“确定”按钮。当用户选择其中一个单选按钮并按“确定”时,将显示一个表单,但我无法执行此操作。请帮忙。

这是我的 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");
        }
    }
|

I have my form Welcome on this form i have two radio buttons-Verification and enrollment and a OK button .when user select one of radio buttons and press OK then a form will show but i am not able to do that. Please help.

this is my Statemachine class code:

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");
        }
    }
|

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2024-11-26 21:49:27

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

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

When you generate a netbeans project from the GUI builder the src folder will now contain the res file you need to work with. Whenever you modify the GUI code that StateMachineBase will be regenerated so you can just rename the components in the GUI builder (you can do this by clicking on the tree node and pressing F2 or by selecting the name attribute in the properties table).
The properties table allows you to assign an event for every component that supports it (e.g. radio button action events) which will generate the appropriate callback method in the StateMachine class (write your code only in the StateMachine class).

Radio buttons can be associated with one group by giving them the same group name.

蒗幽 2024-11-26 21:49:27

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

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

它将创建 4 个 Netbeans 项目(ProjectName、ProjectName_Desktop、ProjectName_MIDP、ProjectName_RIM)。修复依赖关系(对于 ProjectName 和 _MIDP 来说最重要),然后您就可以开始编码了。

文件 StateMachineBase.java 将位于“生成”包中,这意味着每次您在资源编辑器中更改某些内容时都会重新生成它。

在 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 构建器选项卡)添加的每个组件都有一个。

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

The easiest way to do it is to use Resource Editor. Simply run it from LWUIT/util directory.

To create project using this tool follow each step from this video: http://www.youtube.com/watch?v=HOfb8qiySd8. Be sure to watch it to the end.

It will create 4 Netbeans projects (ProjectName, ProjectName_Desktop, ProjectName_MIDP, ProjectName_RIM). Fix depedencies (most important for ProjectName and _MIDP one) and you can start coding.

File StateMachineBase.java will be located in 'generated' package, which means that it will be regenerated every time you change something in Resource Editor.

Implement everything in StateMachine class ('userclasses' package), but don't create new methods there, use Resource Editor to create them for you: Resource Editor -> GUI Builder (tab on left side) -> Select component -> Events (tab on the right).

Now, if you want to do something for example, you want to change TextField value, you will write something like this:

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
}

You can find all 'find*' methods inside StateMachineBase class. There is one for each Component you have added using Resource Editor (GUI Builder tab).

For grouping radio buttons into groups use Resource Editor too, select each radio button and on Properties tab find 'Group' property. Set it to the same word on every radio button you want to have in the same group.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文