import javax.swing.*; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyVetoException; import java.beans.VetoableChangeListener; public class AppTest extends JFrame { public AppTest() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); getContentPane().setLayout(new BorderLayout()); final JTextPane comp = new JTextPane(); final StyledDocument doc = comp.getStyledDocument(); Style style = comp.addStyle("superscript", null); StyleConstants.setSuperscript(style, true); StyleConstants.setBackground(style, Color.YELLOW); try { doc.insertString(doc.getLength(), "hello", style); } catch(Exception e) {} Style style3 = comp.addStyle("subscript", null); StyleConstants.setBackground(style3, Color.YELLOW); StyleConstants.setSubscript(style3, true); try { doc.insertString(doc.getLength(), "goodbye", style3); } catch(Exception e) {} Style style4 = comp.addStyle("normal", null); StyleConstants.setBackground(style4, Color.YELLOW); try { doc.insertString(doc.getLength(), "world", style4); } catch(Exception e) {} comp.setDocument(doc); getContentPane().add(comp, BorderLayout.CENTER); } public static void main(String[] args) throws Exception { JFrame frame = new AppTest(); frame.setSize(600,600); frame.setVisible(true); } }