⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfjframe.java

📁 用java语言实现的简单词法分析器 实现了界面操作
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * CFJFrame.java
 *
 * Created on 2006年1月10日, 下午1:26
 */
package cffx;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
 *
 * @author  高兴
 */
 
//主窗口类
public class CFJFrame extends JFrame implements ActionListener {
	KedWord kdw = new KedWord();
	Word wo;
	File file_in,file_out;
	FileInputStream fileinput = null;
	FileOutputStream fileoutput = null;
	FileDialog filedialog_save,filedialog_load;
	BufferedReader in;
	FileReader file_reader;
	BufferedWriter out;
	FileWriter tofile;
	JMenuBar mb;
	JMenu file,comple,help;
	JMenuItem open,bnew,save,exit,com,about;
	FileInputStream readfile;
	JFileChooser chooser = new JFileChooser();
	JTextArea ta_in,ta_out;
	JLabel jb_in,jb_out;
	JToolBar tb;
	JButton jb_open,jb_new,jb_save,jb_com;
	JScrollPane pane_in,pane_out;
	public CFJFrame() {
		super("词法分析器");
		Container c = this.getContentPane();
		c.setBackground(new Color(212,208,200));
		c.setLayout(null);
		
		mb = new JMenuBar();
		file = new JMenu("文件");
		comple = new JMenu("编译");
		help = new JMenu("帮助");
		open = new JMenuItem("打开");
		open.setMnemonic('O');
		open.setAccelerator( KeyStroke.getKeyStroke('O', java.awt.Event.CTRL_MASK, false));
		bnew = new JMenuItem("新建");
		bnew.setMnemonic('N');
		bnew.setAccelerator( KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK, false));
		save = new JMenuItem("保存");
		save.setMnemonic('S');
		save.setAccelerator( KeyStroke.getKeyStroke('S', java.awt.Event.CTRL_MASK, false));
		exit = new JMenuItem("退出");
		exit.setMnemonic('X');
		exit.setAccelerator( KeyStroke.getKeyStroke('X', java.awt.Event.CTRL_MASK, false));
		com = new JMenuItem("编译");
		com.setMnemonic('C');
		com.setAccelerator( KeyStroke.getKeyStroke('C', java.awt.Event.CTRL_MASK, false));
		about = new JMenuItem("关于");
		
		this.setJMenuBar(mb);
		mb.add(file);
		mb.add(comple);
		mb.add(help);
		file.add(open);
		file.add(bnew);
		file.add(save);
		file.addSeparator();
		file.add(exit);		
		comple.add(com);
		help.add(about);
		
		open.addActionListener(this);
		bnew.addActionListener(this);
		save.addActionListener(this);
		exit.addActionListener(this);
		com.addActionListener(this);
		about.addActionListener(this);
		
		tb = new JToolBar();
		Icon com_icon = new ImageIcon("com.jpg");
		Icon open_icon = new ImageIcon("open.jpg");
		Icon new_icon = new ImageIcon("new.jpg");
		Icon save_icon = new ImageIcon("save.jpg");
		jb_open = new JButton(open_icon);
		jb_open.addActionListener(this);
		jb_new = new JButton(new_icon);
		jb_new.addActionListener(this);
		jb_save = new JButton(save_icon);
		jb_save.addActionListener(this);
		jb_com = new JButton(com_icon);
		jb_com.addActionListener(this);
        jb_open.setToolTipText("打开");
        jb_new.setToolTipText("新建");
        jb_save.setToolTipText("保存");
        jb_com.setToolTipText("编译");
		tb.add(jb_open);
		tb.add(jb_new);
		tb.add(jb_save);
		tb.add(jb_com);
		c.add(tb);
		tb.setBounds(0,0,700,30);
		
