我需要一个更大的文本作为标题使用Valo主题在Vaadin 8应用程序中标记表单的部分。
在早期版本的Vaadin中,我记得使用了一个Label,它以某种方式分配了一个预定义的样式"H1“(就像在HTML "h1”标记中那样),其中该样式是驯鹿主题的一部分。
通过调用Label::addStyleName并传递常量ValoTheme.LABEL_H1,我尝试在Vaadin 8中执行此操作,但失败了。
发布于 2017-04-21 04:01:23
是的,的确,调用addStyleName在Label上并传递常量ValoTheme.LABEL_H1是有效的,这是根据莫里基的评论。
final Label labelPlain = new Label ( "This is a plain Label in Vaadin 8.1.0 Alpha 6." );
final Label labelH1 = new Label ( "This is a 'h1' Label in Vaadin 8.1.0 Alpha 6." );
labelH1.addStyleName ( ValoTheme.LABEL_H1 );下面是一个完整的示例应用程序,它是从Vaadin8.1.0Alpha 6中常用的Vaadin原型 vaadin-archetype-application修改而来的。
package com.example.try_h1;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of a html page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
@Theme ( "mytheme" )
public class MyUI extends UI {
@Override
protected void init ( VaadinRequest vaadinRequest ) {
final VerticalLayout layout = new VerticalLayout ( );
final Label labelPlain = new Label ( "This is a plain Label in Vaadin 8.1.0 Alpha 6." );
final Label labelH1 = new Label ( "This is a 'h1' Label in Vaadin 8.1.0 Alpha 6." );
labelH1.addStyleName ( ValoTheme.LABEL_H1 );
layout.addComponents ( labelPlain, labelH1 );
setContent ( layout );
}
@WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet {
}
}运行示例应用程序的屏幕截图。

addStyleName对setStyleName
请小心调用addStyleName而不是setStyleName。第二个方法清除所有现有的样式,代之以您的一个样式参数。这不是您想要的,因为您将失去所有由Vaadin框架分配的现有样式。
https://stackoverflow.com/questions/43516325
复制相似问题