Features of converting to jtextpane:

Source: Internet
Author: User
Features of jtextpane:
I believe that everyone has used word to write reports or articles, so you will surely know that we can make many changes to the text in the articles in Word. These changes are
Is the "attribute" change of the text. Because the effects produced in jtextpane are almost all caused by changes in attributes, the Class components that change attributes are less in jtextpane.
No. Therefore, before introducing how to construct jtextpane, we should first introduce two attribute classes that are often used in jtextpane:
Simpleattributeset and styleconstant.
Attribute changes are originally handled using attributeset interface, but this interface contains too many methods.
Attributeset interface needs to implement all the methods in this interface, which is not an ideal practice for programming; Java also provides
Simpleattributeset class implements attributeset interface. Therefore, as long as we directly use simpleattributeset class, we can have attributeset
Instead of writing attributeset methods one by one. In addition, the styleconstant class provides many common buteset classes.
Attribute key and method to set or obtain the status of jtextpane content. The styleconstant class contains many common attribute settings, including
And border blank area settings, text font/size/type settings, background color settings, and so on. Using these two classes to help design jtextpane makes jtextpane richer
.
Jtextpane is a component designed for text and layout processing. Jtextpane's design concept for the content of the input area is similar to the word design concept, that is, in
The text structure in jtextpane has the concept of paragraph. The concept of "paragraph" is to use the [enter] key as the demarcation point of each segment, and each time you press the [enter] key, a segment is added.
. Do you remember the element storage mode we mentioned in jtextarea? Jtextpane uses the same method, but the difference is that the storage planning method is different.
In jtextarea, there are no paragraphs, but the [enter] key is used as the demarcation between the two elements. In jtextpane, the root section is the entire editing area.
Point. Each section is a branch node, and each character is a leaf node to store files. Because jtextpane stores data in this way, jtextpane can also
Different paragraphs are set to different attributes like Word files. For example, the first section is italic, the font size is 14 characters, the second section is italic, And the font color is
Blue, 2 cm to the left border, and so on. In addition, we can also set the distance between the input text and each boundary in the jtextpane editing area. From these features
Jtextpane is a component with many practical functions.

9-6-2: Construct the jtextpane component:
After learning about the features of jtextpane, let's take a look at what jtextpane can do. In the following example, we will
Set the color, bold italic, and bottom line of the text in the area.

Import javax. Swing .*;
Import javax. Swing. Text .*;
Import java. AWT. event .*;
Import java. AWT .*;

Public class jtextpane1 {
Private jtextpane textpane;

Public jtextpane1 (){
Textpane = new jtextpane ();
Textpane. setbackground (color. Black );
Textpane. seteditable (false );
}
Public void setyellow_bold_20 (string Str ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, color. Yellow );
Styleconstants. setbold (attrset, true );
Insert (STR, attrset );
}
Public void setblue_italic_bold_22 (string Str ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, color. Blue );
Styleconstants. setitalic (attrset, true );
Styleconstants. setfontsize (attrset, 24 );
Insert (STR, attrset );
}
Public void setred_underline_italic_24 (string Str ){
Simpleattributeset attrset = new simpleattributeset ();
Styleconstants. setforeground (attrset, color. Red );
Styleconstants. setunderline (attrset, true );
Styleconstants. setitalic (attrset, true );
Styleconstants. setfontsize (attrset, 24 );
Insert (STR, attrset );
}
// The main purpose of this method is to insert a string into jtextpane.
Public void insert (string STR, attributeset attrset ){
Document docs = textpane. getdocument (); // use the getdocument () method to obtain jtextpane's document instance.0
STR = STR + "/N ";
Try {
Docs. insertstring (Docs. getlength (), STR, attrset );
} Catch (badlocationexception BLE ){
System. Out. println ("badlocationexception:" + BLE );
}
}
Public component getcomponent (){
Return textpane;
}
Public static void main (string [] ARGs ){
Jtextpane1 pane = new jtextpane1 ();
Pane. setyellow_bold_20 ("this is line 1, yellow, bold, size 20 ");
Pane. setblue_italic_bold_22 ("this is line 2, Blue, italic, bold, size 22 ");
Pane. setred_underline_italic_24 ("this is line 3, Red, underline, italic, size 24 ");

Jframe F = new jframe ("jtextpane1 ");
F. getcontentpane (). Add (pane. getcomponent ());
F. setsize (450,180 );
F. Show ();
F. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
}

If you want to place images or other components (such as tables or buttons) on jtextpane, you can use inseticon () and insertcomponent () provided by jtextpane respectively ()
Method To achieve this effect.
As for the construction of another jtextpane, the difference is that jtextarea adopts Document Interface while jtextpane adopts

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.