我基本上有两对窗口小部件,一个具有两个日期选择器和一个提交按钮,用于将日期提交到服务器上的RPC,而另一个是显示这些日期之间的信息的映射。它们每个都单独工作,但是显示地图时,我无法对日期选择器或“提交”按钮执行任何操作。

这是重要的代码

public void onModuleLoad() {
    final DockLayoutPanel dock = new DockLayoutPanel(Unit.PCT);
    /*
     * Asynchronously loads the Maps API.
     *
     * The first parameter should be a valid Maps API Key to deploy this
     * application on a public server, but a blank key will work for an
     * application served from localhost.
     */
    Maps.loadMapsApi("", "2", false, new Runnable() {
        public void run() {

            final FormPanel form = new FormPanel();
            form.setMethod(form.METHOD_POST);


            DatePicker datePicker = new DatePicker();
            final Label text = new Label();
            text.setText("Start Date");

            // Set the value in the text box when the user selects a date
            datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
                public void onValueChange(ValueChangeEvent<Date> event) {
                    Date date = event.getValue();
                    String dateString = DateTimeFormat.getMediumDateFormat().format(date);
                    text.setText("Start Date - " + dateString);
                }
            });

            // Set the default value
            datePicker.setValue(new Date(), true);

            // Add the widgets to the page
            RootPanel.get().add(text);
            RootPanel.get().add(datePicker);

            DatePicker datePicker2 = new DatePicker();
            final Label text2 = new Label();
            text2.setText("End Date");

            // Set the value in the text box when the user selects a date
            datePicker2.addValueChangeHandler(new ValueChangeHandler<Date>() {
                public void onValueChange(ValueChangeEvent<Date> event) {
                    Date date = event.getValue();
                    String dateString = DateTimeFormat.getMediumDateFormat().format(date);
                    text2.setText("End Date - " + dateString);
                }
            });

            // Set the default value
            datePicker2.setValue(new Date(), true);

            // Add the widgets to the page

            RootPanel.get().add(text2);
            RootPanel.get().add(datePicker2);
            RootPanel.get().add(new Button("Submit", new ClickListener()
            {
                public void onClick(Widget sender)
                {
                    form.submit();
                }
            }));

            form.addSubmitHandler(new SubmitHandler() {
                @Override
                public void onSubmit(SubmitEvent event) {
                    getAwards(text.getText(),text2.getText());
                }
            });

        }
    });
}


这是创建日期选择器的代码。

这是显示地图的代码。

private void buildUi() {
    ArrayList<Icon> icons = createIconList();
    content = new ArrayList<String>();
    LatLng sanDiego = LatLng.newInstance(32.83049, -117.122717);
    final MapWidget map = new MapWidget(sanDiego, 9);
    map.clearOverlays();
    map.setSize("100%", "100%");
    // Add some controls for the zoom level
    map.addControl(new LargeMapControl());

    java.util.Random rand = new java.util.Random();
    for(ContractAward ca : sanDiegoAwards)
    {
        double off1 = (rand.nextDouble()-.5)/100;
        double off2 = (rand.nextDouble()-.5)/100;
        index++;
        // Open a map centered on San Diego
        LatLng contract = LatLng.newInstance(ca.getLat() + off1,ca.getLon()+off2);

        MarkerOptions mo = MarkerOptions.newInstance();
        mo.setTitle(""+index);
        mo.setIcon(icons.get(whichIcon(ca.getAmount())));
        final Marker mark = new Marker(contract,mo);
        map.addOverlay(mark);

        String caContent = "<P>Company:  " + ca.getCompany() + "<br>";
        caContent+= "Date: " + ca.getDate().toGMTString() + "<br>";
        caContent+= "Amount: " + ca.getAmount() + "<br>";
        caContent+= "ContractID:  " + ca.getContractID() + "</P>";

        content.add(caContent);
        mark.addMarkerClickHandler(new MarkerClickHandler() {
            public void onClick(MarkerClickEvent event) {

                InfoWindow info = map.getInfoWindow();
                info.open(mark, new InfoWindowContent(content.get(Integer.parseInt(mark.getTitle())-1)));
            }
        });

    }
    final DockLayoutPanel dock = new DockLayoutPanel(Unit.PCT);
    dock.addEast(map, 80);


    // Add the map to the HTML host page
    RootLayoutPanel.get().add(dock);
}


我尝试将RootLayoutPanel.get()。add(dock)更改为RootPanel.get()。add(dock),但是地图本身不显示。我也尝试过更改所有要通过rootlayoutpanel停靠和插入的顶部部件,但是会出现与当前问题相同的问题。

最佳答案

乍一看,我看到了几种可能性。

1)您要将datepicker内容添加到RootPanel,但是将地图内容添加到RootLayoutPanel。我建议对两者都坚持使用RootLayoutPanel,如果它可以工作,那么标准模式通常在GWT中包含更多有用的最新信息。

2)为什么在第一行代码中使用Runnable进行全部操作?所有这些东西不仅仅存在于onModuleLoad中是有原因的吗?

07-27 19:33