默认情况下,Java GWT的Object富文本区域字体为'times new roman',是否有办法将字体更改为'Arial'?
您有几个选项。您可以创建一个CSS类并将其设置在RichTextArea内的文档主体元素上,或者直接在它上面设置样式属性。如果您希望这种更改在整个应用程序中保持一致,我建议创建一个新类并添加一个InitializeHandler。
public class MyRichTextArea extends RichTextArea {
public MyRichTextArea() {
addInitializeHandler(new InitializeHandler() {
@Override
public void onInitialize(InitializeEvent ie) {
Document document = IFrameElement.as(getElement()).getContentDocument();
BodyElement body = document.getBody();
body.setAttribute("style", "font-family: Arial Unicode MS,Arial,sans-serif;");
});
}
}
}
最好使用提供的功能。如果您想在创建时更改它,您需要像答案1中提到的那样使用initializeHandler:
RichTextArea rta = new RichTextArea();
rta.addInitializeHandler(new InitializeHandler() {
public void onInitialize(InitializeEvent event) {
rta.getFormatter().setFontName("\"Trebuchet MS\",Trebuchet,Arial");
rta.getFormatter().setFontSize(FontSize.SMALL);
rta.getFormatter().setForeColor("#FF0000");
}
});
注意:在向文本框添加任何内容之前,您需要执行此操作,否则它只会应用于新输入。
我找到了更常见的解决方案:
addInitializeHandler(new InitializeHandler() {
public void onInitialize(InitializeEvent ie) {
Document document = IFrameElement.as(getElement()).getContentDocument();
BodyElement body = document.getBody();
HeadElement head = HeadElement.as(Element.as(body.getPreviousSibling()));
LinkElement styleLink = document.createLinkElement();
styleLink.setType("text/css");
styleLink.setRel("stylesheet");
styleLink.setHref("richtextarea.css");
head.appendChild(styleLink);
}
});
添加初始化处理程序后,更改 richtextarea.css
文件中的样式变得非常容易。