Transfer JTextPane characteristics:

xiaoxiao2021-03-06  23

JtextPane features:

I believe that everyone has used Word to write reports or articles, then you will know that we can do a lot of changes in the article in the article in Word, these changes are

"Attribute" that belongs to the text. Since the effects generated in jtextpane are almost all changed by the attribute, the class component that changes the property is less in jtextpane.

Can't. Therefore, before describing how to construct JTextPane, we must first introduce two in JtextPane to usually use attribute classes:

SimpleAttributeSet and STYLECONSTANT.

The change in attributes originally used attributeset interface to handle, but this Interface contains too many methods, if we directly active

AttributeSet Interface needs to do all the methods in this Interface, which is not an ideal approach to writing programs; Java additional

The SimpleAttributeSet class has implemented AttributeSet Interface. So, as long as we use the SimpleAttributeSet class directly to include AttributeSet

All functions of Interface instead of writing the method in AttributeSet. The STYLECONSTANT class is to provide many commonly used belongings at the AttributeSet class.

ATTRIBUTE Key and method set or get the status of JtextPane content. In the StyleConstant class, there is a lot of common property settings, including this article.

Setting with boundary blank section, text font / size / type setting, background color setting, etc. Use these two classes to assist in design JtextPane makes JtextPanes more rich.

Content change.

JtextPane is a component designed for text and layout. JTextPane's design concept for entering area content is a design concept similar to Word, that is,

The text structure in JtextPane is a paragraph concept. The concept of "paragraph" is to increase a segment for each [Enter] button.

drop. Remember the Element storage mode that we mentioned in Jtextarea? In JtextPane is also the same approach, but the difference is to plan storage.

There is no segmentation in JtextArea, just a purely stored into two ELEMENTs with the [ENTER] key. And in JtextPane is the root festival throughout the editorial area

Point, each paragraph is branches, each character as a leaf node to store files. Also because JtextPane is stored in this way, there is also a JTEXTPANE.

To set each paragraph into different properties like a Word file, such as the first paragraph, the font size is 14 characters, bold words, the second paragraph is behind, the font color is

Blue, regenerate 2 cm to the left boundary, etc .; additional, we can also set the distance between the jTextPane editing area input to each boundary. From these functions, for one

TextComponent for JtextPane is a component with considerable multi-purpose features.

9-6-2: Construct the JTextPane component:

After understanding the features of JTextPane, we now come to see what kind of effect can present, in this example, we will be jthxtpane

The text in the area sets colors, bold tops, and bottom lines, etc. related properties.

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; TextPane.setedITable (False);

}

Public void setYellow_bold_20 (String Str) {

SimpleAttributeSet Attrse = New SimpleAttributeSet ();

StyleConstants.SetForeground (Attrset, Color.Yellow);

StyleConstants.setBold (attrse, true);

INSERT (STR (STR, Attrset);

}

Public void setblue_italic_bold_22 (String Str) {

SimpleAttributeSet Attrse = New SimpleAttributeSet ();

StyleConstants.SetForeground (Attrse, Color.Blue);

StyleConstants.Setitalic (attrset, true);

StyleConstants.SetFontSize (AttRset, 24);

INSERT (STR (STR, Attrset);

}

Public void setred_underline_italic_24 (String Str) {

SimpleAttributeSet Attrse = New SimpleAttributeSet ();

StyleConstants.SetForeground (Attrse, Color.Red);

StyleConstants.Setunderline (Attrse, true);

StyleConstants.Setitalic (attrset, true);

StyleConstants.SetFontSize (AttRset, 24);

INSERT (STR (STR, Attrset);

}

// The most important use of this method is to insert strings into JtextPane.

Public void insert (String Str, AttributeSet Attrse) {

Document Docs = TextPane.getDocument (); // Use the getDocument () method to get the Document Instance.0 for JtextPane

Str = STR "/ N";

Try {

DOCS.INSERTSTRING (DOCS.GETLENGTH (), STR, Attrset;

} catch (BadLocationException Ble) {

System.out.println ("BadLocationExce:" 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) {. {PUBLIC VOID WINDOWCLOSING

System.exit (0);

}

});

}

}

If you want to set a graphic or other component (such as a table or button) on JtextPane, you can use the INSETINETICON () and InsertComponent () provided by JtextPane, respectively.

The method is to achieve this effect.

As for another JtextPane's constructor, like JTextarea, the difference is that jtextAREA is using Document Interface and jtextPane is adopted.

转载请注明原文地址:https://www.9cbs.com/read-56384.html

New Post(0)