我为VerticalLayout面板的边框声明了一些颜色,如:
<ui:style>
.onMouseOverBorderColor {border-color: red; border-style: outset}
.onMouseOutBorderColor {border-color: black; border-style: outset}
</ui:style>
然后我想根据鼠标的位置更改面板边框的颜色,并向我的窗口小部件的构造函数添加以下内容:
clickable.addMouseOverHandler(new MouseOverHandler() {
@Override
public void onMouseOver(MouseOverEvent event) {
GWT.log("mouse over");
border.setStyleName("onMouseOverBorderColor");
}
});
clickable.addMouseOutHandler(new MouseOutHandler() {
@Override
public void onMouseOut(MouseOutEvent event) {
GWT.log("mouse out");
border.setStyleName("onMouseOutBorderColor");
}
});
但......没有任何反应!我做错了什么?
建议后的代码(不起作用):
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<ui:style>
.fontStyleTitle {font-weight: bold }
.border {border-color: black; border-style: outset}
.border:hover {border-color: red; border-style: outset}
</ui:style>
<g:FocusPanel ui:field="clickable">
<g:VerticalPanel ui:field="border" borderWidth="1" styleName="style.border">
<g:Image ui:field="myImage"/>
<g:Label ui:field="myTitle" horizontalAlignment="ALIGN_CENTER" styleName="{style.fontStyleTitle}"/>
</g:VerticalPanel>
</g:FocusPanel>
</ui:UiBinder>
和java类:
public UiWidget(String path, String theTitle) {
initWidget(uiBinder.createAndBindUi(this));
GWT.log(URL_PREFIX+path);
myImage.setUrl(URL_PREFIX+path);
myTitle.setText(theTitle);
myImage.setSize(IMG_WIDTH, IMG_HEIGHT);
/*
clickable.addMouseOverHandler(new MouseOverHandler() {
@Override
public void onMouseOver(MouseOverEvent event) {
GWT.log("mouse over");
}
});
clickable.addMouseOutHandler(new MouseOutHandler() {
@Override
public void onMouseOut(MouseOutEvent event) {
GWT.log("mouse out");
}
*/
}
答案 0 :(得分:13)
默认情况下,在UiBinder中声明的所有样式都会被混淆。
这意味着你的风格'onMouseOverBorderColor'将成为'GLX0QCICAR'。
但是当您使用JAVA代码时:
border.setStyleName("onMouseOverBorderColor");
你的border元素确实有'onMouseOverBorderColor'样式。
所以2个解决方案:
使用外部不混淆样式名称:
<ui:style>
@external onMouseOverBorderColor onMouseOutBorderColor;
.onMouseOverBorderColor {border-color: red; border-style: outset}
.onMouseOutBorderColor {border-color: black; border-style: outset}
</ui:style>
在JAVA代码中使用混淆样式:
<ui:style type="your.package.name.UiWidget.MyStyle">
.onMouseOverBorderColor {border-color: red; border-style: outset}
.onMouseOutBorderColor {border-color: black; border-style: outset}
</ui:style>
public class UiWidget {
...
public interface MyStyle extends CssResource {
String onMouseOverBorderColor();
String onMouseOutBorderColor();
}
@UiField
protected MyStyle style;
public UiWidget(String path, String theTitle) {
...
clickable.addMouseOverHandler(new MouseOverHandler() {
@Override
public void onMouseOver(MouseOverEvent event) {
border.setStyleName(style.onMouseOverBorderColor);
}
});
...
}
}
答案 1 :(得分:3)
你不能像这样引用css styleName。在您的示例中,<ui:style>
中的stylename只能在ui binder文件中使用,因为它被GWT混淆了。您应该将样式放在CSSResource中。并将样式放在css文件而不是uibinder文件中,并设置css资源样式名而不是普通字符串。
但如果您只想更改某些css,您也可以使用悬停选择器而无需任何代码:
<ui:style>
.border {border-color: black; border-style: outset}
.border:hover {border-color: red; border-style: outset}
</ui:style>
并在uibinder文件中将窗口小部件上的边框样式设置为属性:styleName="{style.border}"