如何使用外部CSS文件正确设置HTML样式? [英] How do I style HTML correctly using an external CSS file?
问题描述
我希望能够从Internet加载HTML文档,将其显示在JEditorPane中,并使用外部CSS文件和/或从任何< style> ;. ..< / style>
标签。我现在正在做的是使用 jEditorPane.setPage(URL);
并且它的样式不正确。
I want to be able to load an HTML document from the Internet, display it in a JEditorPane and have it styled in Java using both an external CSS file and/or from any <style>...</style>
tags. What I'm doing right now is using jEditorPane.setPage(URL);
and it's not correctly styled.
推荐答案
基于JavaDoc - jEditorPane支持流血的边缘 HTML 3.2 和 CSS1 所以简短的回答是,你真的不想尝试渲染现代网页。
Based on the JavaDoc - jEditorPane supports the bleeding edge HTML 3.2 and CSS1 so the short answer is, you really don't want to try rendering modern web pages with it.
然而,你可以这样做:
However, you may be able to do this:
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);
URL url = new URL(location of your stylesheet);
StyleSheet styleSheet = new StyleSheet();
styleSheet.importStyleSheet(url)
kit.setStyleSheet(styleSheet);
这篇关于如何使用外部CSS文件正确设置HTML样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!