簡體   English   中英

簡單的Java編輯器GUI

[英]Simple Java editor GUI

您好,我為簡單的Java編輯器創建了GUI代碼

我創建菜單,但是我需要匹配文件:新建:創建一個新文件。 詢問文件的名稱(以及公共類),以及文件的存儲目錄。 隨着文件的創建,將插入到公共類的結構中,例如,公共類MyClass {}。

打開:使用源代碼Java(.java)打開文件。 保存:將當前代碼段與創建過程中建立的代碼段保存在同一文件中。 另存為:顯示一個對話框,您在其中請求文件名,文件的存儲目錄格式。 格式為Java文件(.java)。 窗口的主要部分將具有供用戶用來編輯文件源Java的編輯器。

窗口的主要部分將具有供用戶用來編輯文件源Java的編輯器。 在代碼段處理過程中將更新的信息:數字行Java源代碼中的保留字數

格式化文字


每個文件都將打開格式,並按照以下規則進行處理時將進行格式化:java的保留字將顯示為藍色。 注釋將顯示為綠色The String Literals with orange其他所有黑色的字體將為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 */

    }

}

我也提供一個樣機:

小樣

首先,您應該看一下File類。 它為您提供創建,打開,修改和保存文件的方法。 要讀取文件,您可能還想給BufferedReader或任何其他Reader拍照。

  • 創建文件: File有方法createNewFile()結合使用它exists()
  • 要打開和讀取文件,請使用try-with-resource(Java手冊中實際上有一個不錯的教程 )。
  • 要保存文件,您應該檢出FileWriter ,它可以編寫字符串或將它們附加到文件中。
  • 對於您的編輯器,您可能想用LineReader替換前面提到的BufferedReader ,該方法還提供了獲取行號的方法。 除此之外,您還必須弄清楚如何對行進行編號。 (其實這只是谷歌搜索周圍很多,會有一些想法像這一個 -我沒有詳細檢查,但它可能會幫助)。
  • 當然,對於編輯器,您應該首先將文件讀取為字符串,使用格式化程序,然后可以顯示它並在需要時重新格式化。

除了這些提示之外,我無法為您提供更詳細的答案,因為您還可以閱讀評論,所以如果您提供更詳細的問題會更容易。 您現在只給了我們一個GUI,它幾乎與您的實際問題無關。
向我們展示您的一些(有問題的)工作,我們可以為您提供幫助,但是除此之外,我們只能做一些提示,就像我剛才所做的那樣。 因此,請嘗試考慮您的問題,嘗試如何尋求更精確的答案,並在需要時提出一些新問題。
但是不要忘了在網站上找到答案,對我來說,我想問的幾乎所有問題都已經以類似的方式被問到了某個地方。

再次問好,我們將工作分為幾步,

首先,我想創建新的,打開,保存,另存為,導出到PDF菜單和事件

下面是我使用的代碼,GUI框架可以正確打開,包括新的,打開,保存,另存為,導出為pdf標簽,但操作沒有任何反應。

有人可以給我寫正確的代碼嗎? 請記住,我非常java初學者。

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);           

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM