Jtextpane adds text with different attributes

Source: Internet
Author: User

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

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.