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

📄 analyzerframe.java

📁 cmm语言词法分析器的详细源代码和执行程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//Frame for CMM lexical analyzer
import javax.swing.*;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import static java.awt.event.InputEvent.*;
import static java.awt.Color.*;

/**
 * 
 *  puwangrong@163.com
 */
public class AnalyzerFrame extends JFrame implements ActionListener {
	// Constructor
	public AnalyzerFrame(String title) {
		setTitle(title); // Set the window title
		setJMenuBar(menuBar); // Add the menuBar
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		jTextArea1.getDocument().addUndoableEditListener(undoHandler);

		// Create the menus
		JMenu file_menu = new JMenu("文件(F)");
		JMenu edit_menu = new JMenu("编辑(E)");
		JMenu run_menu = new JMenu("运行(R)");
		JMenu settings_menu = new JMenu("设置(S)");
		JMenu help_menu = new JMenu("帮助(H)");

		// Create the shotcuts
		file_menu.setMnemonic('F');
		edit_menu.setMnemonic('E');
		run_menu.setMnemonic('R');
		settings_menu.setMnemonic('S');
		help_menu.setMnemonic('H');

		// Create the action items for the file_menu
		new_action = new FileAction("新建", KeyStroke
				.getKeyStroke('N', CTRL_MASK), "新建");
		open_action = new FileAction("打开", KeyStroke.getKeyStroke('O',
				CTRL_DOWN_MASK), "打开");
		save_action = new FileAction("保存", KeyStroke.getKeyStroke('S',
				CTRL_DOWN_MASK), "保存");
		close_action = new FileAction("退出");

		// Create the action items for the edit_menu
		cut_action = new FileAction("剪切", KeyStroke.getKeyStroke('T',
				CTRL_DOWN_MASK), "剪切");
		copy_action = new FileAction("复制", KeyStroke.getKeyStroke('C',
				CTRL_DOWN_MASK), "复制");
		paste_action = new FileAction("粘贴", KeyStroke.getKeyStroke('V',
				CTRL_DOWN_MASK), "粘贴");
		cancel_action = new FileAction("撤销", KeyStroke.getKeyStroke('Z',
				CTRL_DOWN_MASK), "撤销");

		// Create the action items for the run_menu
		run_action = new FileAction("分析", KeyStroke.getKeyStroke('R',
				CTRL_DOWN_MASK), "词法分析");

		// Create the action items for the settings_menu
		font_action = new FileAction("字体", KeyStroke.getKeyStroke('F',
				CTRL_DOWN_MASK));

		// Create the action items for the help_menu
		help_action = new JMenuItem("帮助");
		about_action = new JMenuItem("关于");

		// Construct the file_menu
		file_menu.add(new JMenuItem(new_action));
		file_menu.add(new JMenuItem(open_action));
		file_menu.add(new JMenuItem(save_action));
		file_menu.addSeparator();
		file_menu.add(new JMenuItem(close_action));

		// Construct the edit_menu
		edit_menu.add(new JMenuItem(cut_action));
		edit_menu.add(new JMenuItem(copy_action));
		edit_menu.add(new JMenuItem(paste_action));
		edit_menu.addSeparator();
		edit_menu.add(new JMenuItem(cancel_action));

		// Construct the run_menu
		run_menu.add(new JMenuItem(run_action));

		// Construct the settings_menu
		settings_menu.add(new JMenuItem(font_action));

		// Construct the help_menu
		help_menu.add(help_action);
		help_action.addActionListener(this);
		help_menu.add(about_action);
		about_action.addActionListener(this);

		// Add the menus
		menuBar.add(file_menu);
		menuBar.add(edit_menu);
		menuBar.add(run_menu);
		menuBar.add(settings_menu);
		menuBar.add(help_menu);

		// Add the toolBar
		toolBar.addSeparator();
		addToolBarButton(new_action);
		addToolBarButton(open_action);
		addToolBarButton(save_action);

		toolBar.addSeparator();
		addToolBarButton(cut_action);
		addToolBarButton(copy_action);
		addToolBarButton(paste_action);
		addToolBarButton(cancel_action);

		toolBar.addSeparator();
		addToolBarButton(run_action);

		toolBar.setBorder(BorderFactory.createEtchedBorder(WHITE, LIGHT_GRAY)); // Create
		// the
		// border
		toolBar.setFloatable(false); // Init the toolBar
		getContentPane().add(toolBar, BorderLayout.NORTH);

		// Add the popupMenu
		popupMenu.add(cut_action);
		popupMenu.add(copy_action);
		popupMenu.add(paste_action);
		popupMenu.addSeparator();
		popupMenu.add(cancel_action);

		// AddMouseListener for jTextArea1
		jTextArea1.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				checkForTriggerEvent(e);
			}

