Package text field;
Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing. Text .*;
Import java. Io .*;
Public class test {
Jframe myframe;
Jtextpane mytextpane;
Public test (){
Myframe = new jframe ("jtextpane ");
Mytextpane = new jtextpane ();
}
/**
* Insert text
*
* @ Param Str
* String
* @ Param attrset
* String description
*/
Public void insert (string STR, attributeset attrset ){
Document Doc = mytextpane. getdocument ();
STR = "\ n" + STR;
Try {
Doc. insertstring (Doc. getlength (), STR, attrset );
} Catch (badlocationexception e ){
System. Out. println ("badlocationexception:" + E );
}
}
/**
* Set the text style
*
* @ Param Str
* String
* @ Param col
* Color
* @ Param bold
* Bold or not. True indicates rough.
* @ Param fontsize
* Font size
*/
Public void setdocs (string STR, color Col, Boolean bold, int fontsize ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, col );
// Color
If (bold = true ){
Styleconstants. setbold (attrset, true );
} // Font type
Styleconstants. setfontsize (attrset, fontsize );
// Font size
Insert (STR, attrset );
}
Public void Init (){
Setdocs ("I love you! ", Color. Black, false, 20 );
Setdocs ("You love me! ", Color. Blue, true, 35 );
Setdocs ("dead", color. Red, false, 28 );
Myframe. Add (mytextpane, borderlayout. center );
Myframe. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
Myframe. setsize (250,360 );
Myframe. setvisible (true );
}
Public static void main (string [] ARGs ){
Test test = new test ();
Test. INIT ();
}
}