Htmleditorkittest

Solo disponible en BuenasTareas
  • Páginas : 2 (274 palabras )
  • Descarga(s) : 0
  • Publicado : 10 de noviembre de 2011
Leer documento completo
Vista previa del texto
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
importjavax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

/**
* A complete Java class that demonstrates how to create an HTML viewer withstyles,
* using the JEditorPane, HTMLEditorKit, StyleSheet, and JFrame.
*
* @author alvin alexander, devdaily.com.
*
*/
public class HtmlEditorKitTest
{
public static void main(String[]args)
{
new HtmlEditorKitTest();
}

public HtmlEditorKitTest()
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// create jeditorpaneJEditorPane jEditorPane = new JEditorPane();

// make it read-only
jEditorPane.setEditable(false);

// create a scrollpane; modify its attributes as desiredJScrollPane scrollPane = new JScrollPane(jEditorPane);

// add an html editor kit
HTMLEditorKit kit = new HTMLEditorKit();
jEditorPane.setEditorKit(kit);

//add some styles to the html
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; }");
styleSheet.addRule("h1{color: blue;}");
styleSheet.addRule("h2 {color: #ff0000;}");
styleSheet.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");

// create somesimple html as a string
String htmlString = "\n"
+ "\n"
+ "Welcome!\n"
+ "This is an H2 header\n"+ "This is some sample text\n"
+ "devdaily blog\n"
+ "\n";

// create a document, set it on the jeditorpane, then add the...
tracking img