无法在SimpleLayout中加载Composite

时间:2013-07-12 18:12:11

标签: java gwt

我开始使用GWT,我遇到了一些困难。我正在尝试在SimpleLayout中加载一个Composite但没有出现。它不会抛出任何错误,所以我不知道在哪里看。

我的切入点如下:

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

public class AppDriver implements EntryPoint{

    private ExampleShell shell;

    @Override
    public void onModuleLoad() {
        shell = new ExampleShell();
        RootPanel.get().add(shell);

    }

}

这个类很好用,并加载看起来像这样的ExampleShell类:

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.ohapp.helloweb.client.contacts.ContactsUi;
import com.google.gwt.user.client.ui.SimplePanel;

    public class ExampleShell extends Composite {

        private static ExampleShellUiBinder uiBinder = GWT
                .create(ExampleShellUiBinder.class);


        @UiField Button btnHome;
        @UiField Button btnContacts;
        @UiField SimplePanel contentPanel;

        interface ExampleShellUiBinder extends UiBinder<Widget, ExampleShell> {
        }

        public ExampleShell() {
            initWidget(uiBinder.createAndBindUi(this));

            btnHome.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    //TODO
                }
            });

            btnContacts.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    try{
                        SimplePanel panel = ExampleShell.this.getContentPanel();
                        panel.clear();
                        ContactsUi contacts = new ContactsUi();
                        panel.add(contacts);

                    } catch (Exception ex){
                        System.out.println(ex.getStackTrace());
                    }
                }
            });
        }

        public SimplePanel getContentPanel(){
            return this.contentPanel;
        }

        public void setContentPanel(SimplePanel contentPanel){
            this.contentPanel = contentPanel;
        }
    }

我的ExampleShell.ui.xml如下所示:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <ui:style>
        .important {
            font-weight: bold;
        }
    </ui:style>
    <g:DockLayoutPanel>
        <g:north size="100.0">
            <g:FlowPanel>
                <g:Image width="80px" height="80px"/>
                <g:Button ui:field="btnHome">Home</g:Button>
                <g:Button ui:field="btnContacts">Contacts</g:Button>
            </g:FlowPanel>
        </g:north>
        <g:center>
            <g:SimplePanel ui:field="contentPanel"/>
        </g:center>


    </g:DockLayoutPanel>
</ui:UiBinder> 

直到这里一切正常。然而,当按下按钮btnContacts时,它应该将ContactsUi(扩展Composite)添加到布局,但我什么也看不到。它也不会抛出错误。

ContactsUi类如下所示:

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;


public class ContactsUi extends Composite {

    private static ContactsUiBinder uiBinder = GWT
            .create(ContactsUiBinder.class);

        interface ContactsUiBinder extends UiBinder<Widget, ContactsUi> {
    }

    public ContactsUi() {
        initWidget(uiBinder.createAndBindUi(this));


    }
}

ContactsUi.ui.xml如下:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <ui:style>
        .backGroundPanel {
            background-color: #F60;
        }
    </ui:style>
    <g:DockLayoutPanel styleName="{style.backGroundPanel}">
        <g:center>
            <g:FlowPanel>
                <g:Label text="New Label" />
                <g:Button>Contacts</g:Button>
            </g:FlowPanel>
        </g:center>

    </g:DockLayoutPanel>


</ui:UiBinder> 

我做错了什么?

最好的问候。

1 个答案:

答案 0 :(得分:2)

实现RequiresResize的小部件必须放在实现ProvidesResize的小部件中,或者被赋予显式大小(最好以像素为单位)。

在您的情况下,由于您的合成的根小部件DockLayoutPanel,您应该使用ResizeComposite代替Composite和{{ 1}}而不是RootLayoutPanel

请参阅https://code.google.com/p/google-web-toolkit/issues/detail?id=5874上的http://www.gwtproject.org/doc/latest/DevGuideUiPanels.html#Recipes和“使用没有RootLayoutPanel的LayoutPanel”方法(您可以在此页面中详细了解RootPanelRequiresResize