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

📄 lexicalframe.java

📁 一个简单有效的CMM语言的词法分析器,带图形界面
💻 JAVA
字号:
package com.lexical.src;import com.lexical.src.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class LexicalFrame extends JFrame{	public LexicalFrame()	{		setTitle("LexicalAnalyer");		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);		contentPane = getContentPane();		//initial something		lexicalBuffer = new LexicalBuffer();		lexicalPanel = new LexicalPanel(DEFAULT_WIDTH,DEFAULT_HEIGHT);		lexicalThread = new LexicalThread(lexicalPanel);		lexicalBuffer.setThread(lexicalThread);		lexicalBuffer.setPanel(lexicalPanel);		contentPane.add(lexicalPanel,BorderLayout.CENTER);		JPanel basePanel = new JPanel();		fileNameField =  new JTextField(10);		startButton = new JButton("analy");		startButton.addActionListener(new StartListener());		speedComboBox = new JComboBox();		speedComboBox.addItem("slow");		speedComboBox.addItem("middle");		speedComboBox.addItem("fast");		speedComboBox.addItem("nothing");		speedComboBox.addActionListener(new SpeedActionListener());		basePanel.add(new JLabel("fileName"));		basePanel.add(fileNameField);		basePanel.add(startButton);		basePanel.add(new JLabel("speed is"));		basePanel.add(speedComboBox);		menuBar = new JMenuBar();		setJMenuBar(menuBar);		//file menu		JMenu fileMenu = new JMenu("Analyer");		fileMenu.add(new				AbstractAction("退出")				{					public void actionPerformed(ActionEvent event)					{						System.exit(0);					}		});		//operation menu				//add lookandfeel		JMenu  setMenu = new JMenu("Operation");		ButtonGroup lookAndFeelGroup = new ButtonGroup();				JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("metal");		JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("motif");		metalItem.setSelected(true);		lookAndFeelGroup.add(motifItem);		lookAndFeelGroup.add(metalItem);		metalItem.addActionListener(new LookAndFeelActionListener());		motifItem.addActionListener(new LookAndFeelActionListener());				JMenu lookAndFeelMenu = new JMenu("LookAndFeel");		lookAndFeelMenu.add(metalItem);		lookAndFeelMenu.add(motifItem);				//add pointer show		ButtonGroup pointerShowGroup = new ButtonGroup();		JRadioButtonMenuItem  bothItem = new JRadioButtonMenuItem("both");		bothItem.setSelected(true);		JRadioButtonMenuItem  neitherItem = new JRadioButtonMenuItem("neither");		JRadioButtonMenuItem  onlyForwardItem = new JRadioButtonMenuItem("only forward");		JRadioButtonMenuItem  onlyBeginningItem = new JRadioButtonMenuItem("only beginning");				PointerShowListener pointerShowListener = new PointerShowListener();		bothItem.addActionListener(pointerShowListener);		neitherItem.addActionListener(pointerShowListener);		onlyForwardItem.addActionListener(pointerShowListener);		onlyBeginningItem.addActionListener(pointerShowListener);				pointerShowGroup.add(bothItem);		pointerShowGroup.add(neitherItem);		pointerShowGroup.add(onlyForwardItem);		pointerShowGroup.add(onlyBeginningItem);				JMenu pointerShowMenu = new JMenu("Pointer");		pointerShowMenu.add(bothItem);		pointerShowMenu.add(neitherItem);		pointerShowMenu.add(onlyForwardItem);		pointerShowMenu.add(onlyBeginningItem);				setMenu.add(lookAndFeelMenu);		setMenu.add(pointerShowMenu);		//help menu		JMenu helpMenu = new JMenu("Help");		JMenuItem aboutItem = new JMenuItem("关于");		aboutItem.addActionListener(new AboutActionListener());		JMenuItem helpItem = new JMenuItem("帮助");		helpItem.addActionListener(new HelpActionListener());		helpMenu.add(helpItem);		helpMenu.add(aboutItem);		//add to menubar		menuBar.add(fileMenu);		menuBar.add(setMenu);		menuBar.add(helpMenu);		//JPanel northPanel = new JPanel();		//aboutButton = new JButton("关于");		//aboutButton.addActionListener(new AboutActionListener());		//northPanel.add(aboutButton,FlowLayout.LEFT);		contentPane.add(basePanel,BorderLayout.SOUTH);		//contentPane.add(northPanel,BorderLayout.NORTH);	}	public static final int DEFAULT_WIDTH = 500;	public static final int DEFAULT_HEIGHT = 300;	private LexicalPanel lexicalPanel;    private Container contentPane;	private LexicalBuffer lexicalBuffer;	private JButton startButton;	private JButton aboutButton;	private JMenuBar menuBar;	private JComboBox speedComboBox;	private JTextField fileNameField;	private LexicalAnalyer lexicalAnalyer;	private LexicalThread lexicalThread;	private class StartListener implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			String fileName = fileNameField.getText().trim();			lexicalThread.setFileName(fileName);			Thread thread = new Thread(lexicalThread);			thread.start();		}	}	private class SpeedActionListener implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			String speed =(String)speedComboBox.getSelectedItem();			if(speed.equals("slow"))			{				lexicalThread.setSleepTime(1000);			}			else if(speed.equals("middle"))			{				lexicalThread.setSleepTime(500);			}			else if(speed.equals("fast"))			{				lexicalThread.setSleepTime(250);			}			else if(speed.equals("nothing"))			{				lexicalThread.setSleepTime(1);			}			else{}		}	}	private class AboutActionListener implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			AboutFrame frame = new AboutFrame();			//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			frame.show();		}	}	private class HelpActionListener implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			HelpFrame frame = new HelpFrame();			frame.show();		}	}	private class LookAndFeelActionListener implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			String text =( (JRadioButtonMenuItem)event.getSource()).getText();			String lookAndFeel="";			if(text.equals("motif"))				lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";			else if(text.equals("metal"))				lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";			else ;			try{				UIManager.setLookAndFeel(lookAndFeel);				SwingUtilities.updateComponentTreeUI(LexicalFrame.this);			}catch(Exception e){}		}	}	private class PointerShowListener implements ActionListener	{		public void actionPerformed(ActionEvent event)		{			String text =( (JRadioButtonMenuItem)event.getSource()).getText();			if(text.equals("both"))			{				lexicalPanel.setShowFowardPointerIndex(true);				lexicalPanel.setShowBeginningPointerIndex(true);			}			else if(text.equals("neither"))			{				lexicalPanel.setShowFowardPointerIndex(false);				lexicalPanel.setShowBeginningPointerIndex(false);			}			else if(text.equals("only forward"))			{				lexicalPanel.setShowFowardPointerIndex(true);				lexicalPanel.setShowBeginningPointerIndex(false);			}			else if(text.equals("only beginning"))			{				lexicalPanel.setShowFowardPointerIndex(false);				lexicalPanel.setShowBeginningPointerIndex(true);			}			else;		}	}}

⌨️ 快捷键说明

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