<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Swing


    天行健 君子以自強不息

    posts - 69, comments - 215, trackbacks - 0, articles - 16
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Swing對JTextPane中字體顏色的設置

    Posted on 2007-07-09 08:50 zht 閱讀(10545) 評論(4)  編輯  收藏 所屬分類: Swing
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.WindowConstants;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.DefaultStyledDocument;
    import javax.swing.text.Document;
    import javax.swing.text.EditorKit;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.StyledEditorKit;

    public class NewJFrame extends javax.swing.JFrame implements ActionListener {
     private JPanel jp1;

     private JButton color;

     private JTextPane jep;

     private JScrollPane jsp;

     private JButton font;

     /**
      * Auto-generated main method to display this JFrame
      */
     public static void main(String[] args) {
      NewJFrame inst = new NewJFrame();
      inst.setVisible(true);
     }

     public NewJFrame() {
      super();
      initGUI();
     }

     private void initGUI() {
      try {
       BorderLayout thisLayout = new BorderLayout();
       getContentPane().setLayout(thisLayout);
       setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
       {
        jp1 = new JPanel();
        getContentPane().add(jp1, BorderLayout.NORTH);
        {
         font = new JButton();
         font.addActionListener(this);
         jp1.add(font);
         font.setText("font");
        }
        {
         color = new JButton();
         jp1.add(color);
         color.addActionListener(this);
         color.setText("color");
        }
       }
       {
        jsp = new JScrollPane();
        getContentPane().add(jsp, BorderLayout.CENTER);
        {
         jep = new JTextPane();
         jsp.setViewportView(jep);
         jep.setDocument(new DefaultStyledDocument());
        }
       }
       pack();
       setSize(400, 300);
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

     public static void setFontSize(JEditorPane editor, int size) {
      if (editor != null) {
       if ((size > 0) && (size < 512)) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setFontSize(attr, size);
        setCharacterAttributes(editor, attr, false);
       } else {
        UIManager.getLookAndFeel().provideErrorFeedback(editor);
       }
      }
     }

     public static void setForeground(JEditorPane editor, Color fg) {
      if (editor != null) {
       if (fg != null) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, fg);
        setCharacterAttributes(editor, attr, false);
       } else {
        UIManager.getLookAndFeel().provideErrorFeedback(editor);
       }
      }
     }

     public static final void setCharacterAttributes(JEditorPane editor,
       AttributeSet attr, boolean replace) {
      int p0 = editor.getSelectionStart();
      int p1 = editor.getSelectionEnd();
      if (p0 != p1) {
       StyledDocument doc = getStyledDocument(editor);
       doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
      }
      StyledEditorKit k = getStyledEditorKit(editor);
      MutableAttributeSet inputAttributes = k.getInputAttributes();
      if (replace) {
       inputAttributes.removeAttributes(inputAttributes);
      }
      inputAttributes.addAttributes(attr);
     }

     protected static final StyledDocument getStyledDocument(JEditorPane e) {
      Document d = e.getDocument();
      if (d instanceof StyledDocument) {
       return (StyledDocument) d;
      }
      throw new IllegalArgumentException("document must be StyledDocument");
     }

     protected static final StyledEditorKit getStyledEditorKit(JEditorPane e) {
      EditorKit k = e.getEditorKit();
      if (k instanceof StyledEditorKit) {
       return (StyledEditorKit) k;
      }
      throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
     }

     public void actionPerformed(ActionEvent e) {
      Object obj = e.getSource();
      if (obj == font) {
       JEditorPane editor = jep;
       setFontSize(editor, 20);
      }
      if (obj == color) {
       JEditorPane editor = jep;
       setForeground(editor, Color.red);
      }
     }

    }
    其他操作如下:
    1、對字體的操作
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attr, family);
        setCharacterAttributes(editor, attr, false);
    family為字體
    2、對字體大小的操作
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setFontSize(attr, size);
        setCharacterAttributes(editor, attr, false);
    size為字號
    3、是否是粗體的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
       MutableAttributeSet attr = kit.getInputAttributes();
       boolean bold = (StyleConstants.isBold(attr)) ? false : true;
       SimpleAttributeSet sas = new SimpleAttributeSet();
       StyleConstants.setBold(sas, bold);
       setCharacterAttributes(editor, sas, false);
    4、是否是斜體的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
       MutableAttributeSet attr = kit.getInputAttributes();
       boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
       SimpleAttributeSet sas = new SimpleAttributeSet();
       StyleConstants.setItalic(sas, italic);
       setCharacterAttributes(editor, sas, false);
    5、是否有下劃線的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
       MutableAttributeSet attr = kit.getInputAttributes();
       boolean underline = (StyleConstants.isUnderline(attr)) ? false
         : true;
       SimpleAttributeSet sas = new SimpleAttributeSet();
       StyleConstants.setUnderline(sas, underline);
       setCharacterAttributes(editor, sas, false);
    6、左中右對齊的處理
    MutableAttributeSet attr = new SimpleAttributeSet();
       StyleConstants.setAlignment(attr, a);
       setParagraphAttributes(editor, attr, false);
    public static final void setParagraphAttributes(JEditorPane editor,
       AttributeSet attr, boolean replace) {
      int p0 = editor.getSelectionStart();
      int p1 = editor.getSelectionEnd();
      StyledDocument doc = getStyledDocument(editor);
      doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
     }
    a:0:左,1:中,2:右

    7、文本字體顏色的設置
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, fg);
        setCharacterAttributes(editor, attr, false);
    fg:為color
    8、文本背景顏色的設置
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setBackground(attr, bg);
        setCharacterAttributes(editor, attr, false);
    主站蜘蛛池模板: 无码一区二区三区免费视频 | 亚洲国产精品综合久久网络| 亚洲第一成年网站大全亚洲| 久久黄色免费网站| 亚洲av中文无码乱人伦在线播放 | 在线观看亚洲免费| 国产精品免费视频网站| 亚洲AV无码成人精品区狼人影院| 啦啦啦www免费视频| 亚洲AV成人片无码网站| 免费在线观看亚洲| fc2免费人成为视频| 亚洲AV无码乱码在线观看富二代| 亚洲免费观看视频| 亚洲视频在线观看视频| 免费看污成人午夜网站| 亚洲AV日韩AV无码污污网站| 免费一级特黄特色大片在线| 国产精品99爱免费视频| 亚洲avav天堂av在线不卡| 精品国产污污免费网站aⅴ| 亚洲人成77777在线播放网站不卡 亚洲人成77777在线观看网 | 久久精品国产亚洲5555| 日韩电影免费在线观看网站| 亚洲精品线在线观看| 皇色在线视频免费网站| 国产亚洲美女精品久久久久| 狠狠亚洲狠狠欧洲2019| 真实国产乱子伦精品免费| 亚洲色偷偷综合亚洲av78| 亚洲人成影院在线无码观看| 香蕉免费一区二区三区| 亚洲中文无码亚洲人成影院| 国产av无码专区亚洲国产精品| 久久99热精品免费观看牛牛| 亚洲人AV在线无码影院观看| 亚洲综合小说另类图片动图 | 国产亚洲精品a在线观看| 4399影视免费观看高清直播| 日韩色视频一区二区三区亚洲| 国产亚洲人成无码网在线观看|