Your own java editor

Keywords: Java Eclipse

Making GUI Interface

New projects

First, double-click to open Eclipse on the desktop, wait for it to start, and click the New-> Java Project option in the menu File.

Fill in the name of the project MyEdit in the pop-up window and click the Finish button.

2 Create packages and classes

After the project is created, we need to create the classes according to the previous project structure. There are two categories in this project:

  • FileWindow: Major Method Class for GUI Interface and Logical Function Implementation
  • Main: Test class

So first we need to create a package called com.shiyanlou.myedit.

Right-click on the src folder of the created project directory, and then select New - > Package.

Fill in the package name com.shiyanlou.myedit in the pop-up New Java Package dialog box and click Finish button.

Finally, new FileWindow and Main classes are created under the new package.

Implementation of 3 GUI Interface

The GUI interface is shown as follows:

The interface is designed with Card Layout, white text field as program input area, pink text field as compiler result display area, and light blue text field as program operation result area. Click on the different function buttons above to display the corresponding text fields.

Write the code to implement GUI in FileWindow class. The relevant code is as follows. The explanation of the code will be in the form of annotations. Please pay attention to the annotations while writing the code.

Note: Neither of the following codes imported the relevant packages

 public class FileWindow extends JFrame implements ActionListener, Runnable {

    /*Note: Because the ActionListener and Runnable interfaces are implemented, it is necessary to implement the methods of these two interfaces. Here we first implement these two methods simply as follows. These two methods will be thoroughly completed in the next lesson.*/

    Thread compiler = null;
    Thread run_prom = null;
    boolean bn = true;
    CardLayout mycard;  //Declare layout, which will be used later
    File file_saved = null;
    JButton button_input_txt,   //Button Definition
            button_compiler_text,
            button_compiler,
            button_run_prom,
            button_see_doswin;

    JPanel p = new JPanel();
    JTextArea input_text = new JTextArea(); // Program input area
    JTextArea compiler_text = new JTextArea();// Compile error display area
    JTextArea dos_out_text = new JTextArea();// Output Information of Programs

    JTextField input_file_name_text = new JTextField();
    JTextField run_file_name_text = new JTextField();

    public FileWindow() {
        // TODO Auto-generated constructor stub
        super("Java Language Compiler");
        mycard = new CardLayout();
        compiler=new Thread(this);
        run_prom=new Thread(this);
        button_input_txt=new JButton("Program input area (white)");
        button_compiler_text=new JButton("Compile result area (pink)");
        button_see_doswin=new JButton("Program Running Results (Light Blue)");
        button_compiler=new JButton("Compiler");
        button_run_prom=new JButton("Running program");

        p.setLayout(mycard);//Setting up Card Layout
        p.add("input",input_text);//Define card name
        p.add("compiler", compiler_text);
        p.add("dos",dos_out_text);
        add(p,"Center");

        compiler_text.setBackground(Color.pink); //Setting colors
        dos_out_text.setBackground(Color.cyan);
        JPanel p1=new JPanel();

        p1.setLayout(new GridLayout(3, 3)); //Setting table layout
        //add component
        p1.add(button_input_txt);
        p1.add(button_compiler_text);
        p1.add(button_see_doswin);
        p1.add(new JLabel("Enter the name of the compiled file(.java): "));
        p1.add(input_file_name_text);
        p1.add(button_compiler);
        p1.add(new JLabel("Enter the application main class name"));
        p1.add(run_file_name_text);
        p1.add(button_run_prom);
        add(p1,"North");

        //Define events
        button_input_txt.addActionListener(this);
        button_compiler.addActionListener(this);
        button_compiler_text.addActionListener(this);
        button_run_prom.addActionListener(this);
        button_see_doswin.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e)
    {
         //Implementation Method

    }

    @Override
    public void run() {
        //Implementation Method
    }


    }

So far, our GUI interface is ready!

Implementation of 4 test classes

Next, let's do a test class to test our interface.

Main.java:

    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    public class Main {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            FileWindow win=new FileWindow();
            win.pack();
            win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });

            win.setBounds(200, 180,550,360);
            win.setVisible(true);
        }

    }

The interface and test class are complete.

Implementation of actionPerformed Method

