简单的Java编译器的前端实现

最近写了个简单的Java编译器,其实做前端文本编辑,最后调用javac编译,完成Java编译器的基本功能。

首先,作为一个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]);
        }     
    }  ....................

做了一周时间,由于只有我和一个女生在做,两个人都属于菜鸟级,工作量有点大,做的很累,最后还有一些细节的问题没有解决,例如如何让出错的那一行代码高亮显示等。等有时间了,想法成熟了再动手完善。做完之后,意识到开发窗体程序要有容器和面板的思想,把组件放进一个面板里,但是要注意放置的位置和次序,组件间的层次关系要清楚,由于是菜鸟所以这个问题曾经弄得很烦。

最后的结果图如下:







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值