我开始使用 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>
我究竟做错了什么?
此致。