这就是CSS的用途.
Label label = new Label("your text");
label.setStyleName("myStyle");
Run Code Online (Sandbox Code Playgroud)
在你的.css文件中:
.myStyle {
background-color: #f00;
}
Run Code Online (Sandbox Code Playgroud)
或者如果您使用的是UIBinder:
<ui:style>
.myStyle {
background-color: #f00;
}
</ui:style>
<g:HTMLPanel>
<g:Label styleName="{style.myStyle}">Your text</g:Label>
</g:HTMLPanel>
Run Code Online (Sandbox Code Playgroud)
UIBinder方法的好处是您的样式与Label在同一文件中,CSS名称也将被混淆.