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

📄 analyseframe.java

📁 语法分析,词法分析,三地址码解析.外加说明文档。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JSplitPane;


public class AnalyseFrame extends JFrame implements DocumentListener {

	/*parameters initialization*/
	private int l; //the lenght of tong

	private int k; //the current index of tong

	private int line = 1, hang = 1;//each semicolon represents a line or hang

	private int n;//the lenght of bucket

	private int p; //the current index of bucket

	private String tong[];//array to store tokens

	private String bucket[];//array to store tokens

	private String current_word;//the current wrod of arrays

	//the following variables are used in the GUI
	private JPanel jContentPane = null;

	private JToolBar toolBar = null;

	private JMenuBar menuBar = null;

	private JMenu fileMenu = null;

	private JMenu analyseMenu = null;

	private JMenu aboutMenu = null;

	private Action openAction = null;

	private Action saveAction = null;

	private Action newAction = null;

	private Action aboutAction = null;

	private Action analyseAction = null;

	private Action exitAction = null;

	private JScrollPane inScrollPane = null;

	private JTextArea inTextArea = null;

	private JScrollPane outScrollPane = null;

	private JTextArea outTextArea = null;

	private File sourFile = null;//source file you want to compile

	private File objFile = null;//file to store the result generated by compiler

	private JSplitPane splitPane = null;

	private Document indocument;

	private boolean edited = false;//judge whether the file is edited or not

