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

📄 mainframe.java

📁 本程序是用JAVA写的
💻 JAVA
字号:
package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;


public class MainFrame extends JFrame{
	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;
	private JPanel topPane;
	private JPanel midPane;
	private JPanel mainPane;
	private JButton openBtn;
	private JButton scanBtn;
	private JButton wordAnlzBtn;
	private JButton gramAnlzBtn;
	private JButton quitBtn;
	private JTextArea codeArea;
	private JTextArea targetArea;
	private JScrollPane cscoll;
	private JScrollPane tscoll;
	private JToolBar topTool;
	private JFileChooser jfc;
	private JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	public MainFrame(){
		this.openBtn = new JButton("打开");
		this.scanBtn = new JButton("扫描");
		this.wordAnlzBtn = new JButton("词法分析");
		this.gramAnlzBtn = new JButton("语法分析");
		this.quitBtn = new JButton("退出");
		this.topTool = new JToolBar();
		this.topTool.setLayout(new GridLayout(1, 5, 5, 15));
		this.topPane = new JPanel(new BorderLayout(2, 2));
		this.topPane.setBorder(BorderFactory.createEtchedBorder());
		this.midPane = new JPanel(new BorderLayout());
		this.midPane.setBorder(BorderFactory.createEtchedBorder());
		this.cscoll = new JScrollPane();
		this.tscoll = new JScrollPane();
		this.codeArea = new JTextArea();
		this.targetArea = new JTextArea();
		targetArea.setEditable(false);
		this.jfc = new JFileChooser();
		this.inital();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	
	public void inital(){
		try {
//			init container
			Container con = this.getContentPane();
			mainPane = new JPanel(new BorderLayout());
			mainPane.setBorder(BorderFactory.createEtchedBorder());
			con.add(mainPane);
			
//			init top
			this.openBtn.setPreferredSize(new Dimension(50, 50));
			this.topTool.add(openBtn);
			this.topTool.add(scanBtn);
			this.topTool.add(wordAnlzBtn);
			this.topTool.add(gramAnlzBtn);
			this.topTool.add(quitBtn);
			this.topPane.add(topTool);
			
//			init center
			codeArea.setFont(new java.awt.Font("Dialog", Font.BOLD, 16));
			targetArea.setFont(new java.awt.Font("Dialog", Font.BOLD, 16));
			this.cscoll.getViewport().add(codeArea);
			this.tscoll.getViewport().add(targetArea);
			splitPane.setLeftComponent(cscoll);
			splitPane.setRightComponent(tscoll);
			this.midPane.add(splitPane,"Center");
//			this.midPane.add(this.tscoll);
			

//			add all pane to mainPane
			mainPane.add(topPane,"North");
			mainPane.add(midPane,"Center");	
			
			
			openBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openBtn_actionPerformed(e);
				}
			});
			
			quitBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					quitBtn_actionPerformed(e);
				}
			});
			scanBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					scanBtn_actionPerformed(e);
				}
			});


		} catch (Exception e) {
		
		}
		
	}
	//打开文件事件
	public void openBtn_actionPerformed(ActionEvent e) {
		this.toOpenFile();
	}
	//退出事件 
	public void quitBtn_actionPerformed(ActionEvent e){
		System.exit(0);
	}
	//扫描事件
	public void scanBtn_actionPerformed(ActionEvent e) {
		WA ioword = new WA();
		ioword.setTemp(codeArea.getText().trim());
		targetArea.setText(ioword.scan());
		
	}
	//词法分析事件
	public void wordAnlzBtn_actionPerformed(ActionEvent e) {
		    
	    this.wordsAnalyse();
		
	}
	//扫描
	public void scanFile(){
		String temp = codeArea.getText();		
		//targetArea.setText(ioword.scan());	
		
		}
	//词法分析
	public void wordsAnalyse(){
		String temp = codeArea.getText();
//		targetArea.setText(ioword.analyse());
			
		}
	
	/**
	 * 打开文件操作函数.
	 */
	public void toOpenFile() {
		String openFileName = new String();
//		判断是否选择文件.jfc.showOpenDialog(this)打开一个选择文件的窗体.
		if (JFileChooser.APPROVE_OPTION == jfc.showOpenDialog(this)) {
			try {
//				获取文件的路径
				openFileName = jfc.getSelectedFile().getPath();
//				告知已经打开一个文件
				File file = new File(openFileName);
				int num = 0;
				int fileLength = (int) file.length();
				char[] data = new char[fileLength];
				FileReader fReader = new FileReader(file);
				while (fReader.ready()) {
					num += fReader.read(data, num, fileLength - num);
				}
				fReader.close();
				this.codeArea.setText(new String(data, 0, num));
				this.targetArea.setText("");
			} catch (FileNotFoundException ex) {
				JOptionPane.showMessageDialog(null, "抱歉,文件打开失败,文件未找到!");
			} catch (IOException ex1) {
				JOptionPane.showMessageDialog(null, "抱歉,文件打开失败,文件异常!");
			}
		}

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame mf = new MainFrame();
		mf.setSize(500,600);
//		center window
		mf.setLocationRelativeTo(null);
		mf.setVisible(true);
		mf.setTitle("Compile Design");
		
	}

}

⌨️ 快捷键说明

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