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