	/**
	 * This method initializes toolBar
	 *
	 * @return javax.swing.JToolBar
	 */
	private JToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new JToolBar();
			toolBar.add(getOpenAction());
			toolBar.add(getSaveAction());
			toolBar.add(getNewAction());
			toolBar.add(getAnalyseAction());
			toolBar.add(getAboutAction());
			toolBar.add(getExitAction());
		}
		return toolBar;
	}

	/**
	 * This method initializes menuBar
	 *
	 * @return javax.swing.JMenuBar
	 */
	private JMenuBar getmenuBar() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			fileMenu = new JMenu("File");
			fileMenu.setMnemonic('F');
			fileMenu.add(getNewAction());
			fileMenu.add(getOpenAction());
			fileMenu.add(getSaveAction());
			fileMenu.addSeparator();
			fileMenu.add(getExitAction());
			menuBar.add(fileMenu);
			analyseMenu = new JMenu("Analyse");
			analyseMenu.setMnemonic('n');
			analyseMenu.add(getAnalyseAction());
			menuBar.add(analyseMenu);
			aboutMenu = new JMenu("About");
			aboutMenu.setMnemonic('A');
			aboutMenu.add(getAboutAction());
			menuBar.add(aboutMenu);

		}
		return menuBar;
	}

	/**
	 * This method initializes openAction
	 *
	 * @return javax.swing.Action
	 */
	private Action getOpenAction() {
		if (openAction == null) {
			openAction = new AbstractAction() {
				public void actionPerformed(ActionEvent e) {
					openFile();
				}
			};
			openAction.putValue(Action.NAME, "Open");
			openAction.putValue(Action.SMALL_ICON, new ImageIcon(
					AnalyseFrame.class.getResource("/img/open.png")));
			openAction.putValue(Action.SHORT_DESCRIPTION, "Open a file");
			openAction.putValue(Action.MNEMONIC_KEY, new Integer('O'));
		}
		return openAction;
	}

	/**
	 * This method initializes saveAction
	 *
	 * @return javax.swing.Action
	 */
	private Action getSaveAction() {
		if (saveAction == null) {
			saveAction = new AbstractAction() {
				public void actionPerformed(ActionEvent e) {
					saveFile();
				}
			};
			saveAction.putValue(Action.NAME, "Save");
			saveAction.putValue(Action.SMALL_ICON, new ImageIcon(
					AnalyseFrame.class.getResource("/img/save.png")));
			saveAction.putValue(Action.SHORT_DESCRIPTION, "Save current file");
			saveAction.putValue(Action.MNEMONIC_KEY, new Integer('S'));
		}
		return saveAction;
	}

	/**
	 * This method initializes newAction
	 *
	 * @return javax.swing.Action
	 */
	private Action getNewAction() {
		if (newAction == null) {
			newAction = new AbstractAction() {
				public void actionPerformed(ActionEvent e) {
					newFile();
				}
			};
			newAction.putValue(Action.NAME, "New");
			newAction.putValue(Action.SMALL_ICON, new ImageIcon(
					AnalyseFrame.class.getResource("/img/new.png")));
			newAction.putValue(Action.SHORT_DESCRIPTION, "Create a new file");
			newAction.putValue(Action.MNEMONIC_KEY, new Integer('N'));
		}
		return newAction;
	}

	/**
	 * This method initializes analyseAction
	 *
	 * @return javax.swing.Action
	 */
	private Action getAnalyseAction() {
		if (analyseAction == null) {
			analyseAction = new AbstractAction() {
				public void actionPerformed(ActionEvent e) {

					analyseFile();
					analysisSyn();
					check_type();
					generate_code();

				}
			};
			analyseAction.putValue(Action.NAME, "Analyse");
			analyseAction.putValue(Action.SMALL_ICON, new ImageIcon(
					AnalyseFrame.class.getResource("/img/analyse.png")));
			analyseAction.putValue(Action.SHORT_DESCRIPTION,
					"Analyse current file");
			analyseAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
		}
		return analyseAction;
	}

	/**
	 * This method initializes aboutAction
	 *
	 * @return javax.swing.Action
	 */
	private Action getAboutAction() {
		if (aboutAction == null) {
			aboutAction = new AbstractAction() {
				Icon imageme = new ImageIcon(AnalyseFrame.class
						.getResource("/img/spence.jpg"));

				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null,
							"项目:小编译器\n作者:林联锋\n学号: 0409853F-I011-0032\n专业:计算机软件\n"
									+ "联系:feixianyexin@163.com", "关于作者",
							JOptionPane.INFORMATION_MESSAGE, imageme);
				}
			};
			aboutAction.putValue(Action.NAME, "About");
			aboutAction.putValue(Action.SMALL_ICON, new ImageIcon(
					AnalyseFrame.class.getResource("/img/about.png")));
			aboutAction
					.putValue(Action.SHORT_DESCRIPTION, "About this program");
			aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
		}
		return aboutAction;
	}

	/**
	 * This method initializes exitAction
	 *
	 * @return javax.swing.Action
	 */
	private Action getExitAction() {
		if (exitAction == null) {
			exitAction = new AbstractAction() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			};
			exitAction.putValue(Action.NAME, "Exit");
			exitAction.putValue(Action.SMALL_ICON, new ImageIcon(
					AnalyseFrame.class.getResource("/img/exit.png")));
			exitAction.putValue(Action.SHORT_DESCRIPTION, "Exit this program");
			exitAction.putValue(Action.MNEMONIC_KEY, new Integer('E'));
		}
		return exitAction;
	}

	/**
	 * This method initializes inscrollPane
	 *
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getInScrollPane() {
		if (inScrollPane == null) {
			inScrollPane = new JScrollPane();
			inScrollPane.setViewportView(getInTextArea());

		}
		return inScrollPane;
	}

	/**
	 * This method initializes inTextArea
	 *
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getInTextArea() {
		if (inTextArea == null) {
			inTextArea = new JTextArea();
			indocument = inTextArea.getDocument();
			indocument.addDocumentListener(this);
			inTextArea.setEditable(false);
			inTextArea.setForeground(Color.magenta);
			inTextArea.setFont(new Font(null, Font.BOLD, 14));
		}
		return inTextArea;
	}

	/**
	 * This method initializes outScrollPane
	 *
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getOutScrollPane() {
		if (outScrollPane == null) {
			outScrollPane = new JScrollPane();
			outScrollPane.setViewportView(getOutTextArea());
		}
		return outScrollPane;
	}

	/**
	 * This method initializes outTextField
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextArea getOutTextArea() {
		if (outTextArea == null) {
			outTextArea = new JTextArea();
			outTextArea.setEditable(false);
			outTextArea.setForeground(Color.BLUE);
			outTextArea.setFont(new Font(null, Font.BOLD, 12));
		}
		return outTextArea;
	}

	/**
	 * This method initializes splitPane
	 *
	 * @return javax.swing.JSplitPane
	 */
	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
					getInScrollPane(), getOutScrollPane());
			splitPane.setOneTouchExpandable(true);
			splitPane.setDividerLocation(350);

		}
		return splitPane;
	}

	/**
	 * mathod main to run this program
	 * @param args
	 */
	public static void main(String[] args) {
		new AnalyseFrame();

	}

	/**
	 * This is the default constructor
	 */
	public AnalyseFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this analyseFrame
	 *
	 * @return void
	 */
	private void initialize() {
		// this.setSize(600, 400);
		this.setContentPane(getJContentPane());
		this.setTitle("小编译器");
		this.setJMenuBar(getmenuBar());
		this.setBounds(200, 150, 600, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getToolBar(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getSplitPane(), java.awt.BorderLayout.CENTER);

		}
		return jContentPane;
	}

	/**
	 * This method initializes open file
	 *
	 * @return void
	 */
	private void openFile() {
		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			sourFile = fc.getSelectedFile();
			try {
				inTextArea.read(new FileReader(sourFile), null);
				this.setTitle("小编译器 - " + sourFile.getName());
				inTextArea.setEditable(true);
				edited = false;
				indocument = inTextArea.getDocument();
				indocument.addDocumentListener(this);
				outTextArea.setText("");
			} catch (IOException ioe) {
				JOptionPane.showMessageDialog(this, "Can not open this file!",
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	/**
	 * This method initializes saveFile
	 *
	 * @return void
	 */
	private void saveFile() {
		if (!inTextArea.isEditable()) {
			JOptionPane.showMessageDialog(this, "Please create a new file",
					"Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (sourFile == null) {
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showSaveDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				sourFile = fc.getSelectedFile();
			} else
				return;
		}
		try {
			inTextArea.write(new FileWriter(sourFile));
			this.setTitle("小编译器 - " + sourFile.getName());
			edited = false;

		} catch (IOException ioe) {
			JOptionPane.showMessageDialog(this, "can not open this file!",
					"Error", JOptionPane.ERROR_MESSAGE);
		}

	}

	/**
	 * This method initializes newFile
	 *
	 * @return void
	 */

⌨️ 快捷键说明

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