			public void mouseReleased(MouseEvent e) {
				checkForTriggerEvent(e);
			}

			private void checkForTriggerEvent(MouseEvent e) {
				if (e.isPopupTrigger())
					popupMenu.show(e.getComponent(), e.getX(), e.getY());
				jTextArea1.requestFocus();
			}

		});

		// Create the panels --Codes were created by NetBeans 5.5
		jPanel1.setBorder(javax.swing.BorderFactory
				.createTitledBorder("CMM\u6e90\u7a0b\u5e8f"));
		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addContainerGap()
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 313,
								Short.MAX_VALUE).addContainerGap()));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout.createSequentialGroup().addContainerGap()
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 445,
								Short.MAX_VALUE).addContainerGap()));

		jPanel2.setBorder(javax.swing.BorderFactory
				.createTitledBorder("\u5206\u6790\u8f93\u51fa"));
		jTextArea2.setColumns(20);
		jTextArea2.setRows(100);
		jScrollPane2.setViewportView(jTextArea2);

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel2Layout.createSequentialGroup().addContainerGap()
						.addComponent(jScrollPane2,
								javax.swing.GroupLayout.DEFAULT_SIZE, 334,
								Short.MAX_VALUE).addContainerGap()));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel2Layout.createSequentialGroup().addContainerGap()
						.addComponent(jScrollPane2,
								javax.swing.GroupLayout.DEFAULT_SIZE, 445,
								Short.MAX_VALUE).addContainerGap()));

		// ...Layout : Add the Panels --Codes were created by NetBeans 5.5
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addContainerGap().addComponent(
						jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGap(23, 23, 23).addComponent(jPanel2,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addContainerGap())
				.addComponent(toolBar, javax.swing.GroupLayout.DEFAULT_SIZE,
						762, Short.MAX_VALUE));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(
												toolBar,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												41,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																jPanel2,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jPanel1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap()));
		pack();

	}

	// Inner class defining action objects for the menu items
	public class FileAction extends AbstractAction {
		FileAction(String name) {
			super(name);
			String iconFileName = "Images/"+name + ".gif";
			if (new File(iconFileName).exists()) {
				putValue(SMALL_ICON, new ImageIcon(iconFileName));
			}
		}

		FileAction(String name, KeyStroke keystroke) {
			this(name);
			if (keystroke != null) {
				putValue(ACCELERATOR_KEY, keystroke);
			}
		}

		FileAction(String name, KeyStroke keystroke, String tooltip) {
			this(name, keystroke);
			if (tooltip != null) {
				putValue(SHORT_DESCRIPTION, tooltip);
			}
		}

		// Event handler
		public void actionPerformed(ActionEvent e) {
			String name = (String) getValue(NAME);
			if (name.equals(new_action.getValue(NAME))) {
				create();
			} else if (name.equals(save_action.getValue(NAME))) {
				save();
			} else if (name.equals(open_action.getValue(NAME))) {
				open();
			} else if (name.equals(close_action.getValue(NAME))) {
				exit();
			} else if (name.equals(run_action.getValue(NAME))) {
				jTextArea2.setText("");
				run();
			} else if (name.equals(font_action.getValue(NAME))) {
				new FontDemo();
			} else if (name.equals(cut_action.getValue(NAME))) {
				cut();
			} else if (name.equals(copy_action.getValue(NAME))) {
				copy();
			} else if (name.equals(paste_action.getValue(NAME))) {
				paste();
			} else if (name.equals(cancel_action.getValue(NAME))) {
				cancel();
			}
		}
	}

	// Method to add a button to the toolBar
	private JButton addToolBarButton(Action action) {
		JButton button = new JButton(action);
		button.setBorder(BorderFactory.createRaisedBevelBorder());// Add
		// button
		// border
		button.setText(null);
		toolBar.add(button);
		return button;
	}

	// Method to create a file
	public void create() {
		jTextArea1.requestFocus();
		jTextArea1.setText(null);
	}

	// Method to open a file
	public void open() {
		String str = "";
		StringBuilder s = new StringBuilder();
		chooser = new JFileChooser(
				"C:\\Documents and Settings\\All Users\\Documents");
		int returnVal = chooser.showOpenDialog(frm);
		if (returnVal == chooser.APPROVE_OPTION) {
			try {
				File file = new File(chooser.getSelectedFile()
						.getAbsolutePath());
				int length;
				FileReader in = new FileReader(file);
				char buf[] = new char[1024];
				while ((length = in.read(buf)) != -1) {
					str = new String(buf, 0, length);
					s.append(str);
				}
			} catch (IOException a) {
			}
			jTextArea1.setText(s.toString());
		}
	}

	// Method to save the file
	public void save() {
		int i = JOptionPane.showConfirmDialog(frm, "确定保存文件吗?");
		if (i == 0) {

⌨️ 快捷键说明

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