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

📄 wordanalyze.java

📁 用DFA方法写得词法分析器 和大家交流一下
💻 JAVA
字号:
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JMenu;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JMenuItem;

/*
 * this is the main class
 * it uses the methods in the Ana class
 * author:贺静
 */

public class WordAnalyze extends JFrame implements ActionListener,
		MouseListener {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JSplitPane jSplitPane = null;

	private JScrollPane leftScrollPane = null;

	private JTextArea inputTextArea, outTextArea;

	private JScrollPane rightScrollPane = null;

	private JPanel jPanel = null;

	private JButton openButton, scanButton;

	private JFileChooser fileChooser = new JFileChooser();

	private JMenu jMenu = null;

	private JMenuBar clearBar = null;

	private JMenuItem inputItem, outputItem, allItem, pm1, pm2, pm3;

	private JPopupMenu pm = new JPopupMenu();

	// return javax.swing.JPanel
	private JPanel getJContentPane() {
		jContentPane = new JPanel();
		jContentPane.setLayout(new BorderLayout());
		jContentPane.add(getJPanel(), java.awt.BorderLayout.NORTH);
		jContentPane.add(getJSplitPane(), java.awt.BorderLayout.CENTER);
		return jContentPane;
	}

	// @return javax.swing.JSplitPane
	private JSplitPane getJSplitPane() {
		jSplitPane = new JSplitPane();
		jSplitPane.setDividerLocation(330);
		jSplitPane.setRightComponent(getRightScrollPane());
		jSplitPane.setLeftComponent(getLeftScrollPane());
		jSplitPane.setOneTouchExpandable(true);

		return jSplitPane;
	}

	private JPanel getJPanel() {
		FlowLayout flowLayout = new FlowLayout();
		flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
		jPanel = new JPanel();
		jPanel.setBackground(new java.awt.Color(224, 255, 255));
		jPanel.setLayout(flowLayout);
		jPanel.setPreferredSize(new java.awt.Dimension(10, 30));
		jPanel.add(getOpenButton(), null);
		jPanel.add(getScanButton(), null);
		jPanel.add(getClearBar());

		return jPanel;
	}

	// return javax.swing.JMenuBar
	private JMenuBar getClearBar() {
		clearBar = new JMenuBar();
		clearBar.setBackground(new java.awt.Color(241, 255, 255));
		clearBar.add(getJMenu());
		return clearBar;
	}

	// return javax.swing.JScrollPane
	private JScrollPane getLeftScrollPane() {
		leftScrollPane = new JScrollPane();
		leftScrollPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("input"), BorderFactory
						.createEmptyBorder()));
		leftScrollPane.setBackground(new java.awt.Color(239, 255, 255));
		leftScrollPane.setViewportView(getInputTextArea());

		return leftScrollPane;
	}

	// return javax.swing.JTextArea
	private JTextArea getInputTextArea() {
		inputTextArea = new JTextArea();
		inputTextArea.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN,
				14));
		inputTextArea.addMouseListener(this);
		pm = new JPopupMenu();
		pm1 = new JMenuItem("剪切");
		pm1.setBackground(new java.awt.Color(241, 255, 255));
		pm2 = new JMenuItem("复制");
		pm2.setBackground(new java.awt.Color(241, 255, 255));
		pm3 = new JMenuItem("粘贴");
		pm3.setBackground(new java.awt.Color(241, 255, 255));
		pm.add(pm1);
		pm.add(pm2);
		pm.add(pm3);
		inputTextArea.add(pm);

		return inputTextArea;
	}

	// return javax.swing.JScrollPane
	private JScrollPane getRightScrollPane() {

		rightScrollPane = new JScrollPane();
		rightScrollPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("output"), BorderFactory
						.createEmptyBorder()));
		rightScrollPane.setBackground(new java.awt.Color(239, 255, 255));
		rightScrollPane.setViewportView(getOutTextArea());

		return rightScrollPane;
	}

	// @return javax.swing.JTextArea
	private JTextArea getOutTextArea() {

		outTextArea = new JTextArea();
		outTextArea
				.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 14));
		outTextArea.setEditable(false);

		return outTextArea;
	}

	// @return javax.swing.JButton
	private JButton getOpenButton() {

		openButton = new JButton();
		openButton.setText("Open");
		openButton.setBackground(new java.awt.Color(241, 255, 255));
		openButton.setFont(new java.awt.Font("DialogInput", java.awt.Font.BOLD,
				12));
		openButton.setBorderPainted(false);

		return openButton;
	}

	// return javax.swing.JButton
	private JButton getScanButton() {

		scanButton = new JButton();
		scanButton.setText("Analyze");
		scanButton.setBackground(new java.awt.Color(241, 255, 255));
		scanButton.setBorderPainted(false);

		return scanButton;
	}

	// return javax.swing.JMenu
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			// jMenu.setBackground(new java.awt.Color(241,255,255));
			jMenu.setText("Clear");
			jMenu.setFont(new java.awt.Font("DialogInput", java.awt.Font.BOLD,
					12));
			inputItem = new JMenuItem("Clear InputBox");
			inputItem.setBackground(new java.awt.Color(241, 255, 255));
			outputItem = new JMenuItem("Clear OutputBox");
			outputItem.setBackground(new java.awt.Color(241, 255, 255));
			allItem = new JMenuItem("Clear All");
			allItem.setBackground(new java.awt.Color(241, 255, 255));
			jMenu.add(inputItem);
			jMenu.add(outputItem);
			jMenu.add(allItem);
		}
		return jMenu;
	}

	/**
	 * This method initializes this
	 */
	private void initialize() {
		this.setBackground(new java.awt.Color(238, 238, 238));
		this.setBounds(new java.awt.Rectangle(160, 50, 673, 547));
		this.setContentPane(getJContentPane());
		this.setTitle("词法分析");
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		openButton.addActionListener(this);
		scanButton.addActionListener(this);
		inputItem.addActionListener(this);
		outputItem.addActionListener(this);
		allItem.addActionListener(this);
		pm1.addActionListener(this);
		pm2.addActionListener(this);
		pm3.addActionListener(this);
	}

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

	public void openFile() {
		int result = fileChooser.showOpenDialog(null);
		if (result == JFileChooser.APPROVE_OPTION) {
			try {
				File fileName = fileChooser.getSelectedFile();
				BufferedReader input = new BufferedReader(new FileReader(
						fileName));
				String line = input.readLine();
				inputTextArea.setText("");
				while (line != null) {
					inputTextArea.append(line + "\n");
					line = input.readLine();
				}
				input.close();
			} catch (IOException ioe) {
				System.out.println(ioe.toString());
			} catch (NullPointerException Ex) {
				System.out.println();
			} catch (Exception ex) {
				System.out.println();
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == openButton) {
			openFile();
		} else if (e.getSource() == scanButton) {
			String s = inputTextArea.getText();
			if (s.equals("")) {
				outTextArea
						.setText("ERROR: Please input strings in the leftbox!");
			} else {
				Ana scan = new Ana();
				scan.setBufferInput(s);
				try {
					scan.analysis();
					outTextArea.setText(scan.getOutput());
				} catch (Exception e1) {
					outTextArea.setText("ERROR: IOException");
				}
			}
		} else if (e.getSource() == inputItem)
			inputTextArea.setText("");
		else if (e.getSource() == outputItem)
			outTextArea.setText("");
		else if (e.getSource() == allItem) {
			inputTextArea.setText("");
			outTextArea.setText("");
		} else if (e.getSource() == pm1) {
			inputTextArea.cut();
		} else if (e.getSource() == pm2) {
			inputTextArea.copy();
		} else if (e.getSource() == pm3)
			inputTextArea.paste();
	}

	public void mouseReleased(MouseEvent e) {
		try {
			if (e.isPopupTrigger()) {
				pm.show(this, e.getX(), e.getY());
			}
		} catch (NullPointerException ex) {
		} catch (Exception exception) {
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public static void main(String[] args) {
		new WordAnalyze();
	}
}

⌨️ 快捷键说明

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