### RESPUESTA A LA PREGUNTA 56710 - JAVA ### ### http://www.lawebdelprogramador.com ### import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.net.URL; import java.util.Hashtable; import java.awt.Color; import javax.swing.*; import javax.swing.text.*; public class Ejemplo extends JFrame{ DefaultStyledDocument doc; StyleContext styles; Hashtable runAttr; JScrollPane rotador; JTextPane area; Ejemplo(){ doc=new DefaultStyledDocument(); styles=StyleContext.getDefaultStyleContext(); runAttr = new Hashtable(); cargarDocumentos(); rotador=new JScrollPane(); area=new JTextPane(doc); rotador.setViewportView(area); add(rotador); setSize(400,500); setVisible(true); } static void main(String args[]){ new Ejemplo(); } void cargarDocumentos() { crearEstilos(); for (int i = 0; i < data.length; i++) { Parrafo p = data[i]; agregarParrafo(p); } } void agregarParrafo(Parrafo p) { try { Style s = null; for (int i = 0; i < p.data.length; i++) { Run run = p.data[i]; s = (Style) runAttr.get(run.attr); doc.insertString(doc.getLength(), run.content, s); } Style ls = styles.getStyle(p.logical); doc.setLogicalStyle(doc.getLength() - 1, ls); doc.insertString(doc.getLength(), " ", null); } catch (BadLocationException e) { System.err.println("Internal error: " + e); } } void crearEstilos() { Style s = styles.addStyle(null, null); runAttr.put("none", s); s = styles.addStyle(null, null); StyleConstants.setItalic(s, true); StyleConstants.setForeground(s, new Color(153,153,102)); runAttr.put("cquote", s); s = styles.addStyle(null, null); StyleConstants.setItalic(s, true); StyleConstants.setForeground(s, new Color(51,102,153)); runAttr.put("aquote", s); Style def = styles.getStyle(StyleContext.DEFAULT_STYLE); Style heading = styles.addStyle("heading", def); StyleConstants.setFontFamily(heading, "SansSerif"); StyleConstants.setBold(heading, true); StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER); StyleConstants.setSpaceAbove(heading, 10); StyleConstants.setSpaceBelow(heading, 10); StyleConstants.setFontSize(heading, 18); // Title Style sty = styles.addStyle("title", heading); StyleConstants.setFontSize(sty, 32); // edition sty = styles.addStyle("edition", heading); StyleConstants.setFontSize(sty, 16); // author sty = styles.addStyle("author", heading); StyleConstants.setItalic(sty, true); StyleConstants.setSpaceBelow(sty, 25); // subtitle sty = styles.addStyle("subtitle", heading); StyleConstants.setSpaceBelow(sty, 35); // normal sty = styles.addStyle("normal", def); StyleConstants.setLeftIndent(sty, 10); StyleConstants.setRightIndent(sty, 10); StyleConstants.setFontFamily(sty, "SansSerif"); StyleConstants.setFontSize(sty, 14); StyleConstants.setSpaceAbove(sty, 4); StyleConstants.setSpaceBelow(sty, 4); } static class Parrafo { Parrafo(String logical, Run[] data) { this.logical = logical; this.data = data; } String logical; Run[] data; } static class Run { Run(String attr, String content) { this.attr = attr; this.content = content; } String attr; String content; } Parrafo[] data = new Parrafo[] { new Parrafo("title", new Run[] { new Run("none", "ALICE'S ADVENTURES IN WONDERLAND") }), new Parrafo("author", new Run[] { new Run("none", "Lewis Carroll") }), new Parrafo("edition", new Run[] { new Run("none", "THE MILLENNIUM FULCRUM EDITION 3.0") }), new Parrafo("heading", new Run[] { new Run("none", "CHAPTER V") }), new Parrafo("subtitle", new Run[] { new Run("none", "Advice from a Caterpillar") }), new Parrafo("normal", new Run[] { new Run("none", " "), }), new Parrafo("normal", new Run[] { new Run("none", "The Caterpillar and Alice looked at each other for some time in silence: at last the Caterpillar took the hookah out of its mouth, and addressed her in a languid, sleepy voice.") }), new Parrafo("normal", new Run[] { new Run("cquote", "Who are YOU? "), new Run("none", "said the Caterpillar.") }), new Parrafo("normal", new Run[] { new Run("none", "This was not an encouraging opening for a conversation. Alice replied, rather shyly, "), new Run("aquote", "I--I hardly know, sir, just at present--at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then. "), }), new Parrafo("normal", new Run[] { new Run("cquote", "What do you mean by that? "), new Run("none", " said the Caterpillar sternly. "), new Run("cquote", "Explain yourself!"), }), new Parrafo("normal", new Run[] { new Run("aquote", "I can't explain MYSELF, I'm afraid, sir"), new Run("none", " said Alice, "), new Run("aquote", "because I'm not myself, you see."), }), new Parrafo("normal", new Run[] { new Run("cquote", "I don't see,"), new Run("none", " said the Caterpillar."), }), new Parrafo("normal", new Run[] { new Run("aquote", "I'm afraid I can't put it more clearly, "), new Run("none", "Alice replied very politely, "), new Run("aquote", "for I can't understand it myself to begin with; and being so many different sizes in a day is very confusing."), }), new Parrafo("normal", new Run[] { new Run("cquote", "It isn't, "), new Run("none", "said the Caterpillar.") }), new Parrafo("normal", new Run[] { new Run("aquote", "Well, perhaps you haven't found it so yet,"), new Run("none", " said Alice; "), new Run("aquote", "but when you have to turn into a chrysalis--you will some day, you know--and then after that into a butterfly, I should think you'll feel it a little queer, won't you?") }), new Parrafo("normal", new Run[] { new Run("cquote", "Not a bit, "), new Run("none", "said the Caterpillar.") }), new Parrafo("normal", new Run[] { new Run("aquote", "Well, perhaps your feelings may be different,"), new Run("none", " said Alice; "), new Run("aquote", "all I know is, it would feel very queer to ME."), }), new Parrafo("normal", new Run[] { new Run("cquote", "You!"), new Run("none", " said the Caterpillar contemptuously. "), new Run("cquote", "Who are YOU?"), }), new Parrafo("normal", new Run[] { new Run("normal", "Which brought them back again to the beginning of the conversation. Alice felt a little irritated at the Caterpillar's making such VERY short remarks, and she drew herself up and said, very gravely, "), new Run("aquote", "I think, you ought to tell me who YOU are, first."), }), new Parrafo("normal", new Run[] { new Run("cquote", "Why? "), new Run("none", "said the Caterpillar."), }), new Parrafo("normal", new Run[] { new Run("none", " "), }), new Parrafo("normal", new Run[] { new Run("none", " "), }), new Parrafo("normal", new Run[] { new Run("none", " "), }) }; } ### Roger MP masinga10@gmail.com ###