- package com.java.wei;
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import static com.java.wei.DefineUtil.*;
- public class theFirst extends JFrame{
- private JTextField jtf1 = new JTextField();
- private JTextArea jta1 = new JTextArea();
- private JPanel jp1 = new JPanel();
- private JFrame frame = new JFrame();
- private JButton button = new JButton("编译");
- public theFirst(){
- jp1.setLayout(new BorderLayout());
- JPanel p1 = new JPanel();
- p1.setLayout(new BorderLayout());
- p1.add(new JLabel("输入一段程序"),BorderLayout.WEST);
- p1.add(jtf1,BorderLayout.CENTER);
- jp1.add(p1,BorderLayout.NORTH);
- jp1.add(new JScrollPane(jta1),BorderLayout.CENTER);
- jp1.add(button,BorderLayout.SOUTH);
- button.addActionListener(new ButtonListener());
- frame.add(jp1);
- frame.setVisible(true);
- frame.setSize(500, 600);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public int theSun(int[] array){
- int sun =0;
- for(int i =0;i<array.length;i++){
- sun+=array[i];
- }
- return sun;
- }
- //判断是否是关键字
- public boolean isDEFI(String string){
- for(int i = 0;i<DEFINESTR.length;i++){
- if(string.equals(DEFINESTR[i])){
- return true;
- }
- }
- return false;
- }
- //判断是否是数字
- public boolean isNum(String string){
- int[] itRight = new int[string.length()];
- for(int i=0;i<string.length();i++){
- if(string.charAt(i)>='0'&&string.charAt(i)<='9')
- itRight[i]=0;
- else itRight[i]=1;
- }
- if(theSun(itRight)==0){
- return true;
- }
- return false;
- }
- // 判断是否是字符串
- public boolean isString(String string){
- int[] itRight = new int[string.length()];
- for(int i=0;i<string.length();i++){
- if(string.charAt(i)>='a'&&string.charAt(i)<='z'||string.charAt(i)>='A'&&string.charAt(i)<='Z'){
- itRight[i]=0;
- }
- else {itRight[i]=1;}
- }
- if(theSun(itRight)==0){
- return true;
- }
- return false;
- }
- // 判断师傅是"=“
- public boolean isFuzhi(String string){
- int[] itRight = new int[string.length()];
- for(int i=0;i<string.length();i++){
- if(string.charAt(i)=='=')
- itRight[i]=0;
- else {itRight[i]=1;}
- }
- if(theSun(itRight)==0){
- return true;
- }
- return false;
- }
- //处理各种情况
- public String outNum(String string){
- String b= null;
- if(isNum(string)){
- b =string+ITISNUM;
- }
- else if(isFuzhi(string)){
- b =string+ITISFUZ;
- }
- else if (isString(string)){
- if(isDEFI(string)){
- b=string+ITISDEF;
- }else{
- b =string+ITISSTR;
- }
- }
- return b;
- }
- //Button监听事件
- private class ButtonListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent arg0) {
- String in = jtf1.getText().toString();
- int i = 0;
- int j =0;
- // int last = in.length();
- String out = null;
- for( ;i<in.length();){
- //int j= 0;
- // System.out.println(in[i]);
- // if(in[i]>'a'&&in[i]<'z'||in[i]>'A'&&in[i]<'Z'){
- // jta1.append("第"+i+"个是字母");
- // }
- // if(in[i]>'0'&&in[i]<'9')
- // jta1.append("第"+i+"是数字");
- //以空格后者是”;“来分割字符串
- if(in.charAt(i)==' '||in.charAt(i)==';'){
- out=in.substring(j,i);
- j=++i;
- String type = outNum(out);
- if(type!=null)
- jta1.append(type+'\\n');
- else jta1.append(ITISRON+"\\n");
- }
- // if(in.charAt(i)=='='){
- // out=in.substring(j,i);
- // j=i;
- // String type = "赋值符号";
- // jta1.append(out.toString()+type+'\\n');
- // }
- // if(i==last-1){
- // out = in.substring(j);
- // String type = outNum(out);
- // jta1.append(out+type);
- // }
- i++;
- }
- }
- }
- public static void main(String[] args){
- new theFirst();
- }
- }
- package com.java.wei;
- public class DefineUtil {
- public final static String[] DEFINESTR = {
- "abstract","double","int","super","assert","else","interface",
- "switch","boolean","enum","long","synchronized","break",
- "extends","native","this","byte","for","new","throw","case",
- "final","package","throws","catch","finally","private","transient",
- "char","float","protected","try","class","goto","public","void",
- "const","if","return","continue","implements","short","while",
- "default","import","static","do","instanceof","strictfp"
- };
- public final static String ITISNUM = "这是个数字";
- public final static String ITISSTR ="这是字符串" ;
- public final static String ITISDEF = "这是关键字";
- public final static String ITISFUZ ="这是赋值符号";
- public final static String ITISRON ="书写格式有错误";
- }
- //该片段来自于http://www.codesnippet.cn/detail/290920136236.html
来源: http://www.codesnippet.cn/detail/290920136236.html