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

📄 form1.cs

📁 Java开发的一个编译器,有词法分析
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace java
{
    public partial class Form1 : Form
    {
        //dos命令变量
        string pathall;
        string path;
        string panfu = "G:";
        string javac;
        string java;
        string[] str=new string[7];

        public bool flag = true;
        public bool IsSave = true;
        public Form1()
        {
            InitializeComponent();
        }

        string filename="";
        bool IsNew = true;
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Text = "java1";
            IsNew = true;
            richTextBox1.Enabled = true;
            flag = false;
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Enabled=true;
            IsNew = false;
            try
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    richTextBox1.Clear();
                    filename = this.openFileDialog1.FileName;
                    richTextBox1.LoadFile(filename,RichTextBoxStreamType.PlainText);
                    this.Text = filename;
                    flag = true;
                }
                IsSave = true;
            }
            catch (ArgumentException ae)
            {
                if (ae.Message == "Invalid file format.")
                    MessageBox.Show("There was an error loading the file: " + openFileDialog1.FileName);
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (flag==true)
                {
                    if (!IsNew)
                    {
                        saveFileDialog1.FileName = filename;
                    }
                    richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
                }
                else
                {
                    if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName.Length > 0)
                    {
                        richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                        flag = true;
                        this.Text = saveFileDialog1.FileName;
                    }
                }
                IsSave = true;
            }
            catch (ObjectDisposedException)
            {
                MessageBox.Show("没有文件保存");
            }
        }

        private void 编译ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pathall = filename.ToString();
            str = pathall.Split('\\');
            path = str[0];
            int i;
            for (i = 1; i < str.Length; i++)
            {
                if (str[i] == null)
                {
                    break;
                }
            }
            javac = str[i - 1];
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "cmd.exe";

            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(path);
            
            p.StandardInput.WriteLine("cd "+pathall);
            p.StandardInput.WriteLine("javac "+javac);
            p.Close();
        }

        private void 执行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] str2 = new string[2];
            str2 = javac.Split('.');
            java = str2[0];
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "cmd.exe";

            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            //p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine("java "+java);
            p.StartInfo.RedirectStandardInput = false;
            p.Close();
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (IsSave==false)
            {
                switch (MessageBox.Show("是否要保存文档?\n\n" + this.Text, "关闭", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
                {
                    case DialogResult.Yes:
                        this.保存ToolStripMenuItem_Click(sender,e);
                        break;
                    case DialogResult.Cancel:
                        break;
                }
            }
            richTextBox1.Clear();
            richTextBox1.Enabled = false;
            flag = true;
            this.Text = "java编译器";
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            IsSave = false;
        }
    }
}

⌨️ 快捷键说明

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