Extend JtextPane so that it has a keyword automatic brightness.

zhaozj2021-02-11  185

Import java.util.StringTokenizer;

Import java.awt. *; import java.awt.event. *;

Import javax.swing. *; import javax.swing.text. *; import javax.swing.event. *; import javax.swing.text.rtf.rtfeditorkit;

Public Class MyTextPane Extends JtextPane {

protected StyleContext m_context; protected DefaultStyledDocument m_doc; private MutableAttributeSet keyAttr, normalAttr; private MutableAttributeSet inputAttributes = new RTFEditorKit () getInputAttributes ().;

Private string [] Keyword = {"SELECT", "from", "where"}

Public mytextpane () {super (); m_context = new styleText (); m_doc = new defaultstyleDDocument; this.setDocument (m_doc);

THIS.AddKeylistener (new keyadapter () {public void keyreleased (keyevent ke) {syntaxpival ();}});

File: // Defines Keyword Display Properties Keyattr = New SimpleAttributeSet (); StyleConstants.SetForeground (Keyattr, Color.Blue);

File: // Define General Text Display Attribute Normalttr = New SimpleAttributeSet (); StyleConstants.SetForeground (Normalattr, Color.Black);}

Public void syntaxpival () {Try {string s = null; element root = m_doc.getdefault (); file: // Take the cursor current line int curusorpos = this.getCaretPosition (); file: // Current cursor location int line = Root.getElementIndex (CURSORPOS); File: // Take the current line

Element Para = root.get.Gtelement (line); int start = para.getStartoffset (); int end = para.GETENDOFFSET () - 1; file: // Delete / R Character s = m_doc.gettext (start, end - start) .touppercase ();

INT i = 0; int XStart = 0;

File: // Analysis Keyword --- m_doc.setcharacTRIBUTES (Start, S.Length (), Normalattr, False; MyStringTokenizer ST = New MyStringTokenizer (s); while (st.hasmoretoKens ()) {s = st.nextToken (); If (s == null) return; for (i = 0; i = keyword.Length ); xstart = st.getCurrPosition ();

File: // Set keyword display attribute m_doc.setcharacTRibutes (Start XStart, S.Length (), keyattr, false;} infutattributes.addattributes (Normalattr);} catch (exception ex) {ex.printstackTrace ();} }

public static void main (String [] args) {JFrame frame = new JFrame ( "test text pane"); frame.getContentPane () add (new MyTextPane ());. WindowListener wndCloser = new WindowAdapter () {public void windowClosing ( WindowEvent e) {System.exit (0);}}; frame.addWindowListener (wndCloser); final int inset = 50; Dimension screenSize = Toolkit.getDefaultToolkit () getScreenSize ();. frame.setBounds (inset, inset, screenSize. Width - INSET * 2, Screensize.Height - INSET * 2); frame.show ();}}

/ * While parsing of the string, where the recording position of each token ** / class MyStringTokenizer extends StringTokenizer {String sval = ""; String oldStr, str; int m_currPosition = 0, m_beginPosition = 0; MyStringTokenizer (String str) { Super (str, "); this.oldstr = Str; this.str = Str;}

Public string nextToken () {Try {string s = super.nextToken (); int POS = -1; if (OldStr.Equals (s)) {Return S;}

POS = str.indexof (s sval); if (pOS == -1) {POS = str.indexof (sval s); if (pOS == -1) Return Null; Else POS = 1;}

INT Xbegin = POS S.LENGTH (); str = str.substring (xbegin);

m_currposition = m_beginposition pos; m_beginposition = m_beginposition xbegin; return s;} catch (java.util.nosuchelementexception ex) {EX.PrintStackTrace (); return null;}}

File: // Returns the location of Token in the string public int getCurrPosition () {return m_curposition;}}

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

New Post(0)