		filedialog_save = new FileDialog(this,"",FileDialog.SAVE);
		filedialog_save.setVisible(false);
		filedialog_load = new FileDialog(this,"",FileDialog.LOAD);
		filedialog_load.setVisible(false);		
		filedialog_save.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				filedialog_save.setVisible(false);
			}
		});
		filedialog_load.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				filedialog_load.setVisible(false);
			}
		});
		
		ta_in = new JTextArea();
		ta_in.setFont(new Font("Courier",Font.PLAIN,16));
		pane_in = new JScrollPane(ta_in);
		pane_in.setBounds(20,50,400,540);
		jb_in = new JLabel("源代码:");
		jb_in.setBounds(20,35,50,12);
		c.add(jb_in);
		c.add(pane_in);
		ta_out = new JTextArea();
		ta_out.setFont(new Font("Courier",Font.PLAIN,16));
		pane_out = new JScrollPane(ta_out);
		pane_out.setBounds(430,50,250,540);
		jb_out = new JLabel("输出结果:");
		jb_out.setBounds(430,35,80,12);
		c.add(jb_out);
		c.add(pane_out);
		
		this.setResizable(false);
		this.setBounds(100,50,700,650);
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==open) {
			System.out.println("打开");
			this.ta_out.setText("");
			this.ta_in.setText("");
			filedialog_load.setVisible(true);
			String s;
			try{
				File file = new File(filedialog_load.getDirectory(),
				filedialog_load.getFile());
				file_reader = new FileReader(file);
				in = new BufferedReader(file_reader);
				while((s=in.readLine())!=null){
					ta_in.append(s+'\n');
				}
			}
			catch(FileNotFoundException e1){
			}
			catch(IOException e1){
			}
			try{
				in.close();
				file_reader.close();
			}
			catch(IOException exp){
			}				
		}
		if(e.getSource()==bnew) {
			System.out.println("新建");
			this.ta_out.setText("");
			this.ta_in.setText("");
			ta_in.setText("");
		}
		if(e.getSource()==save) {
			System.out.println("保存");
			filedialog_save.setVisible(true);
			try{
				File file = new File(filedialog_save.getDirectory(),
					filedialog_save.getFile());
				tofile = new FileWriter(file);
				out = new BufferedWriter(tofile);
				out.write(ta_in.getText(),0,(ta_in.getText()).length());
				out.flush();
			}
			catch(FileNotFoundException e2){
			}
			catch(IOException e3){
			}
			try{
				out.close();
				tofile.close();
			}
			catch(IOException exp){
			}
		}
		if(e.getSource()==jb_open) {
			System.out.println("打开");
			this.ta_out.setText("");
			this.ta_in.setText("");
			filedialog_load.setVisible(true);
			String s;
			try{
				File file = new File(filedialog_load.getDirectory(),
				filedialog_load.getFile());
				file_reader = new FileReader(file);
				in = new BufferedReader(file_reader);
				while((s=in.readLine())!=null){
					ta_in.append(s+'\n');
				}
			}
			catch(FileNotFoundException e1){
			}
			catch(IOException e1){
			}
			try{
				in.close();
				file_reader.close();
			}
			catch(IOException exp){
			}				
		}
		if(e.getSource()==jb_new) {
			System.out.println("新建");
			this.ta_out.setText("");
			this.ta_in.setText("");
		}
		if(e.getSource()==jb_save) {
			System.out.println("保存");
			filedialog_save.setVisible(true);
			try{
				File file = new File(filedialog_save.getDirectory(),
					filedialog_save.getFile());									
				tofile = new FileWriter(file);
				out = new BufferedWriter(tofile);
				out.write(ta_in.getText(),0,(ta_in.getText()).length());
				out.flush();
			}
			catch(FileNotFoundException e2){
			}
			catch(IOException e3){
			}
			try{
				out.close();
				tofile.close();
			}
			catch(IOException exp){
			}
		}
		if(e.getSource()==exit) {
			System.out.println("退出");
			System.exit(0);
		}
		if(e.getSource()==com) {
			System.out.println("编译");
			Word wo = new Word(this,kdw);
			this.repaint();
		}
		if(e.getSource()==jb_com) {
			System.out.println("编译");
			this.ta_out.setText("");
			wo = new Word(this,kdw);
			System.out.println(wo.toString());
			this.repaint();
		}
		if(e.getSource()==about) {
			System.out.println("关于");
		}
	}
	public static void main(String args[]) {
		new CFJFrame();
	}
}


//单词识别类
class Word {
	CFJFrame cfjf;
	KedWord kdw;
	BufferedReader in;
	FileReader file_reader;
	BufferedWriter out;
	FileWriter tofile;
	RandomAccessFile raf;
	String s,s2;
	int senLength = 0;
    char[] sChar = new char[1000];
    String word = "",finalAccidence = "";
    char CHAR;
    int index = 0;
    boolean flag = false;
	public Word(CFJFrame cf,KedWord kw){
		cfjf = cf;
		kdw = kw;
		this.finalAccidence = "";
		System.out.println(kw.toString()+111);
		try{
			File file = new File("ta_out.txt");
			tofile = new FileWriter(file);
			out = new BufferedWriter(tofile);
			out.write(cfjf.ta_in.getText(),0,(cfjf.ta_in.getText()).length());
			out.flush();
			file_reader = new FileReader(file);
			in = new BufferedReader(file_reader);
			raf = new RandomAccessFile(file,"rw");			
			while((s=raf.readLine())!=null){
				System.out.println(s);
				s2 = this.toWord(s);
				s2 = s2 + "\n";
				System.out.println(s2);
				cfjf.ta_out.append(s2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -