简单的Java编辑器GUI

时间:2014-02-27 23:13:55

标签: java

您好我为简单的Java编辑器

创建了GUI代码

我创建菜单但我需要匹配 文件:新建:创建新文件。询问文件的名称(以及公共类)以及存储它的目录。随着文件的创建被插入到公共类的结构中,例如公共类MyClass {}。

打开:打开包含源代码java(.java)的文件。 保存:将当前片段保存在创建期间建立的同一文件上。 另存为:显示您请求文件名称的对话框,以及存储文件名称的目录格式。格式为java文件(.java)。窗口的主要部分将包含用户用来编辑文件源Java的编辑器。

窗口的主要部分将包含用户用来编辑文件源Java的编辑器。在处理代码段期间将更新的信息:数字行java源代码中的保留字数

格式化文字


每个文件都将打开格式化,并在按照以下规则处理时格式化:java的保留字将显示为蓝色。 评论将以绿色显示 字符串文字与橙色 所有其他黑色 字体将是Courier字体大小12pt

我会提供GUI代码,任何人都可以帮我解决上述问题吗?

此致 安东尼

// ClassEEFrame

package editor.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class EEFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = -1709009137090877913L;
    private GridBagLayout layout;
    private GridBagConstraints constraints;
    private EEMenuBar menuBar;
    private EETextPane editor;
    private EEConsole console;
    private EEStatusBar statusBar;
    private File file;

    public EEFrame() throws HeadlessException {
        super("Elearn Editor");

        JScrollPane scrollPane;

        layout = new GridBagLayout();
        setLayout(layout);

        constraints = new GridBagConstraints();

        menuBar = new EEMenuBar();
        setJMenuBar(menuBar);

        editor = new EETextPane();

        scrollPane = new JScrollPane(editor);
        scrollPane.setBorder(new TitledBorder("Editor"));

        setConstraints(1, 100, GridBagConstraints.BOTH);
        addComponent(scrollPane, 0, 0, 1, 1);

        console = new EEConsole();

        scrollPane = new JScrollPane(console);
        scrollPane.setBorder(new TitledBorder("Console"));

        setConstraints(1, 40, GridBagConstraints.BOTH);
        addComponent(scrollPane, 1 ,0 ,1, 1);

        statusBar = new EEStatusBar();
        setConstraints(1, 0, GridBagConstraints.BOTH);
        addComponent(statusBar, 2, 0, 1, 1);

        file = null;
    }

    public JTextPane getTextPane() {
        return this.editor;
    }

    public void setLines(int lines) {
        this.statusBar.setLines(lines);
    }

    public void setWords(int words) {
        this.statusBar.setJavaWords(words);
    }

    private void setConstraints(int weightx, int weighty, int fill) {
        constraints.weightx = weightx;
        constraints.weighty = weighty;
        constraints.fill = fill;
    }

    private void addComponent(Component component, int row, int column, int width, int height) {
        constraints.gridx = column;
        constraints.gridy = row;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        layout.setConstraints(component, constraints);
        add(component);
    }

    private class EEMenuBar extends JMenuBar {

        /**
         * 
         */
        private static final long serialVersionUID = -2176624051362992835L;
        private JMenu fileMenu, compilationMenu;
        private JMenuItem newItem, openItem, saveItem, saveAsItem, exportItem, compileProcessItem, compileClassItem;

        public EEMenuBar() {
            super();

            fileMenu = new JMenu("File");

            newItem = new JMenuItem("New");

            newItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    /* TODO Dispay dialog with inputs class name and file path */
                }

            });

            fileMenu.add(newItem);

            openItem = new JMenuItem("Open");

            openItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    /*TODO Open existing java source file*/

                }

            });

            fileMenu.add(openItem);

            saveItem = new JMenuItem("Save");
            saveItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    /*TODO save changes to file*/                                       
                }

            });

            fileMenu.add(saveItem);

            saveAsItem = new JMenuItem("Save As");

            saveAsItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    /*TODO Save as new java source file*/
                }               
            });

            fileMenu.add(saveAsItem);

            exportItem = new JMenuItem("Export to pdf");

            exportItem.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO save as pdf(formatted)

                }
            });

            fileMenu.add(exportItem);           

            add(fileMenu);

            compilationMenu = new JMenu("Compilation");

            compileProcessItem = new JMenuItem("Compile with system jdk");

            compileProcessItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    /*TODO Compile java source code and show results in teminal(inside editor)*/
                }

            });

            compilationMenu.add(compileProcessItem);

            compileClassItem = new JMenuItem("Compile with JavaCompiler Class");

            compileClassItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    /*TODO Call system compiler for file*/
                }
            });

            compilationMenu.add(compileClassItem);

            add(compilationMenu);

        }
    }

    private class EETextPane extends JTextPane {

        /**
         * 
         */
        private static final long serialVersionUID = -7437561302249475096L;

        public EETextPane() {
            super();

            //add styles to document
            Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
            StyleConstants.setForeground(def, Color.BLACK);
            StyleConstants.setFontFamily(def, "Courier");
            StyleConstants.setFontSize(def, 12);

            Style keyword = addStyle("keyword", def);
            StyleConstants.setForeground(keyword, Color.BLUE);

            Style literal = addStyle("literal", def);
            StyleConstants.setForeground(literal, Color.ORANGE);

            Style comment = addStyle("comment", def);
            StyleConstants.setForeground(comment, Color.GREEN);
        }
    }

    private class EEConsole extends JTextPane {

        /**
         * 
         */
        private static final long serialVersionUID = -5968199559991291905L;

        public EEConsole() {
            super();

            //add styles to document
            Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
            StyleConstants.setForeground(def, Color.BLACK);
            StyleConstants.setFontFamily(def, "Courier");
            StyleConstants.setFontSize(def, 12);

            Style keyword = addStyle("error", def);
            StyleConstants.setForeground(keyword, Color.RED);

            Style literal = addStyle("success", def);
            StyleConstants.setForeground(literal, Color.GREEN);
        }

    }

    private class EEStatusBar extends JPanel {

        /**
         * 
         */
        private static final long serialVersionUID = 185007911993347696L;
        private BoxLayout layout;
        private JLabel linesLabel, lines, wordsLabel, words;

        public EEStatusBar() {
            super();

            layout = new BoxLayout(this, BoxLayout.X_AXIS);
            setLayout(layout);

            linesLabel = new JLabel("Lines : ");
            linesLabel.setAlignmentX(LEFT_ALIGNMENT);
            add(linesLabel);

            lines = new JLabel("");
            lines.setAlignmentX(LEFT_ALIGNMENT);
            add(lines);

            add(Box.createRigidArea(new Dimension(10,10)));

            wordsLabel = new JLabel("Java Words : ");
            wordsLabel.setAlignmentX(LEFT_ALIGNMENT);
            add(wordsLabel);

            words = new JLabel("");
            words.setAlignmentX(LEFT_ALIGNMENT);
            add(words);
        }

        public void setLines(int lines) {
            /*TODO set line numbers */
        }

        public void setJavaWords(int words) {
            /*TODO set java keyword numbers*/
        }
    }

}


