简单的Java编译器的前端实现
2012-11-24 17:11
369 查看
最近写了个简单的Java编译器,其实做前端文本编辑,最后调用javac编译,完成Java编译器的基本功能。
首先,作为一个Java编译器,他应该具有的基本的功能包括:打开代码文本,编辑,保存,编译。
所以整体构建框架的思路就是:编辑java代码,保存代码,编译,处理出错信息。
编辑代码-----真正的代码编辑器编辑代码也就是一个高级点的文本编辑器,所以要实现一个文本编辑器的基本功能,这用一个类实现。同时,编辑代码一般都要做关键字高亮显示,所以这也用一个类实现。
根据这样的思路整个框架就出来了:
编辑代码区因为要关键字高亮,所以用JTextPane类可以实现。JTextArea类适合最普通的文本编辑的功能,但是在部分文本高亮方面比较难以实现。
具体实现:
新建功能:新建文本编辑前要确保已编辑的代码是否要保存,然后清空原来文本文字就可以了。
打开:就是读取文本文件,然后将内容写进代码文本编辑区。
保存和另存基本一样:就是用write方法把文本写进文件保存。
复制、粘贴、剪切、撤退很简单,直接用copy、paste等方法就可。
编译:调用javac直接编译获取编译结果。
主要比较困难的是如何动态实现关键字高亮。因为自己不会写,所以找了一个别人写好的类。
做了一周时间,由于只有我和一个女生在做,两个人都属于菜鸟级,工作量有点大,做的很累,最后还有一些细节的问题没有解决,例如如何让出错的那一行代码高亮显示等。等有时间了,想法成熟了再动手完善。做完之后,意识到开发窗体程序要有容器和面板的思想,把组件放进一个面板里,但是要注意放置的位置和次序,组件间的层次关系要清楚,由于是菜鸟所以这个问题曾经弄得很烦。
最后的结果图如下:
首先,作为一个Java编译器,他应该具有的基本的功能包括:打开代码文本,编辑,保存,编译。
所以整体构建框架的思路就是:编辑java代码,保存代码,编译,处理出错信息。
编辑代码-----真正的代码编辑器编辑代码也就是一个高级点的文本编辑器,所以要实现一个文本编辑器的基本功能,这用一个类实现。同时,编辑代码一般都要做关键字高亮显示,所以这也用一个类实现。
根据这样的思路整个框架就出来了:
编辑代码区因为要关键字高亮,所以用JTextPane类可以实现。JTextArea类适合最普通的文本编辑的功能,但是在部分文本高亮方面比较难以实现。
具体实现:
新建功能:新建文本编辑前要确保已编辑的代码是否要保存,然后清空原来文本文字就可以了。
if(menuItem == NewItem)//新建工程 { //判断代码是否改变,提醒是否保存
codetextPane.setText("");//清空原来文本
codetextPane.setEditable(true); codetextPane.setSelectedTextColor(Color.blue);//选中的代码颜色变为蓝色 }
打开:就是读取文本文件,然后将内容写进代码文本编辑区。
if(menuItem == OpenItem)//打开 { ........... fileName=OpenFileDialog.getDirectory()+OpenFileDialog.getFile(); if(fileName != null) { try { FileReader Readfile = new FileReader(fileName); BufferedReader Buff = new BufferedReader(Readfile); while((Lineword = Buff.readLine())!= null) { Lineword.trim(); content += (Lineword+'\n'); } codetextPane.setText(content); ......... } catch ( IOException e1 ) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { return; } }
保存和另存基本一样:就是用write方法把文本写进文件保存。
private void writeFile(String fileName2) throws IOException { File file = new File(fileName); FileWriter writeOut = new FileWriter(file); writeOut.write(codetextPane.getText());//写文件 writeOut.close(); }
复制、粘贴、剪切、撤退很简单,直接用copy、paste等方法就可。
编译:调用javac直接编译获取编译结果。
Runtime run = Runtime.getRuntime(); Process process = null; try { process = run.exec("javac "+fileName); BufferedInputStream in = new BufferedInputStream(process.getErrorStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); while((str = reader.readLine()) != null) { string += str; string += "\n"; } errortextArea.setText(string+"\n");
主要比较困难的是如何动态实现关键字高亮。因为自己不会写,所以找了一个别人写好的类。
public SyntaxHighlighter(JTextPane editor) { // 准备着色使用的样式 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); String KEYWORDS[]={"abstract","assert","boolen","break","byte","case","catch","char","class","const", "continue","default","do","double","else","enum","extends","final","finally","float","for", "if","implements","import","instanceof","int","interface","long","native","new","package", "private","protected","public","return","short","static","strictfp","super","switch","synchrpnized", "this","throw","throws","transient","try","void","volatile","while"}; // 准备关键字 keywords = new HashSet<String>(); for(int i = 0; i<KEYWORDS.length; i++) { keywords.add(KEYWORDS[i]); } } ....................
做了一周时间,由于只有我和一个女生在做,两个人都属于菜鸟级,工作量有点大,做的很累,最后还有一些细节的问题没有解决,例如如何让出错的那一行代码高亮显示等。等有时间了,想法成熟了再动手完善。做完之后,意识到开发窗体程序要有容器和面板的思想,把组件放进一个面板里,但是要注意放置的位置和次序,组件间的层次关系要清楚,由于是菜鸟所以这个问题曾经弄得很烦。
最后的结果图如下:
相关文章推荐
- 一个简单编译器前端的实现
- 基于java实现的简单网页日历功能,有兴趣得可以把它转换到前端实现
- JAVA实现一个简单的代数运算语言编译器(一)--写在前面
- 用java实现编译器之代码实现Thompson构造:在简单NFA的基础上构造更复杂的NFA
- 简单编译器前端的实现
- JAVA实现一个简单的代数运算语言编译器(二)--词法分析准备
- JAVA实现一个简单的代数运算语言编译器(三)--词法分析
- JAVA实现一个简单的代数运算语言编译器(四)-- 表达式计算
- Java中利用socket实现简单的服务端与客户端的通信(中级)——实现任意双向通信
- 一个简单的厦大网站前端实现
- MySQL第五天---存储过程、查询区分大小写、事务(MySQL及Java实现的简单模板)
- Java实现的一个简单的模板渲染
- Java版C语言版简单使用静态语言实现动态数组的方法
- java几种排序简单实现(快速排序,冒泡排序,直接插入排序)
- 使用java简单模拟ping和telnet的实现
- JAVA编写的一个简单的Socket实现的HTTP响应服务器
- java实现一个叫简单的xml解析
- Java Socket发送与接收HTTP消息简单实现
- java实现一个简单TCPSocket聊天室功能分享
- K_Means优化算法之Canopy算法----java简单实现