javax.swing.text.StyledDocument类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.3k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中javax.swing.text.StyledDocument类的一些代码示例,展示了StyledDocument类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StyledDocument类的具体详情如下:
包路径:javax.swing.text.StyledDocument
类名称:StyledDocument

StyledDocument介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

  1. public class Main {
  2. public static void main(String[] args) {
  3. JTextPane textPane = new JTextPane();
  4. StyledDocument doc = textPane.getStyledDocument();
  5. Style style = textPane.addStyle("I'm a Style", null);
  6. StyleConstants.setForeground(style, Color.red);
  7. try { doc.insertString(doc.getLength(), "BLAH ",style); }
  8. catch (BadLocationException e){}
  9. StyleConstants.setForeground(style, Color.blue);
  10. try { doc.insertString(doc.getLength(), "BLEH",style); }
  11. catch (BadLocationException e){}
  12. JFrame frame = new JFrame("Test");
  13. frame.getContentPane().add(textPane);
  14. frame.pack();
  15. frame.setVisible(true);
  16. }
  17. }

代码示例来源:origin: groovy/groovy-core

  1. public void actionPerformed(ActionEvent ae) {
  2. JTextComponent tComp = (JTextComponent) ae.getSource();
  3. if (tComp.getDocument() instanceof StyledDocument) {
  4. doc = (StyledDocument) tComp.getDocument();
  5. try {
  6. doc.getText(0, doc.getLength(), segment);
  7. }
  8. catch (Exception e) {
  9. // should NEVER reach here
  10. e.printStackTrace();
  11. }
  12. int offset = tComp.getCaretPosition();
  13. int index = findTabLocation(offset);
  14. buffer.delete(0, buffer.length());
  15. buffer.append('\n');
  16. if (index > -1) {
  17. for (int i = 0; i < index + 4; i++) {
  18. buffer.append(' ');
  19. }
  20. }
  21. try {
  22. doc.insertString(offset, buffer.toString(),
  23. doc.getDefaultRootElement().getAttributes());
  24. }
  25. catch (BadLocationException ble) {
  26. ble.printStackTrace();
  27. }
  28. }
  29. }

代码示例来源:origin: groovy/groovy-core

  1. protected void addStylesToDocument(JTextPane outputArea) {
  2. StyledDocument doc = outputArea.getStyledDocument();
  3. Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  4. Style regular = doc.addStyle("regular", def);
  5. StyleConstants.setFontFamily(def, "Monospaced");
  6. promptStyle = doc.addStyle("prompt", regular);
  7. StyleConstants.setForeground(promptStyle, Color.BLUE);
  8. commandStyle = doc.addStyle("command", regular);
  9. StyleConstants.setForeground(commandStyle, Color.MAGENTA);
  10. outputStyle = doc.addStyle("output", regular);
  11. StyleConstants.setBold(outputStyle, true);
  12. }

代码示例来源:origin: stackoverflow.com

  1. StyledDocument doc = textPane.getStyledDocument();
  2. SimpleAttributeSet center = new SimpleAttributeSet();
  3. StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
  4. doc.setParagraphAttributes(0, doc.getLength(), center, false);

代码示例来源:origin: stackoverflow.com

  1. JTextPane textPane = new JTextPane();
  2. textPane.setText( "original text" );
  3. StyledDocument doc = textPane.getStyledDocument();
  4. // Define a keyword attribute
  5. SimpleAttributeSet keyWord = new SimpleAttributeSet();
  6. StyleConstants.setForeground(keyWord, Color.RED);
  7. StyleConstants.setBackground(keyWord, Color.YELLOW);
  8. StyleConstants.setBold(keyWord, true);
  9. // Add some text
  10. try
  11. {
  12. doc.insertString(0, "Start of text\n", null );
  13. doc.insertString(doc.getLength(), "\nEnd of text", keyWord );
  14. }
  15. catch(Exception e) { System.out.println(e); }

