下面列出了怎么用javax.swing.text.html.StyleSheet的API类实例代码及写法,或者点击链接到github查看源代码。
private void postInitComponents () {
this.jLabel2.setVisible(false);
this.platformHome.setVisible(false);
final Collection installFolders = platform.getInstallFolderURLs();
if (platform.getInstallFolders().isEmpty() && installFolders.size() > 0) {
this.jLabel2.setVisible(true);
this.platformHome.setVisible(true);
this.platformHome.setForeground(new Color (164,0,0));
this.platformHome.setText (Utilities.toFile(URI.create(((URL)installFolders.iterator().next()).toExternalForm())).getAbsolutePath());
}
HTMLEditorKit htmlkit = new HTMLEditorKit();
StyleSheet css = htmlkit.getStyleSheet();
if (css.getStyleSheets() == null) {
StyleSheet css2 = new StyleSheet();
Font f = jLabel2.getFont();
css2.addRule(new StringBuffer("body { font-size: ").append(f.getSize()) // NOI18N
.append("; font-family: ").append(f.getName()).append("; }").toString()); // NOI18N
css2.addStyleSheet(css);
htmlkit.setStyleSheet(css2);
}
jTextPane1.setEditorKit(htmlkit);
jTextPane1.setText(NbBundle.getMessage(BrokenPlatformCustomizer.class,"MSG_BrokenProject"));
}
/**
* 初始化使用帮助tab
*/
public static void init() {
helpForm = getInstance();
try {
helpForm.getHelpTextPane().setEditable(false);
HTMLEditorKit kit = new HTMLEditorKit();
helpForm.getHelpTextPane().setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("h2{color:#FBC87A;}");
helpForm.getHelpTextPane().setContentType("text/html; charset=utf-8");
helpForm.getHelpTextPane().setPage(MainWindow.class.getResource("/page/help.html"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method creates and returns a stylesheet that makes the documentation look as it's
* supposed to look.
*
* @return the stylesheet
*/
private StyleSheet createStyleSheet(StyleSheet css) {
css.addRule("body {font-family: Open Sans; font-size: 10px;}");
css.addRule("p {font-size:10px; font-family: Open Sans; margin-top: 0px; padding-top: 0px;}");
css.addRule("ul li {padding-bottom:1ex; font-family: Open Sans; font-size:10px; list-style-type: circle;}");
css.addRule("h2 {font-size:14px; font-family: Open Sans; margin-bottom: 0px; margin-top: 0px;}");
css.addRule("h4 {color: #000000; font-size:10px; font-family: Open Sans; font-weight: bold; margin-bottom: 5px;}");
css.addRule("h5 {color: #3399FF; font-size:11px; font-family: Open Sans;}");
css.addRule("h5 img {margin-right:8px; font-family: Open Sans;}");
css.addRule(
".parametersHeading {color: #000000; font-size:10px; font-family: Open Sans; font-weight: bold; margin-bottom: 0px;}");
css.addRule(".parametersTable {cellspacing: 0px; border: 0;}");
css.addRule(".typeIcon {height: 10px; width: 10px;}");
css.addRule("td {vertical-align: top; font-family: Open Sans;}");
css.addRule(".lilIcon {padding: 2px 4px 2px 0px;}");
css.addRule("td {font-size: 10px; font-family: Open Sans;}");
css.addRule(".packageName {color: #777777; font-size:10px; font-family: Open Sans; font-weight: normal;}");
css.addRule(".parameterDetails {color: #777777; font-size:9px; font-family: Open Sans;}");
css.addRule(".parameterDetailsCell{margin-bottom: 4px; padding-bottom: 4px;}");
css.addRule(".tutorialProcessLink {margin-top: 6px; margin-bottom: 5px;}");
css.addRule("hr {border: 0;height: 1px;}");
css.addRule("a {color:" + SwingTools.getColorHexValue(Colors.LINKBUTTON_LOCAL) + "}");
css.addRule("table {align:left;}");
css.addRule(".tags {font-size: 9px; color: #777777;}");
return css;
}
public ExtendedHTMLEditorKit() {
styleSheet = new StyleSheet();
try {
InputStream is = HTMLEditorKit.class.getResourceAsStream(DEFAULT_CSS);
Reader r = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));
styleSheet.loadRules(r, null);
r.close();
} catch (Exception e) {
// LogService.getRoot().log(Level.WARNING, "Cannot install stylesheet: "+e, e);
LogService.getRoot().log(
Level.WARNING,
I18N.getMessage(LogService.getRoot().getResourceBundle(),
"com.rapidminer.gui.tools.ExtendedHTMLEditorKit.installing_stylesheet_error", e), e);
// on error we simply have no styles... the html
// will look mighty wrong but still function.
}
}
/**
* Creates the main text representation of this result.
*
* @param text
* @return
*/
private Component makeMainLabel(String text) {
JEditorPane label = new ExtendedHTMLJEditorPane("text/html", text);
StyleSheet css = ((HTMLEditorKit) label.getEditorKit()).getStyleSheet();
css.addRule("body {font-family:Sans;font-size:11pt}");
css.addRule("h3 {margin:0; padding:0}");
css.addRule("h4 {margin-bottom:0; margin-top:1ex; padding:0}");
css.addRule("p {margin-top:0; margin-bottom:1ex; padding:0}");
css.addRule("ul {margin-top:0; margin-bottom:1ex; list-style-image: url("
+ Tools.getResource("icons/help/circle.png") + ")}");
css.addRule("ul li {padding-bottom: 2px}");
css.addRule("li.outPorts {padding-bottom: 0px}");
css.addRule("ul li ul {margin-top:0; margin-bottom:1ex; list-style-image: url("
+ Tools.getResource("icons/help/line.png") + ")");
css.addRule("li ul li {padding-bottom:0}");
label.setEditable(false);
label.setBackground(Colors.WHITE);
JScrollPane pane = new JScrollPane(label);
pane.setBackground(Colors.WHITE);
pane.setBorder(null);
return pane;
}
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private static void patchStyledEditorKit() {
try {
StyleSheet defaultStyles = new StyleSheet();
InputStream is = DarculaLaf.class.getResourceAsStream("darcula.css");
Reader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
defaultStyles.loadRules(r, null);
r.close();
final Field keyField = HTMLEditorKit.class.getDeclaredField("DEFAULT_STYLES_KEY");
keyField.setAccessible(true);
final Object key = keyField.get(null);
AppContext.getAppContext().put(key, defaultStyles);
} catch (Throwable e) {
log(e);
}
}
private boolean isInvisible( final javax.swing.text.Element textElement ) {
final HTMLDocument htmlDocument = (HTMLDocument) textElement.getDocument();
final StyleSheet sheet = htmlDocument.getStyleSheet();
final AttributeSet attr = computeStyle( textElement, sheet );
final Object o = attr.getAttribute( CSS.Attribute.DISPLAY );
if ( "none".equals( String.valueOf( o ) ) ) {
return true;
}
final Object tag = findTag( textElement.getAttributes() );
if ( tag == HTML.Tag.COMMENT ) {
return true;
}
if ( tag == HTML.Tag.SCRIPT ) {
return true;
}
if ( tag == HTML.Tag.HEAD ) {
return true;
}
return false;
}
public void setHtmlText(String htmlText) {
textPane1.setContentType("text/html; charset=utf-8");
HTMLEditorKit kit = new HTMLEditorKit();
textPane1.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("h2{color:#FBC87A;}");
styleSheet.addRule("body{font-family:" + buttonOK.getFont().getName() + ";font-size:" + buttonOK.getFont().getSize() + ";}");
textPane1.setText(htmlText);
textPane1.setCaretPosition(0);
}
public void setHtmlText(String htmlText) {
textPane1.setContentType("text/html; charset=utf-8");
HTMLEditorKit kit = new HTMLEditorKit();
textPane1.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("h2{color:#FBC87A;}");
styleSheet.addRule("body{font-family:" + buttonOK.getFont().getName() + ";font-size:" + buttonOK.getFont().getSize() + ";}");
textPane1.setText(htmlText);
}
public void setHtmlText(String htmlText) {
htmlText = htmlText.replaceAll("\n", "<br/>");
editorPane.setContentType("text/html; charset=utf-8");
HTMLEditorKit kit = new HTMLEditorKit();
editorPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("span{background-color:#FBC87A;color:2B2B2B}");
styleSheet.addRule("body{font-family:" + findResultCount.getFont().getName() + ";font-size:" + findResultCount.getFont().getSize() + ";}");
editorPane.setText(htmlText);
editorPane.setCaretPosition(0);
}
/**
* Load the css style sheet used for html display in text components with a {@link
* javax.swing.text.html.HTMLEditorKit}.
*
* @param loaderClass the class to resolve the location of the style sheet.
* @return the {@link StyleSheet}.
*/
public final StyleSheet loadStyleSheetWithClass(final Class<?> loaderClass) {
StyleSheet styleSheet = new StyleSheet();
try (InputStream in = loaderClass.getResourceAsStream(getResourcePath() + getPrefix() + "_styleSheet.css");
InputStreamReader inReader = new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader r = new BufferedReader(inReader)) {
styleSheet.loadRules(r, null);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace());
}
return styleSheet;
}
@Override
public StyleSheet getStyleSheet() {
if (style == null) {
style = super.getStyleSheet();
}
return style;
}
public static void loadStyleAsync(final StyleSheet styleSheet) {
// new Thread() {
// @Override
// public void run() {
// loadStyleSync(styleSheet);
// }
// }.start();
}
public static void loadStyleSync(final StyleSheet styleSheet) {
try {
styleSheet.importStyleSheet(new URL("http://dl.ieclipse.cn/r/smartim.css"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
protected void initHistoryWidget() {
HTMLEditorKit kit = new HTMLEditorKit() {
@Override
public ViewFactory getViewFactory() {
return new WrapHTMLFactory();
}
};
final StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {text-align: left;}");
styleSheet.addRule(".my {font-size: 1 em; font-style: italic; float: left;}");
styleSheet.addRule("div.error {color: red;}");
styleSheet.addRule("img {max-width: 100%; display: block;}");
styleSheet.addRule(".sender {display: inline; float: left;}");
styleSheet.addRule(".content {display: inline-block; white-space: pre-warp; padding-left: 4px;}");
styleSheet.addRule(".br {height: 1px; line-height: 1px; min-height: 1px;}");
RestUtils.loadStyleAsync(styleSheet);
HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
String initText = String.format("<html><head></head><body>%s</body></html>", imPanel.getWelcome());
historyWidget.setContentType("text/html");
historyWidget.setEditorKit(kit);
historyWidget.setDocument(doc);
historyWidget.setText(initText);
historyWidget.setEditable(false);
historyWidget.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
String desc = e.getDescription();
if (!StringUtils.isEmpty(desc)) {
hyperlinkActivated(desc);
}
}
}
});
}
public static void loadStyleAsync(final StyleSheet styleSheet) {
// new Thread() {
// @Override
// public void run() {
// loadStyleSync(styleSheet);
// }
// }.start();
}
public static void loadStyleSync(final StyleSheet styleSheet) {
try {
styleSheet.importStyleSheet(new URL("http://dl.ieclipse.cn/r/smartim.css"));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
/** Creates new form InstallPanelContainer */
public PanelBodyContainer (String heading, String msg, JPanel bodyPanel) {
head = heading;
message = msg;
this.bodyPanel = bodyPanel;
initComponents ();
HTMLEditorKit htmlkit = new HTMLEditorKitEx();
// override the Swing default CSS to make the HTMLEditorKit use the
// same font as the rest of the UI.
// XXX the style sheet is shared by all HTMLEditorKits. We must
// detect if it has been tweaked by ourselves or someone else
// (code completion javadoc popup for example) and avoid doing the
// same thing again
StyleSheet css = htmlkit.getStyleSheet();
if (css.getStyleSheets() == null) {
StyleSheet css2 = new StyleSheet();
Font f = new JList().getFont();
int size = f.getSize();
css2.addRule(new StringBuffer("body { font-size: ").append(size) // NOI18N
.append("; font-family: ").append(f.getName()).append("; }").toString()); // NOI18N
css2.addStyleSheet(css);
htmlkit.setStyleSheet(css2);
}
tpPanelHeader.setEditorKit(htmlkit);
tpPanelHeader.putClientProperty( JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE );
writeToHeader (head, message);
initBodyPanel ();
}
@Override
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new CustomHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
@Override
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new CustomHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
public static void main(String[] args) {
StyleSheet styleSheet = new StyleSheet();
AttributeSet attributeSet = styleSheet.
getDeclaration("border-color: rgb(1, 2, 3) rgb(1, 2, 4);");
if (!attributeSet.getAttribute(BORDER_TOP_COLOR).toString()
.equals("rgb(1, 2, 3)") ||
!attributeSet.getAttribute(BORDER_BOTTOM_COLOR).toString()
.equals("rgb(1, 2, 3)") ||
!attributeSet.getAttribute(BORDER_RIGHT_COLOR).toString()
.equals("rgb(1, 2, 4)") ||
!attributeSet.getAttribute(BORDER_LEFT_COLOR).toString()
.equals("rgb(1, 2, 4)") ) {
throw new RuntimeException("Failed");
}
}
private void test() {
View root = ((View)label.getClientProperty(BasicHTML.propertyKey))
.getView(0);
int n = root.getViewCount();
View v = root.getView(n - 1);
AttributeSet attrs = v.getAttributes();
StyleSheet ss = ((HTMLDocument) v.getDocument()).getStyleSheet();
Font font = ss.getFont(attrs);
System.out.println(font.getSize());
passed = (font.getSize() == 12);
if(!passed) {
throw new RuntimeException("Test failed.");
}
}
public void setHtmlText(String htmlText) {
textPane1.setContentType("text/html; charset=utf-8");
HTMLEditorKit kit = new HTMLEditorKit();
textPane1.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("h2{color:#FBC87A;}");
styleSheet.addRule("body{font-family:" + buttonOK.getFont().getName() + ";font-size:" + buttonOK.getFont().getSize() + ";}");
textPane1.setText(htmlText);
textPane1.setCaretPosition(0);
}
public void setHtmlText(String htmlText) {
textPane1.setContentType("text/html; charset=utf-8");
HTMLEditorKit kit = new HTMLEditorKit();
textPane1.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("h2{color:#FBC87A;}");
styleSheet.addRule("body{font-family:" + buttonOK.getFont().getName() + ";font-size:" + buttonOK.getFont().getSize() + ";}");
textPane1.setText(htmlText);
}
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new CustomHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
@Override
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new CustomHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
@Override
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new CustomHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
@Override
public Document createDefaultDocument() {
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
HTMLDocument doc = new CustomHTMLDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}
private void setStyleSheets() {
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-size: " + Driver.getFontSize() + "pt}");
styleSheet.addRule("H1 {color: red; font-size: 120%; font-weight: bold;}");
styleSheet.addRule("code {font-family: courier; font-size: " + Driver.getFontSize() + "pt}");
styleSheet.addRule(" a:link { color: #0000FF; } ");
styleSheet.addRule(" a:visited { color: #800080; } ");
styleSheet.addRule(" a:active { color: #FF0000; text-decoration: underline; } ");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
mainFrame.summaryHtmlArea.setEditorKit(htmlEditorKit);
}
public static void installDefaultStylesheet(StyleSheet css) {
css.addRule("body {font-family:Sans;font-size:12pt;}");
css.addRule("h3 {margin:0; padding:0;margin-top:8px;margin-bottom:3px; }");
// String hcolor =
// Integer.toHexString(SwingTools.DARKEST_BLUE.darker().darker().darker().getRGB());
// hcolor = hcolor.substring(2, 8);
String hcolor = "446699";
css.addRule("h4 {margin-bottom:1px; margin-top:2ex; padding:0; color:#" + hcolor + "; font-size:12px}");
// css.addRule("h2, h3, h4 { border-width:3px; border-style:solid;
// border-color:#"+Integer.toHexString(SwingTools.RAPID_I_ORANGE.getRGB())+"; }");
css.addRule("p {margin-top:0; margin-bottom:2ex; padding:0;}");
// css.addRule("ul {margin-top:0; margin-bottom:1ex; list-style-image:url(" +
// Tools.getResource("icons/help/circle.png") + "); }");
css.addRule("ul.ports {margin-top:0; margin-bottom:1ex; list-style-image:url("
+ Tools.getResource("icons/help/circle.png") + "); }");
css.addRule("ul li {padding-bottom:1ex}");
// css.addRule("li.outPorts {padding-bottom:0px}");
css.addRule("ul.param_dep {margin-top:0; margin-bottom:1ex; list-style-type:none; list-style-image:none; }");
// css.addRule("ul li ul {margin-top:0; margin-bottom:1ex; list-style-type:none;
// list-style-image:none; }");
// css.addRule("ul li small ul {margin-top:0; list-style-type:none; list-style-image:none;
// }");
css.addRule("li ul li {padding-bottom:0}");
// css.addRule("a {text-decoration:none}");
// css.addRule("a:hover {text-decoration:underline}");
css.addRule("dt {font-weight:bold;}");
// css.addRule("a {text-decoration:underline; font-weight:bold;color:blue;}");
css.addRule("hr {color:red; background-color:red}");
css.addRule("a {color:" + SwingTools.getColorHexValue(Colors.LINKBUTTON_REMOTE) + "}");
}