Vaadin中按钮的java多行标题不起作用
我使用的是Vaadin,我想给一个有多行的按钮添加一个标题——它应该只显示String - 2 linebreaks - String
,我没想到这会太难做到
现在不幸的是,这比我预期的要难一点——我的数据结构应该显示一个时间表(所以我使用的是一个扩展网格的结构),我希望所有的时间段都有相应的讲座和讲师。但是,我必须使用Button元素(而不是NativeButton),因此使用HTML标题不起作用
所以我尝试的是这样一个css类:
.multiline-button {
width: 125px;
white-space: normal !important;
height: auto;
word-break: break-word !important;
word-wrap: break-word !important;
background-color: red !important;
}
(实际上背景色只是为了测试是否应用了样式)-我还尝试通过button.setStyle(name, value);
在代码中手动设置这些属性。不幸的是,这些都没有被应用到按钮上。我的按钮创建代码:
@CssImport("./styles/multiline-button.css")
public class NewScheduleTimetable<T> extends TimetableWithValue<T> {
@Override
protected Button createSlot(int day, int timeSlot) {
final Button slotButton = new Button();
/* even desperately tried removing all other themes, didn't even change anything
slotButton.getThemeNames().forEach(theme -> {
slotButton.removeThemeName(theme);
}); */
slotButton.setWidthFull();
slotButton.setHeight("125px");
slotButton.addClassName("multiline-button");
//this sets the text
if (hasValue(day, timeSlot)) {
slotButton.setText(getText(getValue(day, timeSlot)));
}
this.recalculateColumnWidths();
return slotButton;
}
}
现在,按钮被成功创建,并显示了它们应有的名称-它们只是缺少标题的换行符。如果我在浏览器中检查元素,标题中确实包含换行符(换行符通过\n
完成)
所以我对瓦丁很陌生,但通常这些事情只需要五分钟,我不需要几天就能想出不同的方法(不幸的是,这些方法都没有奏效)——我在这里错过了什么
# 1 楼答案
您可以做的是将一些HTML内容设置为图标组件,尽管按钮似乎不是为此而设计的
Button button = new Button(new Html("<div>Line 1<br /><br />Line 2</div>"));
请注意,Html内容必须包含在一个元素中,就像上面的
div
一样这还添加了
icon
主题,您可能希望通过button.getThemeNames().remove("icon");
删除该主题如果你把CSS改成例如
这会让你走得很远