1.求Java记事本源代码
2.JAVAPOS怎么加切纸命令
3.我是一个刚刚出道的程序员最近应聘了一家专做pos机的公司。想问一下,pos机那里会用到j
4.使ç¨javaå¼å代ç ç¼è¾å¨(使ç¨javaå¼å代ç ç¼è¾å¨ç好å¤)
求Java记事本源代码
自己写的,能实现基本功能:
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
/*因为根据个人的电脑路径可能有所偏差,没有源路径的数字货币充提源码情况下,设置默认保存路径为D盘根目录下
* 若要选择保存其他地方,可以选择 另存为*/
public class TestDemo extends JFrame {
private static final long serialVersionUID = -L;
private String url = null;//文件路径
private String str=null;//复制或剪切 的字符串
private StringSelection stringSelection=null;
private Clipboard clipboard=new Clipboard(str);
private Transferable transferable=null;
private DataFlavor flavor=null;
public TestDemo() {
init();
}
private void init() {
setTitle("我的记事本");
setSize(, );
setContentPane(createContentPane());//添加主面板
}
/*创建主面板*/
private JPanel createContentPane() {
JPanel pane = new JPanel(new BorderLayout());
pane.add(BorderLayout.NORTH, createChocePane());//添加菜单栏
pane.add(createAreaPane());//添加文本编辑区域
return pane;
}
/*创建菜单栏,以及实现功能*/
private JPanel createChocePane() {
JPanel pane = new JPanel();
JMenuBar menuBar1 = new JMenuBar();
JMenu menu = new JMenu("文件");
menuBar1.add(menu);
JMenuItem menuIt1 = new JMenuItem("新建");
JMenuItem menuIt2 = new JMenuItem("打开");
JMenuItem menuIt3 = new JMenuItem("保存");
JMenuItem menuIt4 = new JMenuItem("另存为");
menu.add(menuIt1);
menu.add(menuIt2);
menu.add(menuIt3);
menu.add(menuIt4);
JMenuBar menuBar2 = new JMenuBar();
JMenu menu2 = new JMenu("编辑");
menuBar2.add(menu2);
JMenuItem menuIt5 = new JMenuItem("复制");
JMenuItem menuIt6 = new JMenuItem("剪切");
JMenuItem menuIt7 = new JMenuItem("粘帖");
menu2.add(menuIt5);
menu2.add(menuIt6);
menu2.add(menuIt7);
JMenuBar menuBar3 = new JMenuBar();
JMenu menu3 = new JMenu("帮助");
menuBar3.add(menu3);
JMenuItem menuIt8 = new JMenuItem("关于记事本");
menu3.add(menuIt8);
pane.add(menuBar1);
pane.add(menuBar2);
pane.add(menuBar3);
menuIt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testArea.setText(null);
}
});
menuIt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final FileDialog fd = new FileDialog(new JFrame(), "查找文件",
FileDialog.LOAD);
fd.setVisible(true);
if (fd.getDirectory() != null && fd.getFile() != null) {
testArea.setText(null);
url = fd.getDirectory() + fd.getFile();
try {
BufferedReader in = new BufferedReader(new FileReader(
url));
for (int i = 0;; i++) {
testArea.append(in.readLine());
if (in.read() == -1) {
break;
} else
continue;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
menuIt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (url==null) {
url="D:\\新建 文本文档.txt";
}
File f = new File(url);
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(url));
f.createNewFile();
out.append(testArea.getText());
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FileDialog fd = new FileDialog(new JFrame(), "保存文本",
FileDialog.SAVE);
fd.setVisible(true);
if (url!=null) {
File f = new File(url);
BufferedWriter out = null;
try {
f.createNewFile();
out = new BufferedWriter(new FileWriter(url));
out.append(testArea.getText());
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
menuIt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
}
});
menuIt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
str=testArea.getSelectedText();
stringSelection=new StringSelection(str);
clipboard.setContents(stringSelection, null);
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
}
});
menuIt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
transferable=clipboard.getContents(this);
flavor=DataFlavor.stringFlavor;
if (transferable.isDataFlavorSupported(flavor)) {
int start=testArea.getSelectionStart();
int end=testArea.getSelectionEnd();
testArea.replaceRange( null,start,end);
int pos=testArea.getCaretPosition();
try {
str=(String)transferable.getTransferData(flavor);
testArea.insert(str, pos);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
menuIt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"功能简单,绝对原创 ");
}
});
return pane;
}
JTextArea testArea;
private JScrollPane createAreaPane() {
JScrollPane pane = new JScrollPane();
pane.setBorder(new TitledBorder("编辑区域"));
testArea = new JTextArea();
testArea.setFont(new Font("宋体", Font.BOLD, ));
testArea.setLineWrap(true);
pane.getViewport().add(testArea);
return pane;
}
public static void main(String[] args) {
TestDemo td = new TestDemo();
td.setVisible(true);
}
}
JAVAPOS怎么加切纸命令
设置来自于电脑驱动程序的纸张设置(例如:“X”等),如果你的纸张定义的准确,在打印完成后纸张应该是停住不动的,如果你按一下“切纸”键,航母源码谁写纸张会将两张纸的接缝处准确地走到分纸器的切纸位置,这时你就可以很容易的撕纸了。撕完纸后,按一下“切纸”或者“联机”纸张又会回退到打印初始的位置,可以继续输入打印命令。点对点视频软件源码设置切纸的要领就是定义纸张和排版!
微调页顶保存应该是同时按“联机”+“进纸/退纸”键,不可能保存不住!
我是一个刚刚出道的程序员最近应聘了一家专做pos机的公司。想问一下,黑客主页源码在哪pos机那里会用到j
我是一个刚刚出道的程序员最近应聘了一家专做pos机的公司。想问一下,pos机那里会用到java?很多地方都会用的,比如pos监控系统,一般都是燕窝碎溯源码用java写的。
补充:pos监控就是使用b/s架构的,一般后台用Weblogic
刷卡机连接java
你也只是写刷卡的管理系统吧。比如计费什么的?
你找到POS机的供应商,问他们要接口。。。什么都有了
JavaPOS 什么东西
POS机的管理程序就是用java编写的,是用来管理POS机的各种硬件的驱动和商场营业的进\销\存等管理 ~
POS监控就是使用b/s架构的,一般后台用Weblogic~
POS机不能刷卡,显示java驱动连接失败,怎么办?
后端平台进bios设置看看,因为如今的主板bios默认是关闭com口的,因此bios打开com口就不必再修改了
java如何接收pos机数据
建议你查一下开源的jpos资料(用java来虚拟pos机),另外查一下协议的资料。应该对你的问题有所帮助。
java对接pos机的介绍就聊到这里吧。
使ç¨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,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-11-30 06:58
2024-11-30 06:30
2024-11-30 06:13
2024-11-30 05:53
2024-11-30 05:51
2024-11-30 05:09
2024-11-30 04:49
2024-11-30 04:40