如何将控制从GUI创建的表单传递到codenameONe中的代码创建表单

时间:2015-12-06 08:18:40

标签: codenameone

我已经使用GUI构建器创建了一个表单,并通过加载谷歌地图的代码创建了另一个表单。表单中有一个按钮是使用GUI构建器创建的,我需要在单击按钮时转到地图。怎么做 ?请帮忙。

2 个答案:

答案 0 :(得分:1)

将actionListener添加到按钮,并在StateMachine中生成的方法中调用新表单。

您的表单应该在StateMachine中创建。

例如:

protected void onPage2_ButtonAction(Component c, ActionEvent event) {
    Form hi = new GmapForm("Native Maps Test");
    hi.show();
}

Gmap表单类:

public class GmapForm extends Form {

 public GmapForm(String title) {
    t.setTitle(title);
    setLayout(new BorderLayout());
    final MapContainer cnt = new MapContainer();
    addComponent(BorderLayout.CENTER, cnt);
    addCommand(new Command("Move Camera") {
        public void actionPerformed(ActionEvent ev) {
            cnt.setCameraPosition(new Coord(-33.867, 151.206));
        }
    });
    addCommand(new Command("Add Marker") {
        public void actionPerformed(ActionEvent ev) {

            System.out.println("Marker");

            try {
                cnt.setCameraPosition(new Coord(41.889, -87.622));
                cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
                    }
                });

            } catch(IOException err) {
                // since the image is iin the jar this is unlikely
                err.printStackTrace();
            }

        }
    });
    addCommand(new Command("Add Path") {
        public void actionPerformed(ActionEvent ev) {
            cnt.setCameraPosition(new Coord(-18.142, 178.431));
            cnt.addPath(new Coord(-33.866, 151.195), // Sydney
                new Coord(-18.142, 178.431),  // Fiji
                new Coord(21.291, -157.821),  // Hawaii
                new Coord(37.423, -122.091)  // Mountain View
            );
        }
    });
    addCommand(new Command("Clear All") {
        public void actionPerformed(ActionEvent ev) {
            cnt.clearMapLayers();
        }
    });
    revalidate();
  }

从Gmap类中删除上述代码并取消注释// new StateMachine("/theme");

答案 1 :(得分:0)

//地图加载类

import com.codename1.io.Log;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import userclasses.StateMachine;
import com.codename1.maps.Coord;
import com.codename1.ui.Command;
import com.codename1.ui.Dialog;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import java.io.IOException;

public class Gmap {

    private Form current;

    public void init(Object context) {
        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
        try {
            Resources theme = Resources.openLayered("/theme");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    public void start() {
        if(current != null){
            current.show();
            return;
        }
       // new StateMachine("/theme"); 
        Form hi = new Form("Native Maps Test");
        hi.setLayout(new BorderLayout());
        final MapContainer cnt = new MapContainer();
        hi.addComponent(BorderLayout.CENTER, cnt);
        hi.addCommand(new Command("Move Camera") {
            public void actionPerformed(ActionEvent ev) {
                cnt.setCameraPosition(new Coord(-33.867, 151.206));
            }
        });
        hi.addCommand(new Command("Add Marker") {
            public void actionPerformed(ActionEvent ev) {

                System.out.println("Marker");

                try {
                    cnt.setCameraPosition(new Coord(41.889, -87.622));
                    cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
                        }
                    });

                } catch(IOException err) {
                    // since the image is iin the jar this is unlikely
                    err.printStackTrace();
                }

            }
        });
        hi.addCommand(new Command("Add Path") {
            public void actionPerformed(ActionEvent ev) {
                cnt.setCameraPosition(new Coord(-18.142, 178.431));
                cnt.addPath(new Coord(-33.866, 151.195), // Sydney
                    new Coord(-18.142, 178.431),  // Fiji
                    new Coord(21.291, -157.821),  // Hawaii
                    new Coord(37.423, -122.091)  // Mountain View
                );
            }
        });
        hi.addCommand(new Command("Clear All") {
            public void actionPerformed(ActionEvent ev) {
                cnt.clearMapLayers();
            }
        });

        hi.show();
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
    }

    public void destroy() {
    }
}

//使用GUI构建器

创建的按钮的操作

protected void onPage2_ButtonAction(Component c,ActionEvent event){

//我想在这里重定向到地图页面

}