1.使ç¨javaå¼å代ç ç¼è¾å¨(使ç¨javaå¼å代ç ç¼è¾å¨ç好å¤)
使ç¨javaå¼å代ç ç¼è¾å¨(使ç¨javaå¼å代ç ç¼è¾å¨ç好å¤)
javaç¼ç¨å¸¸ç¨ç软件æåªäº
æ³è¦å好Javaå¼åï¼æ²¡æ常ç¨çå¼åå·¥å ·å¯ä¸è¡ãèJavaåå¦è å¾å¾å¤äºå¯¹Javaè¿ä¸ç¥å解çç¶æï¼å¯¹äºJavaå¼åå·¥å ·çäºè§£ä¹ä¸ä¼å¤ªå¤ï¼å¸¸å¸¸ä¼ä¸ºäºæ¾å¼åå·¥å ·è浪费大éçæ¶é´ãç»å¤§å®¶ä»ç»ä¸Javaå¼åå·¥ç¨å¸å¸¸ç¨çå¼åå·¥å ·æåªäºã
第ä¸ä¸ªJava常ç¨çå¼åå·¥å ·ä¸ºJDKï¼ä¹å«Java软件å¼åå·¥å ·å ï¼æ¯ä¸ä¸ªç¼åJavaçAppletå°ç¨åºååºç¨ç¨åºçç¨åºå¼åç¯å¢ï¼JDKä¸è¿å æ¬äºJavaçå¼åç¯å¢ï¼æ以ä¸è½½äºJDKå°±å å«äºJavaå¼åç¯å¢ãèä¸JDKä¹æ以被å«åJava软件å¼åå·¥å ·å ï¼æ¯å 为å®å å«äºå ¶å®Javaå¼åå·¥å ·ï¼å¦ï¼ç¼è¯å·¥å ·(javac.exe)ãæå å·¥å ·(jar.exe)çã
第äºä¸ªæ¯JavaIDEï¼Eclipseï¼Eclipseæ¯ä¸ä¸ªå¼æ¾æºä»£ç çãåºäºJavaçå¯æ©å±å¼åå¹³å°ãå°±å ¶æ¬èº«èè¨ï¼å®åªæ¯ä¸ä¸ªæ¡æ¶åä¸ç»æå¡ï¼ç¨äºéè¿æ件ç»ä»¶æ建å¼åç¯å¢ãèä¸ç±äºå®çå¹³çåä¸è´æ§ï¼ä½¿å¾å ¶å®å¼åè¯è¨ä¹å¯ä»¥å¨eclipseä¸ä½¿ç¨ã
第ä¸ä¸ªæ¯notepad++ï¼notepad++æ¯ä¸ä¸ªç¼è¯å·¥å ·ï¼å®çä¼ç¹æ¯å³ä½¿ä½ ææ件å æäºï¼å¦æånotepad++ä¸æå¼è¿ï¼é£ä¹é£éå°±ä¼ä¿çï¼æ以对ä¸äºå®¹æ误å ç人æ¥è¯´æ¯è¾å好ï¼notepad++å¯ä»¥ç¨æ¥æ¿ä»£notepadã
第å个æ¯VisualStudioCodeï¼å®æ¯ä¸ä¸ªè¿è¡äºMacOSXãWindowsåLinuxä¹ä¸ç跨平å°æºä»£ç ç¼è¾å¨ãèä¸è¿ä¸ªç¼è¾å¨æ¯æå¤ç§è¯è¨åæ ¼å¼çç¼åã
以ä¸å°±æ¯æ¯è¾å¸¸ç¨ç4ç§Javaå¼åå·¥å ·ï¼Javaçåå¦è å¨å¯¹å¼åå·¥å ·ä¸äºè§£çæ åµä¸ï¼å¯ä»¥æ ¹æ®ä¸è¿°ä»ç»æ¥éæ©éåèªå·±çå¼åå·¥å ·ï¼è¿æ ·å¯ä»¥åå°èªå·±å°å¤æ¾å¼åå·¥å ·è¿å¯è½æ¾å°ä¸éåçæ浪费çæ¶é´ã
å¦ä½ç¨javaç¼åä¸ä¸ªç®åçææ¬ç¼è¾å¨ï¼
importjava.awt.*;\x0d\importjava.awt.event.*;\x0d\importjavax.swing.*;\x0d\importjava.io.*;\x0d\\x0d\publicclassf1extendsFrameimplementsActionListener\x0d\{ \x0d\privateMenuBarmenubar=newMenuBar();\x0d\privateMenufilemenu=newMenu("æ件");\x0d\privateMenueditmenu=newMenu("ç¼è¾");\x0d\privateMenuformmenu=newMenu("æ ¼å¼");\x0d\privateMenuItem[]itemf=newMenuItem[4];\x0d\privateMenuItem[]iteme=newMenuItem[6];\x0d\privateMenuItem[]items=newMenuItem[2];\x0d\privateTextAreatf=newTextArea();\x0d\\x0d\publicinta=0,源码变色rsi源码b=0,c=0,style=Font.PLAIN,size=;\x0d\publicStrings1="red:"+a+""+"green:"+b+""+"blue"+c,\x0d\s2="å®ä½";\x0d\\x0d\publicString[]sz1={ "","","","","",""},\x0d\sz2={ "å®ä½","é»ä½","å¹¼å","é¶ä¹¦","è¡æ¥·","Arial","Georgia"},\x0d\sz3={ "ç²ä½","å¾æ","常è§","ç²æ"};\x0d\\x0d\JDialogdialog=newJDialog(this,"åä½",true);\x0d\Containercp=dialog.getContentPane();\x0d\JLabel[]lb=newJLabel[8];\x0d\JLabellb1=newJLabel(s1,JLabel.LEFT);\x0d\JButtonb1=newJButton("ç¡®å®"),\x0d\b2=newJButton("åæ¶");\x0d\JComboBoxjc1=newJComboBox(),\x0d\jc2=newJComboBox(),\x0d\jc3=newJComboBox();\x0d\JScrollBarjb1=newJScrollBar(JScrollBar.HORIZONTAL,,5,0,);\x0d\JScrollBarjb2=newJScrollBar(JScrollBar.HORIZONTAL,,5,0,);\x0d\JScrollBarjb3=newJScrollBar(JScrollBar.HORIZONTAL,,5,0,);
javaæºä»£ç ç¼è¾å¨è®¾è®¡ç¨äºç¼åJavaæºä»£ç çç¼è¾å¨ï¼åºæ¬è¦æ±ï¼å¯ä»¥å®ææºç¨åºçæ件æå¼ï¼ç¼è¾åæ件ä¿åä¸.é«äº®çå 容:
éè¦é«äº®çå 容æ:
1.å ³é®å,å¦public,int,trueç.
2.è¿ç®ç¬¦,å¦+,-,*,/ç
3.æ°å
4.é«äº®å符串,å¦"exampleofstring"
5.é«äº®åè¡æ³¨é
6.é«äº®å¤è¡æ³¨é
äº.å®ç°é«äº®çæ ¸å¿æ¹æ³:
StyledDocument.setCharacterAttributes(intoffset,intlength,AttributeSets,booleanreplace)
ä¸.ææ¬ç¼è¾å¨éæ©.
Javaä¸æä¾çå¤è¡ææ¬ç¼è¾å¨æ:JTextComponent,JTextArea,JTextPane,JEditorPaneç,é½å¯ä»¥ä½¿ç¨.ä½æ¯å 为è¯æ³çè²ä¸ææ¬è¦ä½¿ç¨å¤ç§é£æ ¼çæ ·å¼,æ以è¿äºææ¬ç¼è¾å¨çdocumentè¦ä½¿ç¨StyledDocument.
JTextArea使ç¨çæ¯PlainDocument,æ¤documentä¸è½è¿è¡å¤ç§æ ¼å¼ççè².
JTextPane,JEditorPane使ç¨çæ¯StyledDocument,é»è®¤å°±å¯ä»¥ä½¿ç¨.
为äºå®ç°è¯æ³çè²,å¯ä»¥ç»§æ¿èªDefaultStyledDocument,è®¾ç½®å ¶ä¸ºè¿äºææ¬ç¼è¾å¨çdocumet,æè ä¹å¯ä»¥ç´æ¥ä½¿ç¨JTextPane,JEditorPaneæ¥å.为äºæ¹ä¾¿,è¿éå°±ç´æ¥ä½¿ç¨JTextPaneäº.
å.ä½æ¶è¿è¡çè².
å½ææ¬ç¼è¾å¨ä¸æå符被æå ¥æè å é¤æ¶,ææ¬çå 容就åçäºåå,è¿æ¶æ£æ¥,è¿è¡çè².
为äºçè§å°ææ¬çå 容åçäºåå,è¦ç»documentæ·»å ä¸ä¸ªDocumentListenerçå¬å¨,å¨ä»çremoveUpdateåinsertUpdateä¸è¿è¡çè²å¤ç.
èchangedUpdateæ¹æ³å¨ææ¬çå±æ§ä¾å¦åæ¯è²,èæ¯è²,åä½çé£æ ¼æ¹åæ¶æä¼è¢«è°ç¨.
@Override
publicvoidchangedUpdate(DocumentEvente){
}
@Override
publicvoidinsertUpdate(DocumentEvente){
try{
colouring((StyledDocument)e.getDocument(),e.getOffset(),e.getLength());
}catch(BadLocationExceptione1){
e1.printStackTrace();
}
}
@Override
publicvoidremoveUpdate(DocumentEvente){
try{
//å 为å é¤åå æ ç´§æ¥çå½±åçåè¯ä¸¤è¾¹,æ以é¿åº¦å°±ä¸éè¦äº
colouring((StyledDocument)e.getDocument(),e.getOffset(),0);
}catch(BadLocationExceptione1){
e1.printStackTrace();
}
}
äº.çè²èå´:
pos:æåååå æ çä½ç½®.
len:æååçå符æ°.
ä¾å¦æå ³é®åpublic,int
åè¯"publicint",å¨"public"å"int"ä¸æå ¥ä¸ä¸ªç©ºæ ¼ååæ"publicint",ä¸ä¸ªåè¯åæäºä¸¤ä¸ª,è¿æ¶å¯¹"public"å"int"è¿è¡çè².
çè²èå´æ¯publicä¸pçä½ç½®åintä¸tçä½ç½®å 1,å³æ¯posåé¢åè¯å¼å§çä¸æ åpos+lenå¼å§åè¯ç»æçä¸æ .æ以ä¸ä¾ä¸è¦çè²çèå´æ¯"publicint".
æä¾äºæ¹æ³indexOfWordStartæ¥åå¾posååè¯å¼å§çä¸æ ,æ¹æ³indexOfWordEndæ¥åå¾posååè¯ç»æçä¸æ .
publicintindexOfWordStart(Documentdoc,intpos)throwsBadLocationException{
//ä»poså¼å§ååæ¾å°ç¬¬ä¸ä¸ªéåè¯å符.
for(;pos0isWordCharacter(doc,pos-1);--pos);
returnpos;
}
publicintindexOfWordEnd(Documentdoc,intpos)throwsBadLocationException{
//ä»poså¼å§ååæ¾å°ç¬¬ä¸ä¸ªéåè¯å符.
for(;isWordCharacter(doc,pos);++pos);
returnpos;
}
ä¸ä¸ªå符æ¯åè¯çææå符:æ¯åæ¯,æ°å,ä¸å线.
publicbooleanisWordCharacter(Documentdoc,intpos)throwsBadLocationException{
charch=getCharAt(doc,pos);//åå¾å¨ææ¡£ä¸posä½ç½®å¤çå符
if(Character.isLetter(ch)||Character.isDigit(ch)||ch=='_'){ returntrue;}
returnfalse;
}
æ以çè²çèå´æ¯[start,end]:
intstart=indexOfWordStart(doc,pos);
intend=indexOfWordEnd(doc,pos+len);
å .å ³é®åçè².
ä»çè²èå´çå¼å§ä¸æ èµ·è¿è¡å¤æ,å¦ææ¯ä»¥åæ¯å¼æè ä¸å线å¼å¤´,å说ææ¯åè¯,é£ä¹å åå¾è¿ä¸ªåè¯,å¦æè¿ä¸ªåè¯æ¯å ³é®å,å°±è¿è¡å ³é®åçè²,å¦æä¸æ¯,å°±è¿è¡æ®éççè².çè²å®è¿ä¸ªåè¯å,继ç»åé¢ççè²å¤ç.å·²ç»çè²è¿çå符,å°±ä¸åè¿è¡çè²äº.
publicvoidcolouring(StyledDocumentdoc,intpos,intlen)throwsBadLocationException{
//åå¾æå ¥æè å é¤åå½±åå°çåè¯.
//ä¾å¦"public"å¨båæå ¥ä¸ä¸ªç©ºæ ¼,å°±åæäº:"public",è¿æ¶å°±æ两个åè¯è¦å¤ç:"pub"å"lic"
//è¿æ¶è¦åå¾çèå´æ¯pubä¸påé¢çä½ç½®ålicä¸cåé¢çä½ç½®
intstart=indexOfWordStart(doc,pos);
intend=indexOfWordEnd(doc,pos+len);
charch;
while(startend){
ch=getCharAt(doc,start);
if(Character.isLetter(ch)||ch=='_'){
//å¦ææ¯ä»¥åæ¯æè ä¸å线å¼å¤´,说ææ¯åè¯
//pos为å¤çåçæåä¸ä¸ªä¸æ
start=colouringWord(doc,start);
}else{
//SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,normalStyle));
++start;
}
}
}
publicintcolouringWord(StyledDocumentdoc,intpos)throwsBadLocationException{
intwordEnd=indexOfWordEnd(doc,pos);
Stringword=doc.getText(pos,wordEnd-pos);//è¦è¿è¡çè²çåè¯
if(keywords.contains(word)){
//å¦ææ¯å ³é®å,å°±è¿è¡å ³é®åççè²,å¦å使ç¨æ®éççè².
//è¿éæä¸ç¹è¦æ³¨æ,å¨insertUpdateåremoveUpdateçæ¹æ³è°ç¨çè¿ç¨ä¸,ä¸è½ä¿®æ¹docçå±æ§.
//ä½æ们åè¦è¾¾å°è½å¤ä¿®æ¹docçå±æ§,æ以ææ¤ä»»å¡æ¾å°è¿ä¸ªæ¹æ³çå¤é¢å»æ§è¡.
//å®ç°è¿ä¸ç®ç,å¯ä»¥ä½¿ç¨æ°çº¿ç¨,ä½æ¾å°swingçäºä»¶éåéå»å¤çæ´è½»ä¾¿ä¸ç¹.
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,keywordStyle));
}else{
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,normalStyle));
}
returnwordEnd;
}
å 为å¨insertUpdateåremoveUpdateæ¹æ³ä¸ä¸è½ä¿®æ¹documentçå±æ§,æ以çè²çä»»å¡æ¾å°è¿ä¸¤ä¸ªæ¹æ³å¤é¢,æ以使ç¨äºSwingUtilities.invokeLateræ¥å®ç°.
privateclassColouringTaskimplementsRunnable{
privateStyledDocumentdoc;
privateStylestyle;
privateintpos;
privateintlen;
publicColouringTask(StyledDocumentdoc,intpos,intlen,Stylestyle){
this.doc=doc;
this.pos=pos;
this.len=len;
this.style=style;
}
publicvoidrun(){
try{
//è¿éå°±æ¯å¯¹å符è¿è¡çè²
doc.setCharacterAttributes(pos,len,style,true);
}catch(Exceptione){ }
}
}
ä¸:æºç
å ³é®åçè²çå®æ代ç å¦ä¸,å¯ä»¥ç´æ¥ç¼è¯è¿è¡.对äºæ°å,è¿ç®ç¬¦,å符串çççè²å¤çå¨ä»¥åçæç¨ä¸ä¼ç»§ç»è¿è¡è¯¦è§£.
importjava.awt.Color;
importjava.util.HashSet;
importjava.util.Set;
importjavax.swing.JFrame;
importjavax.swing.JTextPane;
importjavax.swing.SwingUtilities;
importjavax.swing.event.DocumentEvent;
importjavax.swing.event.DocumentListener;
importjavax.swing.text.BadLocationException;
importjavax.swing.text.Document;
importjavax.swing.text.Style;
importjavax.swing.text.StyleConstants;
importjavax.swing.text.StyledDocument;
publicclassHighlightKeywordsDemo{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
JTextPaneeditor=newJTextPane();
editor.getDocument().addDocumentListener(newSyntaxHighlighter(editor));
frame.getContentPane().add(editor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(,);
frame.setVisible(true);
}
}
/
***å½ææ¬è¾å ¥åºçæå符æå ¥æè å é¤æ¶,è¿è¡é«äº®.
*
*è¦è¿è¡è¯æ³é«äº®,ææ¬è¾å ¥ç»ä»¶çdocumentè¦æ¯styleddocumentæè¡.æ以ä¸è¦ç¨JTextArea.å¯ä»¥ä½¿ç¨JTextPane.
*
*@authorBiao
*
*/
classSyntaxHighlighterimplementsDocumentListener{
privateSetStringkeywords;
privateStylekeywordStyle;
privateStylenormalStyle;
publicSyntaxHighlighter(JTextPaneeditor){
//åå¤çè²ä½¿ç¨çæ ·å¼
keywordStyle=((StyledDocument)editor.getDocument()).addStyle("Keyword_Style",null);
normalStyle=((StyledDocument)editor.getDocument()).addStyle("Keyword_Style",null);
StyleConstants.setForeground(keywordStyle,Color.RED);
StyleConstants.setForeground(normalStyle,Color.BLACK);
//åå¤å ³é®å
keywords=newHashSetString();
keywords.add("public");
keywords.add("protected");
keywords.add("private");
keywords.add("_int9");
keywords.add("float");
keywords.add("double");
}
publicvoidcolouring(StyledDocumentdoc,intpos,intlen)throwsBadLocationException{
//åå¾æå ¥æè å é¤åå½±åå°çåè¯.
//ä¾å¦"public"å¨båæå ¥ä¸ä¸ªç©ºæ ¼,å°±åæäº:"public",è¿æ¶å°±æ两个åè¯è¦å¤ç:"pub"å"lic"
//è¿æ¶è¦åå¾çèå´æ¯pubä¸påé¢çä½ç½®ålicä¸cåé¢çä½ç½®
intstart=indexOfWordStart(doc,pos);
intend=indexOfWordEnd(doc,pos+len);
charch;
while(startend){
ch=getCharAt(doc,start);
if(Character.isLetter(ch)||ch=='_'){
//å¦ææ¯ä»¥åæ¯æè ä¸å线å¼å¤´,说ææ¯åè¯
//pos为å¤çåçæåä¸ä¸ªä¸æ
start=colouringWord(doc,start);
}else{
SwingUtilities.invokeLater(newColouringTask(doc,start,1,normalStyle));
++start;
}
}
}
/
***对åè¯è¿è¡çè²,并è¿ååè¯ç»æçä¸æ .
*
*@paramdoc
*@parampos
*@return
*@throwsBadLocationException
*/
publicintcolouringWord(StyledDocumentdoc,intpos)throwsBadLocationException{
intwordEnd=indexOfWordEnd(doc,pos);
Stringword=doc.getText(pos,wordEnd-pos);
if(keywords.contains(word)){
//å¦ææ¯å ³é®å,å°±è¿è¡å ³é®åççè²,å¦å使ç¨æ®éççè².
//è¿éæä¸ç¹è¦æ³¨æ,å¨insertUpdateåremoveUpdateçæ¹æ³è°ç¨çè¿ç¨ä¸,ä¸è½ä¿®æ¹docçå±æ§.
//ä½æ们åè¦è¾¾å°è½å¤ä¿®æ¹docçå±æ§,æ以ææ¤ä»»å¡æ¾å°è¿ä¸ªæ¹æ³çå¤é¢å»æ§è¡.
//å®ç°è¿ä¸ç®ç,å¯ä»¥ä½¿ç¨æ°çº¿ç¨,ä½æ¾å°swingçäºä»¶éåéå»å¤çæ´è½»ä¾¿ä¸ç¹.
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,keywordStyle));
}else{
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,normalStyle));
}
returnwordEnd;
}
/
**2024-12-24 03:05
2024-12-24 02:33
2024-12-24 02:23
2024-12-24 02:20
2024-12-24 02:18
2024-12-24 02:16