Swing's settings for font color in Jtextpane

Source: Internet
Author: User
Tags final int size

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);


  }


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.