First, let's implement the public void action Performed (Action Event) method. The comments in the code are explained in detail.

    public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==button_input_txt)
            {    //Display program input area
                mycard.show(p,"input");
            }
            else if(e.getSource()==button_compiler_text)
            {    //Display Compiler Result Display Area
                mycard.show(p,"compiler");
            }
            else if(e.getSource()==button_see_doswin)
            {    //Display the result area of program running
                mycard.show(p,"dos");
            }
            else if(e.getSource()==button_compiler)
            {    //If it's a compile button, execute the method of compiling the file
                if(!(compiler.isAlive()))
                {
                    compiler=new Thread(this);
                }
                try {
                    compiler.start();

                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }

                mycard.show(p,"compiler");

            }
            else if(e.getSource()==button_run_prom)
            {    //If it's a run button, how to execute the run file
                if(!(run_prom.isAlive()))
                {
                    run_prom=new Thread(this);
                }
                try {
                    run_prom.start();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
                mycard.show(p,"dos");
            }

        }

The above code is a comparison to determine which events need to be handled.

Implementation of 2 run method

Then there is a run() method, which is also the most important one. In this method, it is judged whether to compile or run:

  • If the current Thread is compiled, the code in the program input area will be saved in the current directory of the project as a. Java file, and the. java file just saved will be executed through the javac command to generate. class file. The compiled information will be output to the compiled result display area.

  • If the current Thread is running, the compiled. class file will be executed through the java command, and the result of the program will be displayed in the result area of the program running.

    public void run() {
             //TODO Auto-generated method stub
            if(Thread.currentThread()==compiler)
            {
            compiler_text.setText(null);
            String temp=input_text.getText().trim();
            byte [] buffer=temp.getBytes();
            int b=buffer.length;
            String file_name=null;
       file_name=input_file_name_text.getText().trim();

      try {
    file_saved=new File(file_name);
    FileOutputStream writefile=null;
    writefile=new FileOutputStream(file_saved);
    writefile.write(buffer, 0, b);
    writefile.close();
            } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("ERROR");
                }
    try {

    //Only by obtaining the error flow of the process can we know whether the result of the operation has failed or succeeded.
     Runtime rt=Runtime.getRuntime();
       InputStream in=rt.exec("javac "+file_name).getErrorStream(); //Call the javac command through Runtime. Note that the string "javac" has a space!!

        BufferedInputStream bufIn=new BufferedInputStream(in);

        byte[] shuzu=new byte[100];
        int n=0;
        boolean flag=true;

        //Input error information        
    while((n=bufIn.read(shuzu, 0,shuzu.length))!=-1)
        {
            String s=null;
              s=new String(shuzu,0,n);
            compiler_text.append(s);
            if(s!=null)
                        {
                            flag=false;
                        }
            }
                    //Determine whether the compilation is successful
                    if(flag)
                    {
                        compiler_text.append("Compile Succeed!");
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
    else if(Thread.currentThread()==run_prom)
        {
            //Run the file and output the result to dos_out_text

        dos_out_text.setText(null);

        try {
              Runtime rt=Runtime.getRuntime();
    String path=run_file_name_text.getText().trim();
    Process stream=rt.exec("java "+path);//Calling java commands

    InputStream in=stream.getInputStream();
                    BufferedInputStream bisErr=new BufferedInputStream(stream.getErrorStream());
                    BufferedInputStream bisIn=new BufferedInputStream(in);

    byte[] buf=new byte[150];
    byte[] err_buf=new byte[150];

    @SuppressWarnings("unused")
    int m=0;
    @SuppressWarnings("unused")
    int i=0;
    String s=null;
    String err=null;

    //Print compilation information and error information
    while((m=bisIn.read(buf, 0, 150))!=-1)
                    {
                        s=new String(buf,0,150);
                        dos_out_text.append(s);
                    }
                                  while((i=bisErr.read(err_buf))!=-1)
                    {
                    err=new String(err_buf,0,150);
                    dos_out_text.append(err);
                    }
        }
         catch (Exception e) {
                    // TODO: handle exception
                    }
        }
     }

3. Simple testing

Clicking on the compilation button will cause an error message, proving that success is not far away.

Error running button:

Click on the button in the program input area (white), write a simple test program! The code is as follows:

    class a
    {
        public static void main(String [] args)
        {
            System.out.println("Hello ShiYanLou");
        }
    }

Then in the text box after entering the compiled file name (. java), fill in the same. java file as the class name, such as a.java, and click on the compiler.

If there are no errors in the program, the compiled results are shown as follows:

Fill in the text box after the main class name of the application, such as a, and click Run the program.

The results of the program will be displayed in the light blue text field.

When re-editing, you need to click the button in the program input area (white), and enter in the white text field.



Posted by robot43298 on Wed, 17 Jul 2019 11:19:35 -0700