1111
1212import javax .swing .BoxLayout ;
1313import javax .swing .JButton ;
14- import javax .swing .JEditorPane ;
1514import javax .swing .JFrame ;
1615import javax .swing .JPanel ;
1716import javax .swing .JTextArea ;
1817import javax .swing .JTextField ;
1918import javax .swing .JTextPane ;
2019import javax .swing .event .CaretEvent ;
2120import javax .swing .event .CaretListener ;
22- import javax .swing .text .AttributeSet ;
2321import javax .swing .text .Document ;
2422import javax .swing .text .Element ;
2523import javax .swing .text .MutableAttributeSet ;
2927
3028public class Test_Editor extends JFrame {
3129
32-
3330 public Test_Editor () {
34-
31+
3532 String test = "line1\n line2\n line3\n \n line5\n " ;
36-
33+
3734 setTitle ("testing editor" );
3835 setLocation (100 , 100 );
3936 JTextPane editor = new JTextPane ();
4037 System .out .println (editor .getDocument ());
4138 editor .setText (test );
4239 System .out .println ("count = " + editor .getDocument ().getRootElements ()[0 ].getElementCount ());
43- editor .setBackground (new Color (200 ,200 ,200 ));
40+ editor .setBackground (new Color (200 , 200 , 200 ));
4441 editor .setPreferredSize (new Dimension (300 , 300 ));
45- editor .setFont (new Font (Font .SANS_SERIF ,Font .PLAIN , 16 ));
46-
47- Style style = editor .addStyle ("Red" , null );
48- StyleConstants .setForeground (style , Color .red );
49-
42+ editor .setFont (new Font (Font .SANS_SERIF , Font .PLAIN , 16 ));
43+
44+ Style style = editor .addStyle ("Red" , null );
45+ StyleConstants .setForeground (style , Color .red );
46+
5047 editor .addPropertyChangeListener (new PropertyChangeListener () {
5148
5249 @ Override
5350 public void propertyChange (PropertyChangeEvent evt ) {
5451 System .out .println (evt .getPropertyName () + " " + evt );
5552 }
56-
53+
5754 });
58-
55+
5956 editor .addCaretListener (new CaretListener () {
6057
6158 @ Override
6259 public void caretUpdate (CaretEvent e ) {
6360 System .out .println ("JTextPane caret:" + e );
64- //dumpRoot(editor.getDocument());
61+ // dumpRoot(editor.getDocument());
6562 }
66-
63+
6764 });
68-
65+
6966 JTextArea area = new JTextArea ();
70- area .setFont (new Font (Font .MONOSPACED ,Font .PLAIN , 16 ));
67+ area .setFont (new Font (Font .MONOSPACED , Font .PLAIN , 16 ));
7168 area .setText (test );
72- area .setBackground (new Color (200 ,200 ,180 ));
73- area .setPreferredSize (new Dimension (300 ,300 ));
69+ area .setBackground (new Color (200 , 200 , 180 ));
70+ area .setPreferredSize (new Dimension (300 , 300 ));
7471 area .addCaretListener (new CaretListener () {
7572
7673 @ Override
7774 public void caretUpdate (CaretEvent e ) {
7875 System .out .println ("JTextArea caret:" + e );
7976 }
80-
77+
8178 });
82-
79+
8380 JTextField field = new JTextField ("testing" );
8481 field .addCaretListener (new CaretListener () {
8582
8683 @ Override
8784 public void caretUpdate (CaretEvent e ) {
8885 System .out .println ("JTextField caret:" + e );
8986 }
90-
87+
9188 });
9289
9390 add (editor );
9491 add (BorderLayout .EAST , area );
95-
92+
9693 JPanel panel = new JPanel ();
9794 panel .setLayout (new BoxLayout (panel , BoxLayout .X_AXIS ));
9895 JButton b = new JButton ("caret+1" );
@@ -103,7 +100,7 @@ public void actionPerformed(ActionEvent e) {
103100 editor .getCaret ().setDot (editor .getCaret ().getDot () + 1 );
104101 editor .requestFocus ();
105102 }
106-
103+
107104 });
108105 panel .add (b );
109106 b = new JButton ("sel7-10" );
@@ -117,7 +114,7 @@ public void actionPerformed(ActionEvent e) {
117114 editor .setSelectionColor (Color .red );
118115 editor .requestFocus ();
119116 }
120-
117+
121118 });
122119 panel .add (b );
123120 b = new JButton ("sel7-10-area" );
@@ -129,9 +126,9 @@ public void actionPerformed(ActionEvent e) {
129126 area .getCaret ().moveDot (10 );
130127 area .getCaret ().setSelectionVisible (true );
131128 area .setSelectionColor (Color .blue );
132- area .requestFocus ();
129+ area .requestFocus ();
133130 }
134-
131+
135132 });
136133 panel .add (b );
137134
@@ -144,9 +141,9 @@ public void actionPerformed(ActionEvent e) {
144141 field .getCaret ().moveDot (5 );
145142 field .getCaret ().setSelectionVisible (true );
146143 field .setSelectionColor (Color .blue );
147- field .requestFocus ();
144+ field .requestFocus ();
148145 }
149-
146+
150147 });
151148 panel .add (b );
152149
@@ -156,7 +153,7 @@ public void actionPerformed(ActionEvent e) {
156153 @ Override
157154 public void actionPerformed (ActionEvent e ) {
158155 int start = editor .getSelectionStart ();
159- int end = editor .getSelectionEnd ();
156+ int end = editor .getSelectionEnd ();
160157 if (end == start )
161158 return ;
162159 Element ch = editor .getStyledDocument ().getCharacterElement (start );
@@ -166,7 +163,7 @@ public void actionPerformed(ActionEvent e) {
166163 StyleConstants .setBold (attrs , isBold );
167164 editor .getStyledDocument ().setCharacterAttributes (start , end - start , attrs , false );
168165 }
169-
166+
170167 });
171168 panel .add (b );
172169
@@ -176,20 +173,20 @@ public void actionPerformed(ActionEvent e) {
176173 @ Override
177174 public void actionPerformed (ActionEvent e ) {
178175 int start = editor .getSelectionStart ();
179- int end = editor .getSelectionEnd ();
176+ int end = editor .getSelectionEnd ();
180177 if (end == start )
181178 return ;
182179 MutableAttributeSet attrs = new SimpleAttributeSet ();
183180 Element ch = editor .getStyledDocument ().getCharacterElement (start );
184181 StyleConstants .setItalic (attrs , !StyleConstants .isItalic (ch .getAttributes ()));
185182 editor .getStyledDocument ().setCharacterAttributes (start , end - start , attrs , false );
186183 }
187-
184+
188185 });
189186 panel .add (b );
190187
191188 panel .add (field );
192-
189+
193190 add (panel , BorderLayout .SOUTH );
194191 pack ();
195192 setVisible (true );
@@ -202,15 +199,11 @@ protected void dumpRoot(Document document) {
202199 private void dumpElement (int index , Element element ) {
203200 System .out .println ("Test_Editor i=" + index + " e=" + element .toString ());
204201 for (int i = 0 , n = element .getElementCount (); i < n ; i ++)
205- dumpElement ((index + 1 )* 100 , element .getElement (i ));
206-
202+ dumpElement ((index + 1 ) * 100 , element .getElement (i ));
203+
207204 }
208205
209206 public static void main (String [] args ) {
210207 new Test_Editor ();
211208 }
212209}
213-
214-
215-
216-
0 commit comments