根据您所写的内容,我将假定 MyResources 是扩展 ClientBundle 的接口,而 MyCssResources 是扩展 CssResource 的接口:
interface MyResources extends ClientBundle {
@Source("myImage.png")
@ImageOptions(repeatStyle = RepeatStyle.BOTH)
ImageResource myImage();
@Source("myCss.css")
MyCssResource myCss();
}
interface MyCssResource extends CssResource {
String myBackground();
}
所以现在有两种方法可以使用从 MyResources 获取的 ImageResource。第一种是使用@sprite 指令将其附加到CSS 规则。myCss.css:
@sprite .myBackground {
gwt-image: "myImage";
/* Additional CSS rules may be added. */
}
然后,任何带有 myBackground 类的东西都将 myImage 作为其背景。因此,使用 UiBinder,例如:
<ui:UiBinder> <!-- Preamble omitted for this example. -->
<ui:with field="myResources" type="com.mycompany.MyResources"/>
<g:FlowPanel styleName="{myResources.myCss.myBackground}"/>
</ui:UiBinder>
也可以使用定义的 ImageResource 直接实例化 Image 对象。绑定器:
<ui:UiBinder> <!-- Preamble omitted for this example. -->
<ui:with field="myResources" type="com.mycompany.MyResources"/>
<g:Image resource="{myResources.myImage}"/>
</ui:UiBinder>
没有 UiBinder:
MyResources myResources = GWT.create(MyResources.class);
Image myImage = new Image(myResources.myImage());