//class Main 

package editor.app;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import elearning.editor.gui.EEFrame;
import elearning.editor.util.EECodeFormater;
import elearning.editor.util.EEJavaWordCounter;
import elearning.editor.util.EELineCounter;

public class EEditor {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            // Set cross-platform Java L&F (also called "Metal")
            //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

            //Set Motif L&F
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

            //Set Nimbus L&F
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

            //Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            //Set GTK L&F --> Same as System L&F on Linux and Solaris\
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");

            //Set Windows L&F --> Same as System L&F on Windows
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } 
        catch (UnsupportedLookAndFeelException e) {
            // handle exception
        }
        catch (ClassNotFoundException e) {
            // handle exception
        }
        catch (InstantiationException e) {
            // handle exception
        }
        catch (IllegalAccessException e) {
            // handle exception
        }

        EEFrame frame = new EEFrame();

        frame.setSize(500, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        /* TODO Instatiate Threads */


        /*TODO Start Threads */

    }

}

我也提供了一个模型:

Mockup

2 个答案:

答案 0 :(得分:0)

首先你应该看一下File课程。它为您提供了创建,打开,修改和保存文件的方法。要阅读文件,您可能还想给BufferedReader或任何其他阅读器一个镜头。

  • 创建文件:File的方法为createNewFile(),与exists()结合使用。
  • 要打开并阅读文件,请使用try-with-resource(Java手册中实际上有一个很好的tutorial)。
  • 要保存文件,您应该查看FileWriter,它可以编写字符串或将它们附加到文件中。
  • 对于您的编辑器,您可能希望将前面提到的BufferedReader替换为LineReader,其中还提供了获取行号的方法。除此之外,你必须弄清楚如何编号线。 (实际上它只是在谷歌上搜索,会有一些像this one这样的想法 - 我没有详细检查但它可能有帮助。)
  • 当然对于编辑器,您应首先将文件读入字符串,为其使用格式化程序,然后您可以呈现它并在需要时重新格式化。

除了这些提示之外,我无法为您提供更详细的答案,因为您还可以在评论中阅读,如果您提供更详细的问题会更容易。你现在给了我们一个与你的实际问题几乎没有关系的GUI 向我们展示你的一些(有问题的)工作,我们可以帮助你,但除此之外我们做的不仅仅是给你一些提示。因此,请尝试考虑您的问题,尝试如何要求更准确的答案,并在需要时打开一些新问题 但是不要忘记查看网站上的答案,对我来说,几乎我想问的所有问题都已经以类似的方式问到了某个地方。

答案 1 :(得分:0)

Hello再次允许将工作拆分为步骤,

首先我想创建新的,打开,保存,另存为,导出到pdf菜单和事件

下面是我使用的代码,GUI框架正确打开,新建,打开,保存,另存为,导出为pdf标签,但操作没有发生。

有人可以给我写正确的代码吗?请记住,我是非常初学者。

public EEMenuBar() {
        super();

        fileMenu = new JMenu("File");
        //Create the New menu item
        newItem = new JMenuItem("New");
        newItem.setMnemonic(KeyEvent.VK_N);
        newItem.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {

            }

        });

        fileMenu.add(newItem);

        openItem = new JMenuItem("Open");
        openItem.setMnemonic(KeyEvent.VK_0);
        openItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                /*TODO Open existing java source file*/

            }

        });

        fileMenu.add(openItem);

        saveItem = new JMenuItem("Save");
        saveItem.setMnemonic(KeyEvent.VK_S);
        saveItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*TODO save changes to file*/                                       
            }

        });

        fileMenu.add(saveItem);

        saveAsItem = new JMenuItem("Save As");
        saveAsItem.setMnemonic(KeyEvent.VK_A);
        saveAsItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                /*TODO Save as new java source file*/
            }               
        });

        fileMenu.add(saveAsItem);

        exportItem = new JMenuItem("Export to pdf");
        exportItem.setMnemonic(KeyEvent.VK_E);
        exportItem.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO save as pdf(formatted)

            }
        });

        fileMenu.add(exportItem);