代码示例来源:origin: RipMeApp/ripme

  1. /**
  2. * Write a line to the Log section of the GUI
  3. *
  4. * @param text the string to log
  5. * @param color the color of the line
  6. */
  7. private void appendLog(final String text, final Color color) {
  8. SimpleAttributeSet sas = new SimpleAttributeSet();
  9. StyleConstants.setForeground(sas, color);
  10. StyledDocument sd = logText.getStyledDocument();
  11. try {
  12. synchronized (this) {
  13. sd.insertString(sd.getLength(), text + "\n", sas);
  14. }
  15. } catch (BadLocationException e) { }
  16. logText.setCaretPosition(sd.getLength());
  17. }

代码示例来源:origin: stackoverflow.com

  1. JFrame frame = new JFrame(TestTextPane.class.getSimpleName());
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. final JTextPane textPane2 = new JTextPane();
  4. textPane2.setText("12345");
  5. frame.add(textPane2);
  6. JButton button = new JButton("Make it red");
  7. button.addActionListener(new ActionListener() {
  8. final SimpleAttributeSet set = new SimpleAttributeSet();
  9. StyleConstants.setForeground(set, Color.RED);
  10. int p0 = textPane2.getSelectionStart();
  11. int p1 = textPane2.getSelectionEnd();
  12. if (p0 != p1) {
  13. StyledDocument doc = textPane2.getStyledDocument();
  14. doc.setCharacterAttributes(p0, p1 - p0, set, false);
  15. public void run() {
  16. textPane2.getCaret().setDot(textPane2.getText().length());
  17. MutableAttributeSet inputAttributes = textPane2.getInputAttributes();
  18. inputAttributes.addAttributes(set);
  19. frame.add(button, BorderLayout.SOUTH);
  20. frame.setSize(600, 400);
  21. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. JFrame f = new JFrame();
  2. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. JTextPane jtp = new JTextPane();
  4. StyledDocument doc = (StyledDocument) jtp.getDocument();
  5. SimpleAttributeSet normal = new SimpleAttributeSet();
  6. StyleConstants.setFontFamily(normal, "Serif");
  7. StyleConstants.setFontSize(normal, 72);
  8. StyleConstants.setForeground(normal, Color.blue);
  9. doc.insertString(doc.getLength(), "Test", normal);
  10. jtp.setSelectionStart(doc.getLength());
  11. jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));
  12. jtp.setSelectionStart(doc.getLength());
  13. jtp.insertComponent(new JLabel("Label"));
  14. jtp.setSelectionStart(doc.getLength());
  15. f.add(jtp);
  16. f.pack();
  17. f.setLocationRelativeTo(null);

代码示例来源:origin: stackoverflow.com

  1. JFrame frame = new JFrame();
  2. JPanel panel = new JPanel();
  3. JTextPane textPane = new JTextPane();
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. panel.setLayout(new BorderLayout());
  6. panel.setPreferredSize(new Dimension(200, 200));
  7. panel.add(textPane, BorderLayout.CENTER);
  8. panel.add(button, BorderLayout.SOUTH);
  9. textPane.addStyle("negra", null);
  10. StyledDocument doc = textPane.getStyledDocument();
  11. int start = textPane.getSelectionStart();
  12. int end = textPane.getSelectionEnd();
  13. if (StyleConstants.isBold(style)) {
  14. StyleConstants.setBold(style, false);
  15. } else {
  16. StyleConstants.setBold(style, true);
  17. doc.setCharacterAttributes(start, end - start, style, false);
  18. frame.add(panel);
  19. frame.pack();
  20. frame.setLocationRelativeTo(null);
  21. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. import javax.swing.JFrame;
  2. import javax.swing.JTextPane;
  3. import javax.swing.WindowConstants;
  4. import javax.swing.text.Style;
  5. import javax.swing.text.StyleConstants;
  6. public class BoldSelected {
  7. public static void main(final String[] args) {
  8. new BoldSelected().launchGui();
  9. }
  10. private void launchGui() {
  11. final String title = "Set bold font style for selected text in JTextArea instance";
  12. final JFrame frame = new JFrame("Stack Overflow: " + title);
  13. frame.setBounds(100, 100, 800, 600);
  14. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  15. final JTextPane textPane = new JTextPane();
  16. textPane.setText(title + ".");
  17. final Style style = textPane.addStyle("Bold", null);
  18. StyleConstants.setBold(style, true);
  19. textPane.getStyledDocument().setCharacterAttributes(4, 33, style, false);
  20. frame.getContentPane().add(textPane);
  21. frame.setVisible(true);
  22. }
  23. }

代码示例来源:origin: stackoverflow.com

  1. @Override
  2. public void run() {
  3. JFrame frame = new JFrame("Colored Text");
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. textPane.setText("Different Colored Text");
  6. for (int i = 0; i < textPane.getDocument().getLength(); i++) {
  7. SimpleAttributeSet set = new SimpleAttributeSet();
  8. StyleConstants.setForeground(set,
  9. new Color(random.nextInt(256), random.nextInt(256),
  10. StyleConstants.setFontSize(set, random.nextInt(12) + 12);
  11. StyleConstants.setBold(set, random.nextBoolean());
  12. doc.setCharacterAttributes(i, 1, set, true);
  13. frame.add(new JScrollPane(textPane));
  14. frame.pack();
  15. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. JTextPane textPane = new JTextPane(doc);
  4. textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has "
  5. + "been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of "
  6. SimpleAttributeSet set = new SimpleAttributeSet();
  7. StyleConstants.setForeground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
  8. StyleConstants.setFontSize(set, random.nextInt(12) + 12);
  9. StyleConstants.setBold(set, random.nextBoolean());
  10. StyleConstants.setItalic(set, random.nextBoolean());
  11. StyleConstants.setUnderline(set, random.nextBoolean());
  12. doc.setCharacterAttributes(i, 1, set, true);
  13. frame.add(new JScrollPane(textPane));
  14. frame.setSize(500, 400);
  15. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. public void run() {
  2. JFrame frame = new JFrame("Test");
  3. frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  4. JTextPane jta = new JTextPane();
  5. SimpleAttributeSet sas = new SimpleAttributeSet();
  6. StyleConstants.setBackground(sas, Color.RED);
  7. StyledDocument doc = jta.getStyledDocument();
  8. doc.setCharacterAttributes(wordsStartPos[i], words[i].length(), sas, false);
  9. frame.add(jta);
  10. frame.pack();
  11. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. public void run() {
  2. JFrame frame = initgui();
  3. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  4. frame.pack();
  5. frame.setVisible(true);
  6. JFrame frame = new JFrame("Test");
  7. JPanel panel = new JPanel();
  8. StyledDocument doc = (StyledDocument) new DefaultStyledDocument();
  9. JTextPane textpane = new JTextPane(doc);
  10. textpane.setText("Test");
  11. javax.swing.text.Style style = textpane.addStyle("Red", null);
  12. StyleConstants.setForeground(style, Color.RED);
  13. doc.setCharacterAttributes(0, 1, textpane.getStyle("Red"), true);
  14. panel.add(textpane);
  15. frame.add(panel);
  16. return frame;

代码示例来源:origin: stackoverflow.com

  1. private JFrame frame = new JFrame();
  2. private JTextPane jtp;
  3. private StyledDocument doc;
  4. jtp = new JTextPane();
  5. jtp.setText("\ntype some text in the above empty line and check the wrapping behavior");
  6. doc = jtp.getStyledDocument();
  7. doc.addDocumentListener(new DocumentListener() {
  8. Style defaultStyle = jtp.getStyle(StyleContext.DEFAULT_STYLE);
  9. doc.setCharacterAttributes(0, doc.getLength(), defaultStyle, false);
  10. JScrollPane scroll = new JScrollPane(jtp);
  11. scroll.setPreferredSize(new Dimension(200, 200));
  12. frame.add(scroll);
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. frame.pack();
  15. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. "one two three four five\r\n";
  2. JTextPane textPane = new JTextPane();
  3. textPane.setText(text);
  4. JScrollPane scrollPane = new JScrollPane( textPane );
  5. getContentPane().add( scrollPane );
  6. StyledDocument doc = textPane.getStyledDocument();
  7. SimpleAttributeSet keyWord = new SimpleAttributeSet();
  8. StyleConstants.setBackground(keyWord, Color.CYAN);
  9. doc.setCharacterAttributes(offset, search.length(), keyWord, false);
  10. offset += search.length();
  11. throws Exception
  12. JFrame.setDefaultLookAndFeelDecorated(true);
  13. JFrame frame = new TextAndNewLinesTest();
  14. frame.setTitle("Text and New Lines");
  15. frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
  16. frame.setSize(400, 120);
  17. frame.setLocationRelativeTo( null );
  18. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. extends JFrame {
  2. JTextPane p = new JTextPane();
  3. public Smiley() throws Exception {
  4. p.setEditorKit(new StyledEditorKit());
  5. getContentPane().add(p, BorderLayout.CENTER);
  6. SimpleAttributeSet attrs = new SimpleAttributeSet();
  7. StyleConstants.setIcon(attrs, getImage());
  8. p.addCaretListener(new CaretListener() {
  9. public void caretUpdate(CaretEvent e) {
  10. int start = 0;
  11. while (index > -1) {
  12. Element el = doc.getCharacterElement(index);
  13. if (StyleConstants.getIcon(el.getAttributes()) == null) {
  14. doc.remove(index, 2);
  15. SimpleAttributeSet attrs = new SimpleAttributeSet();
  16. StyleConstants.setIcon(attrs, getImage());
  17. doc.insertString(index, ":)", attrs);
  18. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. this.setSize(400, 400);
  20. test11.show();

代码示例来源:origin: stackoverflow.com

  1. final JTextPane textPane = new JTextPane();
  2. StyledDocument doc = textPane.getStyledDocument();
  3. final MutableAttributeSet standard = new SimpleAttributeSet();
  4. StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
  5. doc.setParagraphAttributes(0, 0, standard, true);
  6. MutableAttributeSet keyWord = new SimpleAttributeSet();
  7. StyleConstants.setForeground(keyWord, Color.red);
  8. StyleConstants.setItalic(keyWord, true);
  9. textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n");
  10. doc.setCharacterAttributes(0, 3, keyWord, false);
  11. doc.setCharacterAttributes(19, 4, keyWord, false);
  12. try {
  13. doc.insertString(0, "Start of text\n", null);
  14. } catch (Exception e) {
  15. StyledDocument doc = textPane.getStyledDocument();
  16. doc.setCharacterAttributes(index, 4, selWord, false);
  17. add(toggleButton, BorderLayout.SOUTH);
  18. public void run() {
  19. TextPaneAttributes frame = new TextPaneAttributes();
  20. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  21. frame.pack();
  22. frame.setVisible(true);

代码示例来源:origin: stackoverflow.com

  1. frame.setSize(300, 300);
  2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  3. frame.setLocationRelativeTo(null);
  4. frame.setVisible(true);
  5. private JTextPane textPane = new JTextPane();
  6. this.add(textPane, BorderLayout.CENTER);
  7. JPanel panel = new JPanel();
  8. panel.add(btnStyle);
  9. this.add(panel, BorderLayout.NORTH);
  10. btnStyle.addActionListener(new ActionListener() {
  11. @Override
  12. Element element = doc.getCharacterElement(start);
  13. AttributeSet as = element.getAttributes();
  14. MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes());
  15. StyleConstants.setBold(asNew, !StyleConstants.isBold(as));
  16. doc.setCharacterAttributes(start, textPane.getSelectedText().length(), asNew, true);
  17. String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold");
  18. btnStyle.setText(text);
  19. boolean isBold = StyleConstants.isBold(as) ? false : true;

代码示例来源:origin: stackoverflow.com

  1. setSize(800, 600);
  2. initUI();
  3. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  4. textpane = new JTextPane();
  5. textpane.setPreferredSize(new Dimension(700, 600));
  6. Style style = document.addStyle("StyleName", null);
  7. StyleConstants.setIcon(style, new ImageIcon(image));
  8. document.insertString(document.getLength(), "ignored text", style);
  9. } catch (Exception e){
  10. panel.add(textpane);
  11. add(panel);
  12. pack();
  13. public void run() {
  14. MathNotesPlus app = new MathNotesPlus();
  15. app.setVisible(true);

相关文章