下面列出了javax.swing.text.html.HTMLEditorKit#setStyleSheet ( ) 实例代码,或者点击链接到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"));
}
/** 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 ();
}
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);
}
private MainPanel() {
super(new BorderLayout());
Font font = makeFont(getClass().getResource("Burnstown Dam.ttf")).orElseGet(this::getFont);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
JPanel p = new JPanel(new GridLayout(0, 1));
JLabel label = new JLabel();
label.setFont(font.deriveFont(Font.PLAIN, 24));
label.setText("1: setFont(font.deriveFont(Font.PLAIN, 24))");
p.add(label);
JLabel label2 = new JLabel();
label2.setText("<html><font size='+3' face='Burnstown Dam'>2: html, font, size,+3</font></html>");
p.add(label2);
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-size: 24pt; font-family: Burnstown Dam;}");
// styleSheet.addRule(".highlight {color: red; background: green; font-family: Burnstown Dam.ttf; }");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
JEditorPane editor = new JEditorPane();
editor.setEditorKit(htmlEditorKit);
editor.setText(makeTestHtml());
JEditorPane editor2 = new JEditorPane();
editor2.setFont(font);
editor2.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editor2.setText("4: putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE)");
JPanel p2 = new JPanel(new GridLayout(0, 1));
p2.add(new JScrollPane(editor));
p2.add(new JScrollPane(editor2));
add(p, BorderLayout.NORTH);
add(p2);
setPreferredSize(new Dimension(320, 240));
}
private MainPanel() {
super(new BorderLayout());
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-size: 12pt;}");
styleSheet.addRule(".highlight {color: red; background: green}");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
// HTMLDocument htmlDocument = (HTMLDocument) htmlEditorKit.createDefaultDocument();
JEditorPane editor = new JEditorPane();
editor.setEditorKit(htmlEditorKit);
// editor.setDocument(htmlDocument);
editor.setText(makeTestHtml());
add(new JScrollPane(editor));
setPreferredSize(new Dimension(320, 240));
}
private MainPanel() {
super(new BorderLayout());
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule(".str {color:#008800}");
styleSheet.addRule(".kwd {color:#000088}");
styleSheet.addRule(".com {color:#880000}");
styleSheet.addRule(".typ {color:#660066}");
styleSheet.addRule(".lit {color:#006666}");
styleSheet.addRule(".pun {color:#666600}");
styleSheet.addRule(".pln {color:#000000}");
styleSheet.addRule(".tag {color:#000088}");
styleSheet.addRule(".atn {color:#660066}");
styleSheet.addRule(".atv {color:#008800}");
styleSheet.addRule(".dec {color:#660066}");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
editor.setEditorKit(htmlEditorKit);
editor.setEditable(false);
JButton button = new JButton("open");
button.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
int ret = fileChooser.showOpenDialog(getRootPane());
if (ret == JFileChooser.APPROVE_OPTION) {
loadFile(fileChooser.getSelectedFile().getAbsolutePath());
}
});
Box box = Box.createHorizontalBox();
box.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
box.add(Box.createHorizontalGlue());
box.add(button);
add(new JScrollPane(editor));
add(box, BorderLayout.SOUTH);
setPreferredSize(new Dimension(320, 240));
}
public static void configureHtmlKitStylesheet() {
if (ourDefaultHtmlKitCss != null) {
return;
}
// save the default JRE CSS and ..
HTMLEditorKit kit = new HTMLEditorKit();
ourDefaultHtmlKitCss = kit.getStyleSheet();
// .. erase global ref to this CSS so no one can alter it
kit.setStyleSheet(null);
// Applied to all JLabel instances, including subclasses. Supported in JBR only.
UIManager.getDefaults().put("javax.swing.JLabel.userStyleSheet", JBHtmlEditorKit.createStyleSheet());
}
public DetailsPanel() {
initComponents2();
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);
}
setEditorKit(htmlkit);
addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hlevt) {
if (EventType.ACTIVATED == hlevt.getEventType()) {
if (hlevt.getURL () != null) {
Utilities.showURL(hlevt.getURL());
}
}
}
});
setEditable(false);
setPreferredSize(new Dimension(300, 80));
RP.post(new Runnable() {
@Override
public void run() {
getAccessibleContext ().setAccessibleName (
NbBundle.getMessage (DetailsPanel.class, "ACN_DetailsPanel")); // NOI18N
}
});
putClientProperty( JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE );
}
/**
* Prepares the dockable and its elements.
*/
public OperatorDocumentationBrowser() {
dockKey.putProperty(ResourceDockKey.PROPERTY_KEY_DEFAULT_FALLBACK_LOCATION, RelativeDockablePosition.BOTTOM_RIGHT);
setLayout(new BorderLayout());
contentGbc = new GridBagConstraints();
contentPanel = new JPanel(new GridBagLayout()) {
@Override
public Dimension getPreferredSize() {
return new Dimension(getParent().getWidth(), super.getPreferredSize().height);
}
};
editor = new JEditorPane("text/html", "<html>-</html>") {
@Override
public Dimension getPreferredSize() {
return new Dimension(getParent().getWidth(), super.getPreferredSize().height);
}
};
// Instantiate Editor and set Settings
editor.addHyperlinkListener(new OperatorHelpLinkListener());
editor.setEditable(false);
HTMLEditorKit hed = new HTMLEditorKit();
hed.setStyleSheet(createStyleSheet(hed.getStyleSheet()));
editor.setEditorKit(hed);
editor.setBackground(Colors.PANEL_BACKGROUND);
editor.setContentType("text/html");
// add editor to scrollPane
JScrollPane scrollPane = new ExtendedJScrollPane(contentPanel);
scrollPane.setBorder(null);
scrollPane.setMinimumSize(MINIMUM_DOCUMENTATION_SIZE);
scrollPane.setPreferredSize(MINIMUM_DOCUMENTATION_SIZE);
contentGbc.gridx = 0;
contentGbc.gridy = 0;
contentGbc.weightx = 1.0f;
contentGbc.fill = GridBagConstraints.HORIZONTAL;
contentPanel.add(editor, contentGbc);
// add filler at bottom
contentGbc.gridy += 1;
contentGbc.weighty = 1.0f;
contentGbc.fill = GridBagConstraints.BOTH;
contentPanel.add(new JLabel(), contentGbc);
// prepare contentGbc for feedback form
contentGbc.gridy += 1;
contentGbc.weighty = 0.0f;
contentGbc.fill = GridBagConstraints.HORIZONTAL;
// add scrollPane to Dockable
this.add(scrollPane, BorderLayout.CENTER);
this.setVisible(true);
this.validate();
documentationUpdateQueue.start();
}
private MainPanel() {
super(new BorderLayout(5, 5));
// UIManager.put("TextPane.caretForeground", Color.ORANGE);
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule("body {font-size: 12pt}");
styleSheet.addRule(".highlight {color: red; background: green}");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
JEditorPane editor = new JEditorPane();
// editor.setEditable(false);
editor.setEditorKit(htmlEditorKit);
editor.setText(makeTestHtml());
editor.setCaretColor(null);
JTextField field = new JTextField("JTextField");
field.setBackground(Color.GRAY);
field.setForeground(Color.WHITE);
JRadioButton r1 = new JRadioButton("RED");
r1.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
field.setCaretColor(Color.RED);
// editor.setCaretColor(Color.RED);
}
});
JRadioButton r2 = new JRadioButton("null");
r2.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
field.setCaretColor(null);
// editor.setCaretColor(null);
}
});
JRadioButton r3 = new JRadioButton("Lnf default", true);
r3.addItemListener(e -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
Color c = UIManager.getLookAndFeelDefaults().getColor("TextField.caretForeground");
field.setCaretColor(c);
// c = UIManager.getLookAndFeelDefaults().getColor("TextPane.caretForeground");
// System.out.println(c);
// editor.setCaretColor(c);
}
});
ButtonGroup bg = new ButtonGroup();
Box box = Box.createHorizontalBox();
Stream.of(r1, r2, r3).forEach(rb -> {
bg.add(rb);
box.add(rb);
box.add(Box.createHorizontalStrut(2));
});
box.add(field);
UIManager.put("TextArea.caretForeground", Color.ORANGE);
JTextArea area = new JTextArea("TextArea.caretForeground: ORANGE");
// area.setBackground(Color.GREEN);
// area.setFont(area.getFont().deriveFont(15.0f));
// area.setCaretColor(Color.RED);
JPanel p = new JPanel(new GridLayout(2, 1, 2, 2));
p.add(new JScrollPane(area));
p.add(new JScrollPane(editor));
setBorder(BorderFactory.createTitledBorder("JTextComponent#setCaretColor(...)"));
add(box, BorderLayout.NORTH);
add(p);
setPreferredSize(new Dimension(320, 240));
}