form1.cs

来自「This inspired me to make this tool. This」· CS 代码 · 共 135 行

CS
135
字号
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;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFile();
            CompileJava();

        }
        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            txtOutput.Text = e.Data;
        }

        private bool Execute(string filename, string arguments)
        {
            bool flag = true;
            Process p = new Process();
            p.StartInfo.FileName = filename;
            p.StartInfo.Arguments = arguments;

            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.ErrorDialog = false;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.WaitForExit();
            
            string err = p.StandardError.ReadToEnd();
            string output =  p.StandardOutput.ReadToEnd();
            string str = err + output;
            if (output.Length == 0)
            {
                if (err.Length > 0)
                {
                    str += "Status: Failed";
                    flag = false;
                }
                else
                {
                    str += "Status: Success";
                }
            }


            txtOutput.SelectionLength = txtOutput.Text.Length;
            txtOutput.SelectedText = str + "\r\n--------------------------------------\r\n";

            return flag;
        }

        private bool CompileJava()
        {
            return Execute(@"javac.exe", "test.java");
        }

        private bool Runjava()
        {
            return Execute(@"java.exe", "test");
        }

        private void SaveFile()
        {
            StreamWriter str = File.CreateText("test.java");
            str.WriteLine(txtCode.Text);
            str.Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (File.Exists("test.class"))
            {
                Runjava();
            }
            else
            {
                txtOutput.SelectionLength = txtOutput.Text.Length;
                txtOutput.SelectedText = "Compile first!" + "\r\n--------------------------------------\r\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (CompileJava())
                Runjava();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (!CheckIfJavaInstalled())
            {
                btnCompile.Enabled = btnRun.Enabled = btnCompileRun.Enabled = false;
                txtOutput.SelectionLength = txtOutput.Text.Length;
                txtOutput.SelectedText = "Java not installed or not found in the PATH varilable." + "\r\n--------------------------------------\r\n";
            }
            txtCode.SelectionStart = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string msg = @"This is a quick hobby program. So please do not look with 'professional' eye. Use this for educational purpose. I have no plans for further development unless the crowd request\n\n
Programmed by : Praveen (ninethsesne@hotmail.com)";
            MessageBox.Show(msg, "Java Learner IDE");
        }

        private bool CheckIfJavaInstalled()
        {
            bool flag = true;
            if (Environment.GetEnvironmentVariable("path").ToLower().IndexOf("java") == -1)
            {
                flag = false;
            }
            return flag;
        }

 
    }
}

⌨️ 快捷键说明

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