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

📄 main.java

📁 编译原理的FOR循环语句的翻译
💻 JAVA
字号:

import java.awt.BorderLayout;
import java.awt.Container;
//import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;


public class Main extends JFrame implements ActionListener{

	/**
	 * @param args
	 */
	JButton jbOk=new JButton("翻译");
	JButton jbBrower=new JButton("浏览");
	JButton jbDisplay=new JButton("显示");
	JTextArea jtaSource=new JTextArea();
	JTextArea jtaResult=new JTextArea();
	JScrollPane jspSource=new JScrollPane(jtaSource);
	JScrollPane jspResult=new JScrollPane(jtaResult);
	JPanel jpSouth=new JPanel();
	Container con=null;
	JPanel jpCenter=new JPanel();
	JLabel jlSource=new JLabel("源程序:");
	JLabel jlResult=new JLabel("结果:");
	JPanel jpSource=new JPanel();
	JPanel jpResult=new JPanel();
	Main(){
		this.setTitle("for语句的翻译程序");
		this.setSize(450,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		Toolkit tk=this.getToolkit();
		Dimension screenSize=tk.getScreenSize();
		Dimension frameSize=this.getSize();
		this.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
		this.setVisible(true);
		//jtaSource.setEditable(false);
		jtaResult.setEditable(false);
		con=this.getContentPane();
		jpSource.setLayout(new BorderLayout());
		jpResult.setLayout(new BorderLayout());
		jpSource.add(jlSource,BorderLayout.NORTH);
		jpSource.add(jspSource);
		jpResult.add(jlResult,BorderLayout.NORTH);
		jpResult.add(jspResult);
		jpCenter.setLayout(new GridLayout(1,2));
		jpCenter.add(jpSource);
		jpCenter.add(jpResult);
		jpSouth.add(jbOk);
		jpSouth.add(jbBrower);
		jpSouth.add(jbDisplay);
		con.add(jpCenter);
		con.add(jpSouth,BorderLayout.SOUTH);
		con.validate();
		jbOk.addActionListener(this);
		jbBrower.addActionListener(this);
		jbDisplay.addActionListener(this);
		jbDisplay.setEnabled(false);
		jtaSource.setEditable(false);
		jtaResult.setEditable(false);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new Main();

	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==jbOk){
			
			String s=jtaSource.getText();
			Compile compile=new Compile(s,this);
			compile.start();
			
		}else if(e.getSource()==jbBrower){
			jbDisplay.setEnabled(false);
			JFileChooser jfc=new JFileChooser();
			jfc.setCurrentDirectory(new File("."));
			jfc.addChoosableFileFilter(new FileFilter(){

				public boolean accept(File f) {
					// TODO Auto-generated method stub
					return true;
				}

				public String getDescription() {
					// TODO Auto-generated method stub
					return "所有文件(*.*)";
				}
				
			});
			jfc.addChoosableFileFilter(new FileFilter(){

				public boolean accept(File f) {
					if(f.getName().endsWith(".txt")){
						return true;
					}
					return false;
				}

				public String getDescription() {
					// TODO Auto-generated method stub
					return "*.txt";
				}
				
			});
			int result=jfc.showOpenDialog(this);
			if(result==jfc.CANCEL_OPTION){
				return;
			}
			jtaSource.setText("");
			File file=jfc.getSelectedFile();
			
			try {
				String s=null;
				BufferedReader bb=new BufferedReader(new FileReader(file));
				while((s=bb.readLine())!=null){
					jtaSource.append(s+'\n');
				}
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			catch(IOException e2){
				e2.printStackTrace();
			}
		}
		else if(e.getSource()==jbDisplay){
			display();
		}
		
	}
	
	private void display(){
		File file=new File("output.txt");
		String path=file.getAbsolutePath();
		
		try {
			Runtime.getRuntime().exec("notepad "+path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*Desktop desktop=Desktop.getDesktop();
		if(desktop.isDesktopSupported()&&desktop.isSupported(Desktop.Action.OPEN)){
			try {
				desktop.open(file);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
	}

}

⌨️ 快捷键说明

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