1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 16-Jun-2006
* Time: 08:00:59
* To change this template use File | Settings | File Templates.
*/
public class Test extends JFrame {
public Test() throws HeadlessException {
JTextPane textPane = new JTextPane();
StyleSheet styles = new StyleSheet();
// if you want to add some of you own style in the code do it like this.
// styles.addRule("h1 {color:blue; font-size:25; }");
URL stylesURL = getClass().getClassLoader().getResource("styles.css");
FileReader f = null;
try {
f = new FileReader(stylesURL.getFile());
// load your styles from the style-sheet.
styles.loadRules(f, stylesURL);
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
// print all styles found. just in case you want to be sure it works :-)
Enumeration enumeration = styles.getStyleNames();
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
HTMLEditorKit editor = (HTMLEditorKit) textPane.getEditorKitForContentType("text/html");
editor.setStyleSheet(styles);
textPane.setEditorKit(editor);
// load a page
try {
textPane.setPage(getClass().getClassLoader().getResource("index.html"));
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
add(new JScrollPane(textPane));
}
public static void main(String[] args) {
Runnable runner = new Runnable(){
public void run() {
Test t = new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setPreferredSize(new Dimension(400,300));
t.pack();
t.setLocationRelativeTo(null);
t.setVisible(true);
}
};
SwingUtilities.invokeLater(runner);
}
} |