textpaneattributes.java

来自「这是一个汉诺塔程序」· Java 代码 · 共 65 行

JAVA
65
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class TextPaneAttributes extends JFrame {
        public TextPaneAttributes() {
                JTextPane textPane = new JTextPane();
                StyledDocument doc = textPane.getStyledDocument();

                //  Set alignment to be centered for all paragraphs
                MutableAttributeSet standard = new SimpleAttributeSet();
                StyleConstants.setAlignment(standard, StyleConstants.ALIGN_CENTER);
                doc.setParagraphAttributes(0, 0, standard, true);

                //  Define a keyword attribute
                HTML.Tag []tags=HTML.getAllTags();
                HTML.Attribute[] atrs=HTML.getAllAttributeKeys();
                MutableAttributeSet keyWord = new SimpleAttributeSet();
                MutableAttributeSet tagWord = new SimpleAttributeSet();
                
                keyWord.addAttribute(tags[0].toString().toLowerCase(),tags[0].toString());
                
                StyleConstants.setForeground(keyWord, Color.red);
                StyleConstants.setForeground(tagWord, Color.BLUE);
                StyleConstants.setItalic(keyWord, true);
                StyleConstants.setBold(tagWord,true);
                
                //  Add initial text
                textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nbody\nbgcolor\n");

                //  Highlight some keywords
                doc.setCharacterAttributes(0, 3, keyWord, false);
                doc.setCharacterAttributes(19, 4, keyWord, false);

                //  Add some text
                try {
                        doc.insertString(0, "Start of text\n", null);
                        doc.insertString(doc.getLength(), "End of text\n", keyWord);
                     
                } catch (Exception e) {
                }

                //  Add text pane to frame
                JScrollPane scrollPane = new JScrollPane(textPane);
                scrollPane.setPreferredSize(new Dimension(200, 200));
                getContentPane().add(scrollPane);

                //  Add a bold button
                JButton button = new JButton("bold");
                getContentPane().add(button, BorderLayout.SOUTH);
                button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                new StyledEditorKit.BoldAction().actionPerformed(null);
                        }
                });
        }

        public static void main(String[] args) {
                TextPaneAttributes frame = new TextPaneAttributes();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
        }
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?